Skip to content

Add transpose exercise #151

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

Closed
wants to merge 3 commits into from
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
1 change: 1 addition & 0 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
"beer-song",
"difference-of-squares",
"hamming",
"transpose",
"scrabble-score",
"pangram",
"nucleotide-count",
Expand Down
4 changes: 4 additions & 0 deletions exercises/transpose/Cargo.lock

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

4 changes: 4 additions & 0 deletions exercises/transpose/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
[package]
name = "transpose"
version = "0.0.0"

20 changes: 20 additions & 0 deletions exercises/transpose/example.rs
Original file line number Diff line number Diff line change
@@ -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<String> = 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
}

1 change: 1 addition & 0 deletions exercises/transpose/tests/input-1.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Single line.
2 changes: 2 additions & 0 deletions exercises/transpose/tests/input-2.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
The first line.
The second line.
41 changes: 41 additions & 0 deletions exercises/transpose/tests/input-3.txt
Original file line number Diff line number Diff line change
@@ -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
}
}
}
}

12 changes: 12 additions & 0 deletions exercises/transpose/tests/output-1.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
S
i
n
g
l
e

l
i
n
e
.
16 changes: 16 additions & 0 deletions exercises/transpose/tests/output-2.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
TT
hh
ee

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this correct? I would expect that this line would have two spaces, as the original input also has two spaces.

fs
ie
rc
so
tn
d
l
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Similarly, I expected a space as the second character.

il
ni
en
.e
.
70 changes: 70 additions & 0 deletions exercises/transpose/tests/output-3.txt
Original file line number Diff line number Diff line change
@@ -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
)
,
43 changes: 43 additions & 0 deletions exercises/transpose/tests/transpose.rs
Original file line number Diff line number Diff line change
@@ -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
}
}
}
}