This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
import sys, yaml, json | |
try: | |
# included in standard lib from Python 2.7 | |
from collections import OrderedDict | |
except ImportError: | |
# try importing the backported drop-in replacement | |
# it's available on PyPI |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// test/alphabet_position_spec.rs | |
use dev_challenge_47::alphabet_position; | |
use speculate::speculate; | |
speculate! { // <-- it's a macro but who cares | |
describe "alphabet_position" { | |
it "should replace 'a' with 1" { | |
assert_eq!(alphabet_position("a"), "1"); | |
} | |
it "should replace 'A' with 1" { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// test/alphabet_position_test.rs | |
#[test] | |
fn it_should_replace_the_sentence() { | |
let replaced = alphabet_position("The sunset sets at twelve o' clock."); | |
assert_eq!( | |
replaced, | |
"20 8 5 19 21 14 19 5 20 19 5 20 19 1 20 20 23 5 12 22 5 15 3 12 15 3 11" | |
); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// src/lib.rs | |
pub fn alphabet_position(s: &str) -> String { | |
s.to_lowercase() | |
.chars() | |
.filter(|x| x.is_alphabetic()) // <-- adding this line here | |
.map(|x| -> u8 { x as u8 - 'a' as u8 + 1 }) | |
.map(|x| -> String { x.to_string() }) | |
.collect::<Vec<String>>() | |
.join(" ") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// test/alphabet_position_test.rs | |
#[test] | |
fn it_should_ignore_non_characters() { | |
let replaced = alphabet_position("'a a. 2"); | |
assert_eq!(replaced, "1 1"); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// src/lib.rs | |
pub fn alphabet_position(s: &str) -> String { | |
s.to_lowercase() // <-- adding this line | |
.chars() | |
.map(|x| -> u8 { x as u8 - 'a' as u8 + 1 }) | |
.map(|x| -> String { x.to_string() }) | |
.collect::<Vec<String>>() | |
.join(" ") | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// test/alphabet_position_test.rs | |
#[test] | |
fn it_should_replace_the_capital_a_with_1() { | |
let replaced = alphabet_position("A"); | |
assert_eq!(replaced, "1"); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// src/lib.rs | |
pub fn alphabet_position(s: &str) -> String { | |
s.chars() // <-- get an iterator over all chars | |
.map(|x| -> u8 { x as u8 - 'a' as u8 + 1 }) // <-- substract the ascii value of 'a' | |
.map(|x| -> String { x.to_string() }) // <-- convert the char to a String | |
.collect::<Vec<String>>() // <-- collect a Vector of String | |
.join(" ") // <-- join the Strings by whitespace | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// src/lib.rs | |
pub fn alphabet_position(s: &str) -> String { | |
String::from("Hello") | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// test/alphabet_position_test.rs | |
use dev_challenge_47::alphabet_position; // <-- we don't have this function yet | |
#[test] // <-- how to define a test in rust | |
fn it_should_replace_the_a_with_1() { // <-- we express the requirement as name | |
let replaced = alphabet_position("a"); | |
assert_eq!(replaced, "1"); // <-- we assert that the both are equal | |
} |