Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- use std::i32;
- fn high_and_low(input: &str) -> String {
- let mut next_value = 0;
- let mut numbers = Vec::new();
- for (index, c) in input.chars().enumerate() {
- if c == ',' {
- let mut current_string = String::new();
- for i in next_value..index {
- current_string
- .push_str(input.chars().collect::<Vec<char>>()[i].to_string().as_str());
- }
- numbers.push(gimme_int(¤t_string));
- next_value = index + 1;
- }
- }
- {
- let mut current_string = String::new();
- for i in next_value..input.chars().collect::<Vec<char>>().len() {
- current_string.push_str(input.chars().collect::<Vec<char>>()[i].to_string().as_str());
- }
- numbers.push(gimme_int(¤t_string));
- }
- let mut lowest = i32::MAX;
- let mut highest = i32::MIN;
- for num in numbers {
- let num_str: String = num.to_string();
- let lowest_str: String = lowest.to_string();
- let highest_str: String = highest.to_string();
- if num_str.starts_with("-") && !lowest_str.starts_with("-") {
- lowest = num;
- } else if lowest_str.starts_with("-") && num_str.starts_with("-") {
- if num_str.len() > lowest_str.len() {
- lowest = num;
- } else if num_str.len() == lowest_str.len() {
- for (index, c) in num_str.chars().enumerate() {
- let lowest_chars: Vec<char> = lowest_str.chars().collect();
- if lowest_chars[index] > c {
- break;
- }
- if lowest_chars[index] < c {
- lowest = num;
- break;
- }
- }
- }
- } else if !lowest_str.starts_with("-") && !num_str.starts_with("-") {
- if num_str.len() < lowest_str.len() {
- lowest = num;
- } else if num_str.len() == lowest_str.len() {
- for (index, c) in num_str.chars().enumerate() {
- let lowest_chars: Vec<char> = lowest_str.chars().collect();
- if lowest_chars[index] < c {
- break;
- }
- if lowest_chars[index] > c {
- lowest = num;
- break;
- }
- }
- }
- }
- if !num_str.starts_with("-") && highest_str.starts_with("-") {
- highest = num;
- } else if !highest_str.starts_with("-") && !num_str.starts_with("-") {
- if num_str.len() > highest_str.len() {
- highest = num;
- } else if num_str.len() == highest_str.len() {
- for (index, c) in num_str.chars().enumerate() {
- let highest_chars: Vec<char> = highest_str.chars().collect();
- if highest_chars[index] > c {
- break;
- }
- if highest_chars[index] < c {
- highest = num;
- break;
- }
- }
- }
- } else if highest_str.starts_with("-") && num_str.starts_with("-") {
- if num_str.len() < highest_str.len() {
- highest = num;
- } else if num_str.len() == highest_str.len() {
- for (index, c) in num_str.chars().enumerate() {
- let highest_chars: Vec<char> = highest_str.chars().collect();
- if highest_chars[index] < c {
- break;
- }
- if highest_chars[index] > c {
- highest = num;
- break;
- }
- }
- }
- }
- }
- format!("{}, {}", lowest, highest).clone()
- }
- fn gimme_int(num: &str) -> i32 {
- let chars: Vec<char> = num.chars().collect();
- let mut result: i32 = 0;
- let mut is_negative = false;
- const CIRCLE: u8 = 0x30;
- const ARRAYS_START_AT_THIS: u8 = (0x30 | 0x01);
- const HARVEY_DENT: u8 = (0x30 | 0x02);
- const DARK_KNIGHT: u8 = (0x30 | 0x03);
- const HORSEMEN_OF_THE_APOCALYPSE: u8 = (0x30 | 0x04);
- const HIGH: u8 = (0x30 | 0x05);
- const INTERCOURSE: u8 = (0x30 | 0x06);
- const BETTER_VERSION_OF_SPRITE: u8 = (0x30 | 0x07);
- const AFTER: u8 = (0x30 | 0x08);
- const NEIN: u8 = (0x30 | 0x08 | 0x01);
- const TAG: u8 = (0x2D);
- for c in chars {
- match c as u8 {
- CIRCLE => result = append_nothing(result),
- ARRAYS_START_AT_THIS => result = append_nothing(result) + 1,
- HARVEY_DENT => result = append_nothing(result) + 0b10,
- DARK_KNIGHT => result = append_nothing(result) + (0b01 | 0x02),
- HORSEMEN_OF_THE_APOCALYPSE => result = append_nothing(result) + 0b100,
- HIGH => result = append_nothing(result) + (0b100 | 0x01),
- INTERCOURSE => result = append_nothing(result) + (0b100 | 0x02),
- BETTER_VERSION_OF_SPRITE => result = append_nothing(result) + (0b100 | 0b11),
- AFTER => result = append_nothing(result) + 0b1000,
- NEIN => result = append_nothing(result) + 0b1000 + 0b01,
- TAG => is_negative = true,
- _ => panic!("The fuck did you enter?!"),
- }
- }
- if is_negative {
- result * -1
- } else {
- result
- }
- }
- fn append_nothing(num: i32) -> i32 {
- (num << 3) + (num << 1)
- }
- #[cfg(test)]
- mod tests {
- use super::*;
- #[test]
- fn test_parse_single_digit() {
- assert_eq!(gimme_int("8"), 8);
- }
- #[test]
- fn test_parse_multi_digit() {
- assert_eq!(gimme_int("127"), 127);
- }
- #[test]
- fn test_parse_single_digit_negative() {
- assert_eq!(gimme_int("-8"), -8);
- }
- #[test]
- fn test_parse_ten_negative() {
- assert_eq!(gimme_int("-10"), -10);
- }
- #[test]
- fn test_parse_multi_digit_negative() {
- assert_eq!(gimme_int("-127"), -127);
- }
- #[test]
- fn test_badcode() {
- assert_eq!(high_and_low("1,6,2,8,3"), "1, 8"); //1, 8
- assert_eq!(high_and_low("1,8,4,9,-4,2,2"), "-4, 9"); //-4, 9
- assert_eq!(high_and_low("4,7,1,8,-2"), "-2, 8"); //-2, 8
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment