Guest User

Bad Code Coding Challenge #12 - Ebrithil95

a guest
Jun 4th, 2019
285
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Rust 6.36 KB | None | 0 0
  1. use std::i32;
  2.  
  3. fn high_and_low(input: &str) -> String {
  4.     let mut next_value = 0;
  5.     let mut numbers = Vec::new();
  6.  
  7.     for (index, c) in input.chars().enumerate() {
  8.         if c == ',' {
  9.             let mut current_string = String::new();
  10.  
  11.             for i in next_value..index {
  12.                 current_string
  13.                     .push_str(input.chars().collect::<Vec<char>>()[i].to_string().as_str());
  14.             }
  15.  
  16.             numbers.push(gimme_int(&current_string));
  17.             next_value = index + 1;
  18.         }
  19.     }
  20.  
  21.     {
  22.         let mut current_string = String::new();
  23.  
  24.         for i in next_value..input.chars().collect::<Vec<char>>().len() {
  25.             current_string.push_str(input.chars().collect::<Vec<char>>()[i].to_string().as_str());
  26.         }
  27.  
  28.         numbers.push(gimme_int(&current_string));
  29.     }
  30.  
  31.     let mut lowest = i32::MAX;
  32.     let mut highest = i32::MIN;
  33.  
  34.     for num in numbers {
  35.         let num_str: String = num.to_string();
  36.         let lowest_str: String = lowest.to_string();
  37.         let highest_str: String = highest.to_string();
  38.  
  39.         if num_str.starts_with("-") && !lowest_str.starts_with("-") {
  40.             lowest = num;
  41.         } else if lowest_str.starts_with("-") && num_str.starts_with("-") {
  42.             if num_str.len() > lowest_str.len() {
  43.                 lowest = num;
  44.             } else if num_str.len() == lowest_str.len() {
  45.                 for (index, c) in num_str.chars().enumerate() {
  46.                     let lowest_chars: Vec<char> = lowest_str.chars().collect();
  47.                     if lowest_chars[index] > c {
  48.                         break;
  49.                     }
  50.  
  51.                     if lowest_chars[index] < c {
  52.                         lowest = num;
  53.                         break;
  54.                     }
  55.                 }
  56.             }
  57.         } else if !lowest_str.starts_with("-") && !num_str.starts_with("-") {
  58.             if num_str.len() < lowest_str.len() {
  59.                 lowest = num;
  60.             } else if num_str.len() == lowest_str.len() {
  61.                 for (index, c) in num_str.chars().enumerate() {
  62.                     let lowest_chars: Vec<char> = lowest_str.chars().collect();
  63.                     if lowest_chars[index] < c {
  64.                         break;
  65.                     }
  66.  
  67.                     if lowest_chars[index] > c {
  68.                         lowest = num;
  69.                         break;
  70.                     }
  71.                 }
  72.             }
  73.         }
  74.  
  75.         if !num_str.starts_with("-") && highest_str.starts_with("-") {
  76.             highest = num;
  77.         } else if !highest_str.starts_with("-") && !num_str.starts_with("-") {
  78.             if num_str.len() > highest_str.len() {
  79.                 highest = num;
  80.             } else if num_str.len() == highest_str.len() {
  81.                 for (index, c) in num_str.chars().enumerate() {
  82.                     let highest_chars: Vec<char> = highest_str.chars().collect();
  83.                     if highest_chars[index] > c {
  84.                         break;
  85.                     }
  86.  
  87.                     if highest_chars[index] < c {
  88.                         highest = num;
  89.                         break;
  90.                     }
  91.                 }
  92.             }
  93.         } else if highest_str.starts_with("-") && num_str.starts_with("-") {
  94.             if num_str.len() < highest_str.len() {
  95.                 highest = num;
  96.             } else if num_str.len() == highest_str.len() {
  97.                 for (index, c) in num_str.chars().enumerate() {
  98.                     let highest_chars: Vec<char> = highest_str.chars().collect();
  99.                     if highest_chars[index] < c {
  100.                         break;
  101.                     }
  102.  
  103.                     if highest_chars[index] > c {
  104.                         highest = num;
  105.                         break;
  106.                     }
  107.                 }
  108.             }
  109.         }
  110.     }
  111.  
  112.     format!("{}, {}", lowest, highest).clone()
  113. }
  114.  
  115. fn gimme_int(num: &str) -> i32 {
  116.     let chars: Vec<char> = num.chars().collect();
  117.     let mut result: i32 = 0;
  118.     let mut is_negative = false;
  119.  
  120.     const CIRCLE: u8 = 0x30;
  121.     const ARRAYS_START_AT_THIS: u8 = (0x30 | 0x01);
  122.     const HARVEY_DENT: u8 = (0x30 | 0x02);
  123.     const DARK_KNIGHT: u8 = (0x30 | 0x03);
  124.     const HORSEMEN_OF_THE_APOCALYPSE: u8 = (0x30 | 0x04);
  125.     const HIGH: u8 = (0x30 | 0x05);
  126.     const INTERCOURSE: u8 = (0x30 | 0x06);
  127.     const BETTER_VERSION_OF_SPRITE: u8 = (0x30 | 0x07);
  128.     const AFTER: u8 = (0x30 | 0x08);
  129.     const NEIN: u8 = (0x30 | 0x08 | 0x01);
  130.     const TAG: u8 = (0x2D);
  131.  
  132.     for c in chars {
  133.         match c as u8 {
  134.             CIRCLE => result = append_nothing(result),
  135.             ARRAYS_START_AT_THIS => result = append_nothing(result) + 1,
  136.             HARVEY_DENT => result = append_nothing(result) + 0b10,
  137.             DARK_KNIGHT => result = append_nothing(result) + (0b01 | 0x02),
  138.             HORSEMEN_OF_THE_APOCALYPSE => result = append_nothing(result) + 0b100,
  139.             HIGH => result = append_nothing(result) + (0b100 | 0x01),
  140.             INTERCOURSE => result = append_nothing(result) + (0b100 | 0x02),
  141.             BETTER_VERSION_OF_SPRITE => result = append_nothing(result) + (0b100 | 0b11),
  142.             AFTER => result = append_nothing(result) + 0b1000,
  143.             NEIN => result = append_nothing(result) + 0b1000 + 0b01,
  144.  
  145.             TAG => is_negative = true,
  146.             _ => panic!("The fuck did you enter?!"),
  147.         }
  148.     }
  149.  
  150.     if is_negative {
  151.         result * -1
  152.     } else {
  153.         result
  154.     }
  155. }
  156.  
  157. fn append_nothing(num: i32) -> i32 {
  158.     (num << 3) + (num << 1)
  159. }
  160.  
  161. #[cfg(test)]
  162. mod tests {
  163.     use super::*;
  164.  
  165.     #[test]
  166.     fn test_parse_single_digit() {
  167.         assert_eq!(gimme_int("8"), 8);
  168.     }
  169.  
  170.     #[test]
  171.     fn test_parse_multi_digit() {
  172.         assert_eq!(gimme_int("127"), 127);
  173.     }
  174.  
  175.     #[test]
  176.     fn test_parse_single_digit_negative() {
  177.         assert_eq!(gimme_int("-8"), -8);
  178.     }
  179.  
  180.     #[test]
  181.     fn test_parse_ten_negative() {
  182.         assert_eq!(gimme_int("-10"), -10);
  183.     }
  184.  
  185.     #[test]
  186.     fn test_parse_multi_digit_negative() {
  187.         assert_eq!(gimme_int("-127"), -127);
  188.     }
  189.  
  190.     #[test]
  191.     fn test_badcode() {
  192.         assert_eq!(high_and_low("1,6,2,8,3"), "1, 8"); //1, 8
  193.         assert_eq!(high_and_low("1,8,4,9,-4,2,2"), "-4, 9"); //-4, 9
  194.         assert_eq!(high_and_low("4,7,1,8,-2"), "-2, 8"); //-2, 8
  195.     }
  196. }
Advertisement
Add Comment
Please, Sign In to add comment