Skip to content

Commit f414ae4

Browse files
committed
Added pig-latin exercise.
Instructions for running check-exercises.sh for Windows Rust users.
1 parent 0a6e716 commit f414ae4

File tree

7 files changed

+365
-0
lines changed

7 files changed

+365
-0
lines changed

_test/WINDOWS_README.md

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
# check_exercises.sh for Windows Rust Developers
2+
3+
It is possible to run `check-exercises.sh` on Windows 10, pointing to the Windows location for you GitHub repository. This is done with the Ubunuto on Windows subsystem.
4+
5+
## Enable Developer Mode
6+
To run Ubunutu on Windows, you need to be in Developer Mode.
7+
8+
- Open Settings
9+
- Open Update and Security
10+
- Select For Developers on Left Side
11+
- Change to Developer Mode from Sideload Apps
12+
13+
## Install
14+
15+
Start a PowerShell as Administartor.
16+
17+
Run the following:
18+
19+
Enable-WindowsOptionalFeature -Online -FeatureName Microsoft-Windows-Subsystem-Linux
20+
21+
## Run bash
22+
23+
The `bash` command now gives you a terminal in a Ubuntu Linux instance. You have access to Windows files via /mnt/[drive_letter]
24+
25+
Example: Windows user directory would be
26+
27+
/mnt/c/Users/username
28+
29+
## Installing Rust
30+
31+
Inside bash, you will not have access to Window's Rust. You need to install the Linux version of Rust.
32+
33+
curl -sf -L https://static.rust-lang.org/rustup.sh | sh
34+
35+
You also need to install a cc linker for Rust.
36+
37+
sudo apt-get install build-essential
38+
39+
## Running Tests
40+
41+
cd /mnt/c/[path of github project]
42+
_test/check_exercises.sh
43+
44+
This will redownload and build any crates needed, as they only existed in your Windows Rust.
45+
46+
47+
48+
49+
50+
51+
52+

config.json

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -659,6 +659,16 @@
659659
"closures"
660660
]
661661
},
662+
{
663+
"uuid": "c21c379b-fb23-449b-809a-3c6ef1c31221",
664+
"slug": "pig-latin",
665+
"core": false,
666+
"unlocked_by": null,
667+
"difficulty": 2,
668+
"topics": [
669+
"regex"
670+
]
671+
},
662672
{
663673
"uuid": "8dae8f4d-368d-477d-907e-bf746921bfbf",
664674
"slug": "nucleotide-codons",

exercises/pig-latin/Cargo.lock

Lines changed: 89 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

exercises/pig-latin/Cargo.toml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
[package]
2+
name = "pig-latin"
3+
version = "0.1.0"
4+
authors = ["sacherjj <[email protected]>"]
5+
6+
[dependencies]
7+
regex = "0.2"
8+
lazy_static = "0.2"

exercises/pig-latin/README.md

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
# Pig Latin
2+
3+
Implement a program that translates from English to Pig Latin.
4+
5+
Pig Latin is a made-up children's language that's intended to be
6+
confusing. It obeys a few simple rules (below), but when it's spoken
7+
quickly it's really difficult for non-children (and non-native speakers)
8+
to understand.
9+
10+
- **Rule 1**: If a word begins with a vowel sound, add an "ay" sound to
11+
the end of the word.
12+
- **Rule 2**: If a word begins with a consonant sound, move it to the
13+
end of the word, and then add an "ay" sound to the end of the word.
14+
15+
There are a few more rules for edge cases, and there are regional
16+
variants too.
17+
18+
See <http://en.wikipedia.org/wiki/Pig_latin> for more details.
19+
20+
## Rust Installation
21+
22+
Refer to the [exercism help page][help-page] for Rust installation and learning
23+
resources.
24+
25+
## Writing the Code
26+
27+
Execute the tests with:
28+
29+
```bash
30+
$ cargo test
31+
```
32+
33+
All but the first test have been ignored. After you get the first test to
34+
pass, remove the ignore flag (`#[ignore]`) from the next test and get the tests
35+
to pass again. The test file is located in the `tests` directory. You can
36+
also remove the ignore flag from all the tests to get them to run all at once
37+
if you wish.
38+
39+
Make sure to read the [Crates and Modules](https://doc.rust-lang.org/stable/book/crates-and-modules.html) chapter if you
40+
haven't already, it will help you with organizing your files.
41+
42+
## Feedback, Issues, Pull Requests
43+
44+
The [exercism/rust](https://github.com/exercism/rust) repository on GitHub is the home for all of the Rust exercises. If you have feedback about an exercise, or want to help implement new exercises, head over there and create an issue. Members of the [rust track team](https://github.com/orgs/exercism/teams/rust) are happy to help!
45+
46+
If you want to know more about Exercism, take a look at the [contribution guide](https://github.com/exercism/docs/blob/master/contributing-to-language-tracks/README.md).
47+
48+
[help-page]: http://exercism.io/languages/rust
49+
[crates-and-modules]: http://doc.rust-lang.org/stable/book/crates-and-modules.html
50+
51+
## Source
52+
53+
Rust System: Tyler Long
54+
55+
This Exercise: Joe Sacher
56+
57+
## Submitting Incomplete Solutions
58+
It's possible to submit an incomplete solution so you can see how others have completed the exercise.

exercises/pig-latin/example.rs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#[macro_use] extern crate lazy_static;
2+
extern crate regex;
3+
4+
use regex::Regex;
5+
6+
// Regular expressions from Python version of exercism
7+
8+
pub fn translate_word(word: &str) -> String {
9+
// Prevent creation and compilation at every call.
10+
// These are compiled exactly once
11+
lazy_static! {
12+
// Detects if it starts with a vowel
13+
static ref VOWEL: Regex = Regex::new(r"^([aeiou]|y[^aeiou]|xr)[a-z]*").unwrap();
14+
// Detects splits for initial consonants
15+
static ref CONSONANTS: Regex = Regex::new(r"^([^aeiou]?qu|[^aeiou]+)([a-z]*)").unwrap();
16+
}
17+
18+
if VOWEL.is_match(word) {
19+
String::from(word) + "ay"
20+
} else {
21+
let caps = CONSONANTS.captures(word).unwrap();
22+
String::from(&caps[2]) + &caps[1] + "ay"
23+
}
24+
}
25+
26+
pub fn translate(text: &str) -> String {
27+
text.split(" ").map(|w| translate_word(w)).collect::<Vec<_>>().join(" ")
28+
}
Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
extern crate pig_latin as pl;
2+
3+
#[test]
4+
fn test_word_beginning_with_a() {
5+
assert_eq!(pl::translate("apple"), "appleay");
6+
}
7+
8+
#[test]
9+
#[ignore]
10+
fn test_word_beginning_with_e() {
11+
assert_eq!(pl::translate("ear"), "earay");
12+
}
13+
14+
#[test]
15+
#[ignore]
16+
fn test_word_beginning_with_i() {
17+
assert_eq!(pl::translate("igloo"), "iglooay");
18+
}
19+
20+
#[test]
21+
#[ignore]
22+
fn test_word_beginning_with_o() {
23+
assert_eq!(pl::translate("object"), "objectay");
24+
}
25+
26+
#[test]
27+
#[ignore]
28+
fn test_word_beginning_with_u() {
29+
assert_eq!(pl::translate("under"), "underay");
30+
}
31+
32+
#[test]
33+
#[ignore]
34+
fn test_word_beginning_with_a_vowel_and_followed_by_a_qu() {
35+
assert_eq!(pl::translate("equal"), "equalay");
36+
}
37+
38+
#[test]
39+
#[ignore]
40+
fn test_word_beginning_with_p() {
41+
assert_eq!(pl::translate("pig"), "igpay");
42+
}
43+
44+
#[test]
45+
#[ignore]
46+
fn test_word_beginning_with_k() {
47+
assert_eq!(pl::translate("koala"), "oalakay");
48+
}
49+
50+
#[test]
51+
#[ignore]
52+
fn test_word_beginning_with_y() {
53+
assert_eq!(pl::translate("yellow"), "ellowyay");
54+
}
55+
56+
#[test]
57+
#[ignore]
58+
fn test_word_beginning_with_x() {
59+
assert_eq!(pl::translate("xenon"), "enonxay");
60+
}
61+
62+
#[test]
63+
#[ignore]
64+
fn test_word_beginning_with_q_without_a_following_u() {
65+
assert_eq!(pl::translate("qat"), "atqay");
66+
}
67+
68+
#[test]
69+
#[ignore]
70+
fn test_word_beginning_with_ch() {
71+
assert_eq!(pl::translate("chair"), "airchay");
72+
}
73+
74+
#[test]
75+
#[ignore]
76+
fn test_word_beginning_with_qu() {
77+
assert_eq!(pl::translate("queen"), "eenquay");
78+
}
79+
80+
#[test]
81+
#[ignore]
82+
fn test_word_beginning_with_qu_and_a_preceding_consonant() {
83+
assert_eq!(pl::translate("square"), "aresquay");
84+
}
85+
86+
#[test]
87+
#[ignore]
88+
fn test_word_beginning_with_th() {
89+
assert_eq!(pl::translate("therapy"), "erapythay");
90+
}
91+
92+
#[test]
93+
#[ignore]
94+
fn test_word_beginning_with_thr() {
95+
assert_eq!(pl::translate("thrush"), "ushthray");
96+
}
97+
98+
#[test]
99+
#[ignore]
100+
fn test_word_beginning_with_sch() {
101+
assert_eq!(pl::translate("school"), "oolschay");
102+
}
103+
104+
#[test]
105+
#[ignore]
106+
fn test_word_beginning_with_yt() {
107+
assert_eq!(pl::translate("yttria"), "yttriaay");
108+
}
109+
110+
#[test]
111+
#[ignore]
112+
fn test_word_beginning_with_xr() {
113+
assert_eq!(pl::translate("xray"), "xrayay");
114+
}
115+
116+
#[test]
117+
#[ignore]
118+
fn test_a_whole_phrase() {
119+
assert_eq!(pl::translate("quick fast run"), "ickquay astfay unray");
120+
}

0 commit comments

Comments
 (0)