-
Notifications
You must be signed in to change notification settings - Fork 544
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
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
[package] | ||
name = "transpose" | ||
version = "0.0.0" | ||
|
This file contains hidden or 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
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 | ||
} | ||
|
This file contains hidden or 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Single line. |
This file contains hidden or 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
The first line. | ||
The second line. |
This file contains hidden or 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
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 | ||
} | ||
} | ||
} | ||
} | ||
|
This file contains hidden or 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
S | ||
i | ||
n | ||
g | ||
l | ||
e | ||
|
||
l | ||
i | ||
n | ||
e | ||
. |
This file contains hidden or 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
TT | ||
hh | ||
ee | ||
|
||
fs | ||
ie | ||
rc | ||
so | ||
tn | ||
d | ||
l | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
. |
This file contains hidden or 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
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 | ||
) | ||
, |
This file contains hidden or 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
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 | ||
} | ||
} | ||
} | ||
} | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.