Skip to content
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

Added solutions in Rust #158

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
42 changes: 42 additions & 0 deletions Rust/integer_to_roman.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
fn int_to_rom(num: u32) -> String {
let table = vec![
(1000, "M"),
(900, "CM"),
(500, "D"),
(400, "CD"),
(100, "C"),
(90, "XC"),
(50, "L"),
(40, "XL"),
(10, "X"),
(9, "IX"),
(5, "V"),
(4, "IV"),
(1, "I"),
];

let mut num = num;
let mut s = String::new();
for t in table.iter() {
if num >= t.0 {
for _ in 0..(num / t.0) {
s.push_str(t.1);
}
num = num % t.0;
}
}
s
}

#[test]
fn it_works() {
assert_eq!(int_to_rom(1), "I");
assert_eq!(int_to_rom(8), "VIII");
assert_eq!(int_to_rom(1999), "MCMXCIX");
assert_eq!(int_to_rom(1994), "MCMXCIV");
assert_eq!(int_to_rom(320), "CCCXX");
println!("420: {}", int_to_rom(420));
println!("3999 {}", int_to_rom(3999));
println!("5000 {}", int_to_rom(5000));
println!("4000 {}", int_to_rom(4000));
}
26 changes: 26 additions & 0 deletions Rust/reverse_integer.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// Assuming only 32bit range integers.
fn reverse(num: i32) -> i32 {
let base: i64 = 2;
let upperbound = base.pow(31) - 1;
let lowerbound = -base.pow(31);
let mut input = num as i64;
let mut result: i64 = 0;
let mut digit: i64 = 0;
while input != 0 {
digit = input % 10;
result = result * 10 + digit;
input = input / 10;
}
if result > upperbound || result < lowerbound {
return 0;
}
result as i32
}

#[test]
fn it_works() {
let no = 133;
assert_eq!(reverse(no), 331);
assert_eq!(reverse(-65), -56);
assert_eq!(reverse(1991), 1991);
}
36 changes: 36 additions & 0 deletions Rust/roman_to_integer.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
fn rom_to_int(s: String) -> u32 {
let table: Vec<(u32, &'static str)> = vec![
(1000, "M"),
(900, "CM"),
(500, "D"),
(400, "CD"),
(100, "C"),
(90, "XC"),
(50, "L"),
(40, "XL"),
(10, "X"),
(9, "IX"),
(5, "V"),
(4, "IV"),
(1, "I"),
];
let mut sum = 0;
let mut i = 0;
for t in table.iter() {
while i + t.1.len() <= s.len() && t.1 == &s[i..i + t.1.len()] {
i += t.1.len();
sum += t.0;
if i >= s.len() {
return sum;
}
}
}
sum
}

#[test]
fn it_works() {
assert_eq!(rom_to_int("LVIII".to_string()), 58);
assert_eq!(rom_to_int("DCXXI".to_string()), 621);
assert_eq!(rom_to_int("MDXIV".to_string()), 1514);
}
54 changes: 54 additions & 0 deletions Rust/string_to_integer_atoi.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
fn atoi(s: String) -> i32 {
let (i32_min, i32_max) = (-2_i64.pow(31), 2_i64.pow(31) - 1);
let mut result: i64 = 0;
let chars = s.chars().into_iter();
let (mut minus, mut num_matched) = (false, false);
for c in chars {
if !num_matched {
match c {
' ' => {}
'-' => {
num_matched = true;
minus = true;
}
'0'..='9' => {
num_matched = true;
result = result * 10 + c.to_digit(10).unwrap() as i64;
}
'+' => {
num_matched = true;
}
_ => return 0,
}
} else {
match c {
'0'..='9' => {
result = result * 10 + c.to_digit(10).unwrap() as i64;
if result > i32_max {
break;
}
}
_ => break,
}
}
}
result = if minus { -result } else { result };
if result > i32_max {
return i32_max as i32;
}
if result < i32_min {
return i32_min as i32;
}
return result as i32;
}

#[test]
fn it_works() {
assert_eq!(atoi(String::from("32")), 32);
assert_eq!(atoi("100".to_string()), 100);
assert_eq!(atoi("bb".to_string()), 0);
assert_eq!(atoi("-2147483647".to_string()), -2147483647);
assert_eq!(atoi("2147483648".to_string()), 2147483647);
assert_eq!(atoi("-2147483649".to_string()), -2147483648);
assert_eq!(atoi(" ".to_string()), 0);
}
29 changes: 29 additions & 0 deletions Rust/valid_parentheses.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
fn is_pair(open: char, close: char) -> bool {
(open == '{' && close == '}') || (open == '[' && close == ']') || (open == '(' && close == ')')
}

fn is_valid(s: String) -> bool {
let chars = s.chars().into_iter();
let mut stack = Vec::new();
for c in chars {
match stack.last() {
None => {}
Some(&last) => {
if is_pair(last, c) {
stack.pop();
continue;
}
}
}
stack.push(c);
}
stack.is_empty()
}

#[test]
fn it_works() {
assert_eq!(is_valid("{}".to_string()), true);
assert_eq!(is_valid("[]".to_string()), true);
assert_eq!(is_valid("()".to_string()), true);
assert_eq!(is_valid("".to_string()), true);
}