Skip to content

Instantly share code, notes, and snippets.

@itarato
Created December 15, 2015 07:37
Show Gist options
  • Select an option

  • Save itarato/2abc8ad621cc2ad79225 to your computer and use it in GitHub Desktop.

Select an option

Save itarato/2abc8ad621cc2ad79225 to your computer and use it in GitHub Desktop.
Typoglycemia on dailyprogrammer 240 easy.
use std::fs::File;
use std::io::Read;
use rand::Rng;
extern crate rand;
fn main() {
let mut f = File::open("/Users/itarato/Documents/Rust/240.in").unwrap();
let mut content: String = String::new();
f.read_to_string(&mut content).ok().expect("Cannot read input");
let chars: Vec<char> = content.chars().collect();
let mut word_buf: Vec<char> = Vec::new();
let mut content_buf: Vec<char> = Vec::new();
for ch in chars {
match ch {
ch @ 'a' ... 'z' | ch @ 'A' ... 'Z' => {
word_buf.push(ch);
},
_ if word_buf.len() > 0 => {
shuffle(&mut word_buf);
content_buf.append(&mut word_buf);
content_buf.push(ch);
word_buf.clear();
},
_ => content_buf.push(ch),
}
}
let mut out = String::new();
for ch in content_buf {
out = format!("{}{}", out, &ch);
}
println!("{:?}", out);
}
fn shuffle(word: &mut Vec<char>) {
let mut rng = rand::thread_rng();
for i in 1..word.len() - 1 {
let rand_pos = rng.gen_range::<usize>(1, word.len() - 1);
word.swap(i, rand_pos);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment