Skip to content
Closed
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
32 changes: 26 additions & 6 deletions exercises/allergies/example.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,16 @@
pub struct Allergies(pub usize);

#[derive(PartialEq, Debug)]
pub enum Allergen { Eggs, Peanuts, Shellfish, Strawberries, Tomatoes, Chocolate, Pollen, Cats }
pub enum Allergen {
Eggs,
Peanuts,
Shellfish,
Strawberries,
Tomatoes,
Chocolate,
Pollen,
Cats,
}

impl Allergies {
pub fn new(score: usize) -> Allergies {
Expand All @@ -11,16 +20,27 @@ impl Allergies {
pub fn is_allergic_to(&self, allergen: &Allergen) -> bool {
let allergens = Allergies::allergens();
let index = allergens.iter().position(|x: &Allergen| x == allergen).unwrap();
match self.0 & 1 << index { 0 => false, _ => true }
match self.0 & 1 << index {
0 => false,
_ => true,
}
}

pub fn allergies(&self) -> Vec<Allergen> {
Allergies::allergens().into_iter().filter(|allergen| self.is_allergic_to(allergen)).collect()
Allergies::allergens()
.into_iter()
.filter(|allergen| self.is_allergic_to(allergen))
.collect()
}

fn allergens() -> Vec<Allergen> {
vec![Allergen::Eggs, Allergen::Peanuts, Allergen::Shellfish,
Allergen::Strawberries, Allergen::Tomatoes, Allergen::Chocolate,
Allergen::Pollen, Allergen::Cats]
vec![Allergen::Eggs,
Allergen::Peanuts,
Allergen::Shellfish,
Allergen::Strawberries,
Allergen::Tomatoes,
Allergen::Chocolate,
Allergen::Pollen,
Allergen::Cats]
}
}
40 changes: 24 additions & 16 deletions exercises/beer-song/example.rs
Original file line number Diff line number Diff line change
@@ -1,25 +1,33 @@
pub fn verse(n: i32) -> String {
pub fn verse(n: i32) -> String {
match n {
0 => "No more bottles of beer on the wall, no more bottles of beer.\n\
Go to the store and buy some more, 99 bottles of beer on the wall.\n".to_string(),
1 => "1 bottle of beer on the wall, 1 bottle of beer.\n\
Take it down and pass it around, no more bottles of beer on the wall.\n".to_string(),
2 => "2 bottles of beer on the wall, 2 bottles of beer.\n\
Take one down and pass it around, 1 bottle of beer on the wall.\n".to_string(),
n if n > 2 && n <= 99 =>
format!(
"{n} bottles of beer on the wall, {n} bottles of beer.\n\
Take one down and pass it around, {n_minus_1} bottles of beer on the wall.\n",
n=n,
n_minus_1=n - 1),
_ =>
panic!(),
0 => {
"No more bottles of beer on the wall, no more bottles of beer.\nGo to the store and \
buy some more, 99 bottles of beer on the wall.\n"
.to_string()
}
1 => {
"1 bottle of beer on the wall, 1 bottle of beer.\nTake it down and pass it around, no \
more bottles of beer on the wall.\n"
.to_string()
}
2 => {
"2 bottles of beer on the wall, 2 bottles of beer.\nTake one down and pass it around, \
1 bottle of beer on the wall.\n"
.to_string()
}
n if n > 2 && n <= 99 => {
format!("{n} bottles of beer on the wall, {n} bottles of beer.\nTake one down and \
pass it around, {n_minus_1} bottles of beer on the wall.\n",
n = n,
n_minus_1 = n - 1)
}
_ => panic!(),
}
}

pub fn sing(start: i32, end: i32) -> String {
let mut song = Vec::new();
for n in (end .. start + 1).rev() {
for n in (end..start + 1).rev() {
song.push(verse(n))
}
song.join("\n")
Expand Down
32 changes: 26 additions & 6 deletions exercises/beer-song/tests/beer-song.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,35 +2,55 @@ extern crate beer_song as beer;

#[test]
fn test_verse_0() {
assert_eq!(beer::verse(0), "No more bottles of beer on the wall, no more bottles of beer.\nGo to the store and buy some more, 99 bottles of beer on the wall.\n");
assert_eq!(beer::verse(0),
"No more bottles of beer on the wall, no more bottles of beer.\nGo to the store \
and buy some more, 99 bottles of beer on the wall.\n");
}

#[test]
#[ignore]
fn test_verse_1() {
assert_eq!(beer::verse(1), "1 bottle of beer on the wall, 1 bottle of beer.\nTake it down and pass it around, no more bottles of beer on the wall.\n");
assert_eq!(beer::verse(1),
"1 bottle of beer on the wall, 1 bottle of beer.\nTake it down and pass it \
around, no more bottles of beer on the wall.\n");
}

#[test]
#[ignore]
fn test_verse_2() {
assert_eq!(beer::verse(2), "2 bottles of beer on the wall, 2 bottles of beer.\nTake one down and pass it around, 1 bottle of beer on the wall.\n");
assert_eq!(beer::verse(2),
"2 bottles of beer on the wall, 2 bottles of beer.\nTake one down and pass it \
around, 1 bottle of beer on the wall.\n");
}

#[test]
#[ignore]
fn test_verse_8() {
assert_eq!(beer::verse(8), "8 bottles of beer on the wall, 8 bottles of beer.\nTake one down and pass it around, 7 bottles of beer on the wall.\n");
assert_eq!(beer::verse(8),
"8 bottles of beer on the wall, 8 bottles of beer.\nTake one down and pass it \
around, 7 bottles of beer on the wall.\n");
}

#[test]
#[ignore]
fn test_song_8_6() {
assert_eq!(beer::sing(8, 6), "8 bottles of beer on the wall, 8 bottles of beer.\nTake one down and pass it around, 7 bottles of beer on the wall.\n\n7 bottles of beer on the wall, 7 bottles of beer.\nTake one down and pass it around, 6 bottles of beer on the wall.\n\n6 bottles of beer on the wall, 6 bottles of beer.\nTake one down and pass it around, 5 bottles of beer on the wall.\n");
assert_eq!(beer::sing(8, 6),
"8 bottles of beer on the wall, 8 bottles of beer.\nTake one down and pass it \
around, 7 bottles of beer on the wall.\n\n7 bottles of beer on the wall, 7 \
bottles of beer.\nTake one down and pass it around, 6 bottles of beer on the \
wall.\n\n6 bottles of beer on the wall, 6 bottles of beer.\nTake one down and \
pass it around, 5 bottles of beer on the wall.\n");
}

#[test]
#[ignore]
fn test_song_3_0() {
assert_eq!(beer::sing(3, 0), "3 bottles of beer on the wall, 3 bottles of beer.\nTake one down and pass it around, 2 bottles of beer on the wall.\n\n2 bottles of beer on the wall, 2 bottles of beer.\nTake one down and pass it around, 1 bottle of beer on the wall.\n\n1 bottle of beer on the wall, 1 bottle of beer.\nTake it down and pass it around, no more bottles of beer on the wall.\n\nNo more bottles of beer on the wall, no more bottles of beer.\nGo to the store and buy some more, 99 bottles of beer on the wall.\n");
assert_eq!(beer::sing(3, 0),
"3 bottles of beer on the wall, 3 bottles of beer.\nTake one down and pass it \
around, 2 bottles of beer on the wall.\n\n2 bottles of beer on the wall, 2 \
bottles of beer.\nTake one down and pass it around, 1 bottle of beer on the \
wall.\n\n1 bottle of beer on the wall, 1 bottle of beer.\nTake it down and pass \
it around, no more bottles of beer on the wall.\n\nNo more bottles of beer on \
the wall, no more bottles of beer.\nGo to the store and buy some more, 99 \
bottles of beer on the wall.\n");
}
13 changes: 9 additions & 4 deletions exercises/bob/example.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
pub fn reply(message: &str) -> &str {
if is_silence(message) { "Fine. Be that way!" }
else if is_yelling(message) { "Whoa, chill out!" }
else if is_question(message) { "Sure." }
else { "Whatever." }
if is_silence(message) {
"Fine. Be that way!"
} else if is_yelling(message) {
"Whoa, chill out!"
} else if is_question(message) {
"Sure."
} else {
"Whatever."
}
}

fn is_silence(message: &str) -> bool {
Expand Down
7 changes: 4 additions & 3 deletions exercises/bob/tests/bob.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ fn test_exclaiming() {
#[test]
#[ignore]
fn test_asking() {
assert_eq!("Sure.", bob::reply("Does this cryogenic chamber make me look fat?"));
assert_eq!("Sure.",
bob::reply("Does this cryogenic chamber make me look fat?"));
}

#[test]
Expand All @@ -32,7 +33,8 @@ fn test_shout_numbers() {
#[test]
#[ignore]
fn test_shout_weird_characters() {
assert_eq!("Whoa, chill out!", bob::reply("ZOMG THE %^*@#$(*^ ZOMBIES ARE COMING!!11!!1!"));
assert_eq!("Whoa, chill out!",
bob::reply("ZOMG THE %^*@#$(*^ ZOMBIES ARE COMING!!11!!1!"));
}

#[test]
Expand All @@ -52,4 +54,3 @@ fn test_non_question_with_question_mark() {
fn test_silent_treatment() {
assert_eq!("Fine. Be that way!", bob::reply(""));
}

19 changes: 9 additions & 10 deletions exercises/circular-buffer/example.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@ pub struct CircularBuffer<T: Default + Clone> {

impl<T: Default + Clone> CircularBuffer<T> {
// this circular buffer keeps an unallocated slot between the start and the end
// when the buffer is full.
// when the buffer is full.
pub fn new(size: usize) -> CircularBuffer<T> {
CircularBuffer {
buffer: vec![T::default(); size + 1],
size: size + 1,
start: 0,
end: 0
CircularBuffer {
buffer: vec![T::default(); size + 1],
size: size + 1,
start: 0,
end: 0,
}
}

Expand All @@ -36,12 +36,12 @@ impl<T: Default + Clone> CircularBuffer<T> {
pub fn write(&mut self, byte: T) -> Result<(), Error> {
if self.is_full() {
return Err(Error::FullBuffer);
}
}

self.buffer[self.end] = byte;
self.advance_end();
Ok(())

}

pub fn overwrite(&mut self, byte: T) {
Expand Down Expand Up @@ -73,5 +73,4 @@ impl<T: Default + Clone> CircularBuffer<T> {
fn advance_end(&mut self) {
self.end = (self.end + 1) % self.size;
}

}
14 changes: 7 additions & 7 deletions exercises/circular-buffer/tests/circular-buffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ mod tests {
buffer.write('2');
assert_eq!(Err(Error::FullBuffer), buffer.write('3'));
}

#[test]
#[ignore]
fn overwrite_item_in_non_full_buffer() {
Expand Down Expand Up @@ -94,20 +94,20 @@ mod tests {
let mut buffer = CircularBuffer::new(2);
buffer.write(1);
buffer.write(2);
assert_eq!(1,buffer.read().unwrap());
assert_eq!(1, buffer.read().unwrap());
buffer.write(-1);
assert_eq!(2,buffer.read().unwrap());
assert_eq!(-1,buffer.read().unwrap());
assert_eq!(2, buffer.read().unwrap());
assert_eq!(-1, buffer.read().unwrap());
assert_eq!(Err(Error::EmptyBuffer), buffer.read());
}

#[test]
#[ignore]
fn string_buffer() {
let mut buffer = CircularBuffer::new(2);
buffer.write("".to_string());
buffer.write("Testing".to_string());
assert_eq!(0,buffer.read().unwrap().len());
assert_eq!("Testing",buffer.read().unwrap());
assert_eq!(0, buffer.read().unwrap().len());
assert_eq!("Testing", buffer.read().unwrap());
}
}
2 changes: 1 addition & 1 deletion exercises/difference-of-squares/example.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ pub fn square_of_sum(n: usize) -> usize {
}

pub fn sum_of_squares(n: usize) -> usize {
(0..n+1).map(|x| x*x).fold(0, |accum, x| accum + x)
(0..n + 1).map(|x| x * x).fold(0, |accum, x| accum + x)
}

pub fn difference(n: usize) -> usize {
Expand Down
38 changes: 20 additions & 18 deletions exercises/dominoes/example.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ pub type Domino = (usize, usize);
/// dots and row dots. Positions are mirrored ((3,4) == (4,3)), except for positions with equal row
/// and column numbers.
struct AvailabilityTable {
m: Vec<usize>
m: Vec<usize>,
}

impl AvailabilityTable {
Expand All @@ -17,20 +17,19 @@ impl AvailabilityTable {
}

fn get(&self, x: usize, y: usize) -> usize {
self.m[(x-1) * 6 + (y-1)]
self.m[(x - 1) * 6 + (y - 1)]
}

fn set(&mut self, x: usize, y: usize, v: usize) {
let m = &mut self.m[..];
m[(x-1) * 6 + (y-1)] = v;
m[(x - 1) * 6 + (y - 1)] = v;
}

fn add(&mut self, x: usize, y: usize) {
if x == y {
let n = self.get(x, y);
self.set(x, y, n + 1) // Along the diagonal
}
else {
} else {
let m = self.get(x, y);
self.set(x, y, m + 1);
let n = self.get(y, x);
Expand All @@ -43,25 +42,23 @@ impl AvailabilityTable {
if x == y {
let n = self.get(x, y);
self.set(x, y, n - 1) // Along the diagonal
}
else {
} else {
let m = self.get(x, y);
self.set(x, y, m - 1);
let n = self.get(y, x);
self.set(y, x, n - 1);
}
}
else {
} else {
// For this toy code hard explicit fail is best
panic!("remove for 0 stones: ({:?}, {:?})", x, y)
}
}

fn pop_first(&mut self, x: usize) -> Option<usize> {
for y in 1 .. 7 {
for y in 1..7 {
if self.get(x, y) > 0 {
self.remove(x, y);
return Some(y)
return Some(y);
}
}
None
Expand All @@ -70,12 +67,18 @@ impl AvailabilityTable {

pub fn chain(dominoes: &Vec<Domino>) -> Option<Vec<Domino>> {
match dominoes.len() {
0 => Some(vec!()),
1 => if dominoes[0].0 == dominoes[0].1 { Some(vec![dominoes[0]]) } else { None },
0 => Some(vec![]),
1 => {
if dominoes[0].0 == dominoes[0].1 {
Some(vec![dominoes[0]])
} else {
None
}
}
_ => {
// First check if the total number of each amount of dots is even, if not it's not
// possible to complete a cycle. This follows from that it's an Eulerian path.
let mut v: Vec<usize> = vec!(0, 0, 0, 0, 0, 0);
let mut v: Vec<usize> = vec![0, 0, 0, 0, 0, 0];
// Keep the mutable borrow in a small scope here to allow v.iter().
{
let vs = &mut v[..];
Expand All @@ -86,14 +89,13 @@ pub fn chain(dominoes: &Vec<Domino>) -> Option<Vec<Domino>> {
}
for n in v.iter() {
if n % 2 != 0 {
return None
return None;
}
}
let chain = chain_worker(dominoes);
if chain.len() == dominoes.len() {
Some(chain)
}
else {
} else {
None
}
}
Expand Down
Loading