From f418bda863d4c5a58430bc25a7da179b7c0281f3 Mon Sep 17 00:00:00 2001 From: andrey goverdovsky Date: Sat, 2 Jul 2016 11:36:32 +0300 Subject: [PATCH 1/3] add transpose tests and example solution --- exercises/transpose/Cargo.lock | 4 ++ exercises/transpose/Cargo.toml | 4 ++ exercises/transpose/example.rs | 20 ++++++++ exercises/transpose/tests/input-1.txt | 1 + exercises/transpose/tests/input-2.txt | 2 + exercises/transpose/tests/input-3.txt | 41 +++++++++++++++ exercises/transpose/tests/output-1.txt | 12 +++++ exercises/transpose/tests/output-2.txt | 16 ++++++ exercises/transpose/tests/output-3.txt | 70 ++++++++++++++++++++++++++ exercises/transpose/tests/transpose.rs | 43 ++++++++++++++++ 10 files changed, 213 insertions(+) create mode 100644 exercises/transpose/Cargo.lock create mode 100644 exercises/transpose/Cargo.toml create mode 100644 exercises/transpose/example.rs create mode 100644 exercises/transpose/tests/input-1.txt create mode 100644 exercises/transpose/tests/input-2.txt create mode 100644 exercises/transpose/tests/input-3.txt create mode 100644 exercises/transpose/tests/output-1.txt create mode 100644 exercises/transpose/tests/output-2.txt create mode 100644 exercises/transpose/tests/output-3.txt create mode 100644 exercises/transpose/tests/transpose.rs diff --git a/exercises/transpose/Cargo.lock b/exercises/transpose/Cargo.lock new file mode 100644 index 000000000..001a5d39b --- /dev/null +++ b/exercises/transpose/Cargo.lock @@ -0,0 +1,4 @@ +[root] +name = "transpose" +version = "0.0.0" + diff --git a/exercises/transpose/Cargo.toml b/exercises/transpose/Cargo.toml new file mode 100644 index 000000000..dcc723e22 --- /dev/null +++ b/exercises/transpose/Cargo.toml @@ -0,0 +1,4 @@ +[package] +name = "transpose" +version = "0.0.0" + diff --git a/exercises/transpose/example.rs b/exercises/transpose/example.rs new file mode 100644 index 000000000..98b53caac --- /dev/null +++ b/exercises/transpose/example.rs @@ -0,0 +1,20 @@ +pub fn transpose(in_lines: String) -> String { + let in_lines: Vec<&str> = in_lines.lines().collect(); + let out_lines_count = in_lines.iter().map(|l| l.len()).max().unwrap(); + let mut out_lines: Vec = vec![String::new(); out_lines_count]; + + for (out_line_n, out_line) in out_lines.iter_mut().enumerate() { + for in_line in &in_lines { + let c = (*in_line).chars().nth(out_line_n).unwrap_or(' '); + out_line.push(c); + } + } + + let mut out = String::new(); + for line in &out_lines { + out.push_str(line.trim_right()); + out.push('\n'); + } + out +} + diff --git a/exercises/transpose/tests/input-1.txt b/exercises/transpose/tests/input-1.txt new file mode 100644 index 000000000..1953487ba --- /dev/null +++ b/exercises/transpose/tests/input-1.txt @@ -0,0 +1 @@ +Single line. diff --git a/exercises/transpose/tests/input-2.txt b/exercises/transpose/tests/input-2.txt new file mode 100644 index 000000000..7f4264dcd --- /dev/null +++ b/exercises/transpose/tests/input-2.txt @@ -0,0 +1,2 @@ +The first line. +The second line. diff --git a/exercises/transpose/tests/input-3.txt b/exercises/transpose/tests/input-3.txt new file mode 100644 index 000000000..9573fc409 --- /dev/null +++ b/exercises/transpose/tests/input-3.txt @@ -0,0 +1,41 @@ +use std::fs::File; +use std::path::Path; +use std::io::Read; + +extern crate transpose; + +#[test] +fn single_line() { + let input = read_to_string("tests/input-1.txt"); + let output = read_to_string("tests/output-1.txt"); + assert_eq!(output, transpose::transpose(input)); +} + +#[test] +fn two_lines() { + let input = read_to_string("tests/input-2.txt"); + let output = read_to_string("tests/output-2.txt"); + assert_eq!(output, transpose::transpose(input)); +} + +#[test] +fn these_tests() { + let input = read_to_string("tests/input-3.txt"); + let output = read_to_string("tests/output-3.txt"); + assert_eq!(output, transpose::transpose(input)); +} + +fn read_to_string(filename: &str) -> String { + let file = File::open(&Path::new(filename)); + match file { + Err(e) => panic!("Couldn't open {}: {}", filename, e), + Ok(mut f) => { + let mut buf = String::new(); + match f.read_to_string(&mut buf) { + Err(e) => panic!("Couldn't read {}: {}", filename, e), + Ok(_) => buf + } + } + } +} + diff --git a/exercises/transpose/tests/output-1.txt b/exercises/transpose/tests/output-1.txt new file mode 100644 index 000000000..0e683d6c9 --- /dev/null +++ b/exercises/transpose/tests/output-1.txt @@ -0,0 +1,12 @@ +S +i +n +g +l +e + +l +i +n +e +. diff --git a/exercises/transpose/tests/output-2.txt b/exercises/transpose/tests/output-2.txt new file mode 100644 index 000000000..3c4a3ec19 --- /dev/null +++ b/exercises/transpose/tests/output-2.txt @@ -0,0 +1,16 @@ +TT +hh +ee + +fs +ie +rc +so +tn + d +l +il +ni +en +.e + . diff --git a/exercises/transpose/tests/output-3.txt b/exercises/transpose/tests/output-3.txt new file mode 100644 index 000000000..9ca7dbdd1 --- /dev/null +++ b/exercises/transpose/tests/output-3.txt @@ -0,0 +1,70 @@ +uuu e #f } #f } #f } f } +sss x [n [n [n n +eee t t t t + e es et et r +sss r silla swlla shlla elm } +ttt n tnees toees teees aea +ddd ]gtts ]_tts ]stts dtt +::: c l e l e e e _ c +::: r eior iior _ior tfhEO } +fpi a _nut nnut tnut oi rk +sao t lpt_ ept_ ept_ _lfr( +:t: e iupe supe supe sei(m +:h: ntuq (tuq ttuq t leulm } +F:R t e t! ) t! s t! r=e)tea +i:e r (= ( = ( (= ( i tt +lPa a ) =o { =o ) =o nF{=f c +ead n r u r u r u gi >)mhEO +;t; s {ert ert {ert (l u rk + h p aep aep aep fe p=tfr( + ; o dau dau dau i: a> .(_ + s _dt _dt _dt l: n bre) + e t_, t_, t_, eo i{ue) + ; ot ot ot np c fa = + _ot _ot _ot ae ! d=> + s_r s_r s_r mn ( =_> + tsa tsa tsa e( " t b + rtn rtn rtn :& C Sopu + irs irs irs P o t_af + nip nip nip &a u rsn + gno gno gno st l iti + (gs (gs (gs th d nrc + "(e "(e "(e r: n gi! + t": t": t": ): ' :n( + et: et: et: n t :g" + set set set -e n(C + tsr tsr tsr >w o e&o + sta sta sta ( p wmu + /sn /sn /sn Sf e (ul + i/s i/s i/s ti n )td + nop nop nop rl ; n + puo puo puo ie { b' + uts uts uts nn } ut + tpe tpe tpe ga : f + -u( -u( -u( m )r + 1ti 2ti 3ti {e { e + .-n .-n .-n ) } {a + t1p t2p t3p ) " d + x.u x.u x.u ; , + ttt ttt ttt { + "x) "x) "x) f } + )t) )t) )t) i : + ;"; ;"; ;"; l + ) ) ) e { + ; ; ; n } + a " + m , + e + , f + i + e l + ) e + , n + a + m + e + , + + e + ) + , diff --git a/exercises/transpose/tests/transpose.rs b/exercises/transpose/tests/transpose.rs new file mode 100644 index 000000000..4265a10d8 --- /dev/null +++ b/exercises/transpose/tests/transpose.rs @@ -0,0 +1,43 @@ +use std::fs::File; +use std::path::Path; +use std::io::Read; + +extern crate transpose; + +#[test] +fn single_line() { + let input = read_to_string("tests/input-1.txt"); + let output = read_to_string("tests/output-1.txt"); + assert_eq!(output, transpose::transpose(input)); +} + +#[test] +#[ignore] +fn two_lines() { + let input = read_to_string("tests/input-2.txt"); + let output = read_to_string("tests/output-2.txt"); + assert_eq!(output, transpose::transpose(input)); +} + +#[test] +#[ignore] +fn these_tests() { + let input = read_to_string("tests/input-3.txt"); + let output = read_to_string("tests/output-3.txt"); + assert_eq!(output, transpose::transpose(input)); +} + +fn read_to_string(filename: &str) -> String { + let file = File::open(&Path::new(filename)); + match file { + Err(e) => panic!("Couldn't open {}: {}", filename, e), + Ok(mut f) => { + let mut buf = String::new(); + match f.read_to_string(&mut buf) { + Err(e) => panic!("Couldn't read {}: {}", filename, e), + Ok(_) => buf + } + } + } +} + From ad8d5a3ba8aa66479c43ea47701185c19fdd7027 Mon Sep 17 00:00:00 2001 From: andrey goverdovsky Date: Sat, 2 Jul 2016 15:09:47 +0300 Subject: [PATCH 2/3] add transpose to config.json --- config.json | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/config.json b/config.json index 059600eec..20c3c7abd 100644 --- a/config.json +++ b/config.json @@ -37,7 +37,8 @@ "tournament", "rectangles", "forth", - "circular-buffer" + "circular-buffer", + "transpose" ], "deprecated": [ From f9cd68ea2214ea6155a1bba9957a9644a7d0c5e8 Mon Sep 17 00:00:00 2001 From: andrey goverdovsky Date: Sat, 2 Jul 2016 15:11:42 +0300 Subject: [PATCH 3/3] move transpose higher up in config.json --- config.json | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/config.json b/config.json index 20c3c7abd..8bbf60b10 100644 --- a/config.json +++ b/config.json @@ -12,6 +12,7 @@ "beer-song", "difference-of-squares", "hamming", + "transpose", "scrabble-score", "pangram", "nucleotide-count", @@ -37,8 +38,7 @@ "tournament", "rectangles", "forth", - "circular-buffer", - "transpose" + "circular-buffer" ], "deprecated": [