Browse Source

Upto 03-equality

master
Akshay Dixit 4 years ago
parent
commit
4c7a569bb7
  1. 2
      .gitignore
  2. 8
      Cargo.toml
  3. 11
      src/bin/01-map.rs
  4. 1228
      src/bin/02-ocr.rs
  5. 1274
      src/bin/03-equality.rs
  6. 0
      src/bin/04-linkedlist.rs
  7. 50
      src/cipher.rs
  8. 2
      src/lib.rs
  9. 14
      src/strutils.rs

2
.gitignore vendored

@ -0,0 +1,2 @@
/target
Cargo.lock

8
Cargo.toml

@ -0,0 +1,8 @@
[package]
name = "pythonchallenge"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]

11
src/bin/01-map.rs

@ -0,0 +1,11 @@
use pythonchallenge::cipher::caesar;
// http://www.pythonchallenge.com/pc/def/map.html
const INPUT: &str = "g fmnc wms bgblr rpylqjyrc gr zw fylb. rfyrq ufyr amknsrcpq ypc dmp. bmgle gr gl zw fylb gq glcddgagclr ylb rfyr'q ufw rfgq rcvr gq qm jmle. sqgle qrpgle.kyicrpylq() gq pcamkkclbcb. lmu ynnjw ml rfc spj.";
fn main() {
println!("{}", caesar(INPUT, 2));
println!("{}", caesar("map", 2))
}

1228
src/bin/02-ocr.rs

File diff suppressed because it is too large Load Diff

1274
src/bin/03-equality.rs

File diff suppressed because it is too large Load Diff

0
src/bin/04-linkedlist.rs

50
src/cipher.rs

@ -0,0 +1,50 @@
fn ascii_char_step(ascii_char: u8, step: i32) -> u8 {
const LOWER_A : u8 = 'a' as u8;
const LOWER_Z : u8 = 'z' as u8;
const UPPER_A : u8 = 'A' as u8;
const UPPER_Z : u8 = 'Z' as u8;
match ascii_char {
LOWER_A..=LOWER_Z => {
((ascii_char - LOWER_A) as i32 + step).rem_euclid(26) as u8 + LOWER_A
}
UPPER_A..=UPPER_Z => {
((ascii_char - UPPER_A) as i32 + step).rem_euclid(26) as u8 + UPPER_A
}
_ => ascii_char
}
}
/// Apply caser / shift cipher on a ASCII string
pub fn caesar(input: &str, step: i32) -> String {
assert!(input.is_ascii(), "Only works for ASCII messages");
let modified_bytes = input.bytes().map(|c| {ascii_char_step(c, step)}).collect();
String::from_utf8(modified_bytes).unwrap()
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn simple_test() {
let input = "abcde";
assert_eq!(caesar(input, 1), "bcdef");
let input = "ABCDE";
assert_eq!(caesar(input, 3), "DEFGH");
}
#[test]
fn overflow() {
let input = "xyzab";
assert_eq!(caesar(input, 2), "zabcd");
}
#[test]
fn negative_step() {
let input = "DEFGH";
assert_eq!(caesar(input, -4), "ZABCD");
}
}

2
src/lib.rs

@ -0,0 +1,2 @@
pub mod cipher;
pub mod strutils;

14
src/strutils.rs

@ -0,0 +1,14 @@
/// Strip away ASCII control characters and whitespaces
pub fn strip_cw(input: &str) -> String {
input.chars().filter(|c| { !c.is_ascii_control() && !c.is_ascii_whitespace() }).collect()
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn simple_test() {
let input = "abc def ghi\njkl mno";
assert_eq!(strip_cw(input), "abcdefghijklmno");
}
}
Loading…
Cancel
Save