Skip to content

Added pig-latin exercise and Windows Testing Notes #319

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jul 19, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions _test/WINDOWS_README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# check_exercises.sh for Windows Rust Developers

It is possible to run `check-exercises.sh` on Windows 10, pointing to the Windows location for your GitHub repository. This is done with the Ubuntu on Windows subsystem.

## Enable Developer Mode
To run Ubuntu on Windows, you need to be in Developer Mode.

- Open Settings
- Open Update and Security
- Select For Developers on Left Side
- Change to Developer Mode from Sideload Apps

## Install

Start a PowerShell as Administrator.

Run the following:

Enable-WindowsOptionalFeature -Online -FeatureName Microsoft-Windows-Subsystem-Linux

## Run bash

The `bash` command now gives you a terminal in a Ubuntu Linux instance. You have access to Windows files via /mnt/[drive_letter]

Example: Windows user directory would be

/mnt/c/Users/username

## Installing Rust

Inside bash, you will not have access to Window's Rust. You need to install the Linux version of Rust.

curl -sf -L https://static.rust-lang.org/rustup.sh | sh

You also need to install a cc linker for Rust.

sudo apt-get install build-essential

## Running Tests

cd /mnt/c/[path of github project]
_test/check_exercises.sh

This will re-download and build any crates needed, as they only existed in your Windows Rust.
9 changes: 9 additions & 0 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -529,6 +529,15 @@
"external crates (optional)"
]
},
{
"uuid": "c21c379b-fb23-449b-809a-3c6ef1c31221",
"slug": "pig-latin",
"core": false,
"unlocked_by": null,
"difficulty": 4,
"topics": [
]
},
{
"uuid": "f3172997-91f5-4941-a76e-91c4b8eed401",
"slug": "anagram",
Expand Down
8 changes: 8 additions & 0 deletions exercises/pig-latin/Cargo-example.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[package]
name = "pig-latin"
version = "1.0.0"
authors = ["sacherjj <[email protected]>"]

[dependencies]
regex = "0.2"
lazy_static = "0.2"
4 changes: 4 additions & 0 deletions exercises/pig-latin/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions exercises/pig-latin/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[package]
name = "pig-latin"
version = "1.0.0"
authors = ["sacherjj <[email protected]>"]

[dependencies]
56 changes: 56 additions & 0 deletions exercises/pig-latin/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
# Pig Latin

Implement a program that translates from English to Pig Latin.

Pig Latin is a made-up children's language that's intended to be
confusing. It obeys a few simple rules (below), but when it's spoken
quickly it's really difficult for non-children (and non-native speakers)
to understand.

- **Rule 1**: If a word begins with a vowel sound, add an "ay" sound to
the end of the word.
- **Rule 2**: If a word begins with a consonant sound, move it to the
end of the word, and then add an "ay" sound to the end of the word.

There are a few more rules for edge cases, and there are regional
variants too.

See <http://en.wikipedia.org/wiki/Pig_latin> for more details.

## Rust Installation

Refer to the [exercism help page][help-page] for Rust installation and learning
resources.

## Writing the Code

Execute the tests with:

```bash
$ cargo test
```

All but the first test have been ignored. After you get the first test to
pass, remove the ignore flag (`#[ignore]`) from the next test and get the tests
to pass again. The test file is located in the `tests` directory. You can
also remove the ignore flag from all the tests to get them to run all at once
if you wish.

Make sure to read the [Crates and Modules](https://doc.rust-lang.org/stable/book/crates-and-modules.html) chapter if you
haven't already, it will help you with organizing your files.

## Feedback, Issues, Pull Requests

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!

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).

[help-page]: http://exercism.io/languages/rust
[crates-and-modules]: http://doc.rust-lang.org/stable/book/crates-and-modules.html

## Source

The Pig Latin exercise at Test First Teaching by Ultrasaurus [https://github.com/ultrasaurus/test-first-teaching/blob/master/learn_ruby/pig_latin/](https://github.com/ultrasaurus/test-first-teaching/blob/master/learn_ruby/pig_latin/)

## Submitting Incomplete Solutions
It's possible to submit an incomplete solution so you can see how others have completed the exercise.
28 changes: 28 additions & 0 deletions exercises/pig-latin/example.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#[macro_use] extern crate lazy_static;
extern crate regex;

use regex::Regex;

// Regular expressions from Python version of exercism

pub fn translate_word(word: &str) -> String {
// Prevent creation and compilation at every call.
// These are compiled exactly once
lazy_static! {
// Detects if it starts with a vowel
static ref VOWEL: Regex = Regex::new(r"^([aeiou]|y[^aeiou]|xr)[a-z]*").unwrap();
// Detects splits for initial consonants
static ref CONSONANTS: Regex = Regex::new(r"^([^aeiou]?qu|[^aeiou]+)([a-z]*)").unwrap();
}

if VOWEL.is_match(word) {
String::from(word) + "ay"
} else {
let caps = CONSONANTS.captures(word).unwrap();
String::from(&caps[2]) + &caps[1] + "ay"
}
}

pub fn translate(text: &str) -> String {
text.split(" ").map(|w| translate_word(w)).collect::<Vec<_>>().join(" ")
}
120 changes: 120 additions & 0 deletions exercises/pig-latin/tests/pig-latin.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
extern crate pig_latin as pl;

#[test]
fn test_word_beginning_with_a() {
assert_eq!(pl::translate("apple"), "appleay");
}

#[test]
#[ignore]
fn test_word_beginning_with_e() {
assert_eq!(pl::translate("ear"), "earay");
}

#[test]
#[ignore]
fn test_word_beginning_with_i() {
assert_eq!(pl::translate("igloo"), "iglooay");
}

#[test]
#[ignore]
fn test_word_beginning_with_o() {
assert_eq!(pl::translate("object"), "objectay");
}

#[test]
#[ignore]
fn test_word_beginning_with_u() {
assert_eq!(pl::translate("under"), "underay");
}

#[test]
#[ignore]
fn test_word_beginning_with_a_vowel_and_followed_by_a_qu() {
assert_eq!(pl::translate("equal"), "equalay");
}

#[test]
#[ignore]
fn test_word_beginning_with_p() {
assert_eq!(pl::translate("pig"), "igpay");
}

#[test]
#[ignore]
fn test_word_beginning_with_k() {
assert_eq!(pl::translate("koala"), "oalakay");
}

#[test]
#[ignore]
fn test_word_beginning_with_y() {
assert_eq!(pl::translate("yellow"), "ellowyay");
}

#[test]
#[ignore]
fn test_word_beginning_with_x() {
assert_eq!(pl::translate("xenon"), "enonxay");
}

#[test]
#[ignore]
fn test_word_beginning_with_q_without_a_following_u() {
assert_eq!(pl::translate("qat"), "atqay");
}

#[test]
#[ignore]
fn test_word_beginning_with_ch() {
assert_eq!(pl::translate("chair"), "airchay");
}

#[test]
#[ignore]
fn test_word_beginning_with_qu() {
assert_eq!(pl::translate("queen"), "eenquay");
}

#[test]
#[ignore]
fn test_word_beginning_with_qu_and_a_preceding_consonant() {
assert_eq!(pl::translate("square"), "aresquay");
}

#[test]
#[ignore]
fn test_word_beginning_with_th() {
assert_eq!(pl::translate("therapy"), "erapythay");
}

#[test]
#[ignore]
fn test_word_beginning_with_thr() {
assert_eq!(pl::translate("thrush"), "ushthray");
}

#[test]
#[ignore]
fn test_word_beginning_with_sch() {
assert_eq!(pl::translate("school"), "oolschay");
}

#[test]
#[ignore]
fn test_word_beginning_with_yt() {
assert_eq!(pl::translate("yttria"), "yttriaay");
}

#[test]
#[ignore]
fn test_word_beginning_with_xr() {
assert_eq!(pl::translate("xray"), "xrayay");
}

#[test]
#[ignore]
fn test_a_whole_phrase() {
assert_eq!(pl::translate("quick fast run"), "ickquay astfay unray");
}