Last active
June 8, 2018 01:14
-
-
Save shekohex/2e81833bc9360d798843034cc9c05c2d to your computer and use it in GitHub Desktop.
Constructing a Number - HackerRank Problems: https://www.hackerrank.com/challenges/constructing-a-number
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
100 | |
1 | |
1 | |
1 | |
2 | |
1 | |
3 | |
1 | |
4 | |
1 | |
5 | |
1 | |
6 | |
1 | |
7 | |
1 | |
8 | |
1 | |
9 | |
1 | |
10 | |
1 | |
11 | |
1 | |
12 | |
1 | |
13 | |
1 | |
14 | |
1 | |
15 | |
1 | |
16 | |
1 | |
17 | |
1 | |
18 | |
1 | |
19 | |
1 | |
20 | |
1 | |
1000000 | |
1 | |
999999 | |
1 | |
999998 | |
1 | |
999997 | |
1 | |
999996 | |
1 | |
999995 | |
1 | |
999994 | |
1 | |
999993 | |
1 | |
999992 | |
1 | |
999991 | |
1 | |
999990 | |
1 | |
999989 | |
1 | |
999988 | |
1 | |
999987 | |
1 | |
999986 | |
1 | |
999985 | |
1 | |
999984 | |
1 | |
999983 | |
1 | |
999982 | |
1 | |
999981 | |
1 | |
101010 | |
1 | |
260522 | |
1 | |
914575 | |
1 | |
436426 | |
1 | |
979445 | |
1 | |
648772 | |
1 | |
690081 | |
1 | |
933447 | |
1 | |
190629 | |
1 | |
703497 | |
1 | |
47202 | |
1 | |
407775 | |
1 | |
894325 | |
1 | |
963982 | |
1 | |
804784 | |
1 | |
968417 | |
1 | |
302156 | |
1 | |
631932 | |
1 | |
735902 | |
1 | |
895728 | |
1 | |
78537 | |
1 | |
723857 | |
1 | |
330739 | |
1 | |
286918 | |
1 | |
329211 | |
1 | |
539679 | |
1 | |
238506 | |
1 | |
63340 | |
1 | |
686568 | |
1 | |
361868 | |
1 | |
660016 | |
1 | |
287940 | |
1 | |
296263 | |
1 | |
224593 | |
1 | |
601449 | |
1 | |
836991 | |
1 | |
890310 | |
1 | |
823355 | |
1 | |
177068 | |
1 | |
11431 | |
1 | |
8580 | |
1 | |
291757 | |
1 | |
449218 | |
1 | |
374934 | |
1 | |
594328 | |
1 | |
163676 | |
1 | |
829355 | |
1 | |
996221 | |
1 | |
899080 | |
1 | |
195922 | |
1 | |
531545 | |
1 | |
748511 | |
1 | |
34067 | |
1 | |
575467 | |
1 | |
338674 | |
1 | |
284691 | |
1 | |
206504 | |
1 | |
999835 | |
1 | |
262034 | |
1 | |
344965 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#![feature(iterator_step_by)] | |
use std::io::{self, Read}; | |
/** | |
* Divisibility by 3 or 9. | |
* - First, take any number (for this example it will be 492) | |
* and add together each digit in the number (4 + 9 + 2 = 15). | |
* Then take that sum (15) and determine if it is divisible by 3. | |
* - The original number is divisible by 3 (or 9) | |
* if and only if the sum of its digits is divisible by 3 (or 9). | |
*/ | |
fn can_construct(nums: &mut [u32]) -> &'static str { | |
let sum: u32 = nums.iter().sum(); | |
if sum % 3 == 0 { | |
"Yes" | |
} else { | |
"No" | |
} | |
} | |
fn main() { | |
let mut line = Vec::new(); | |
let stdin = io::stdin(); | |
stdin | |
.lock() | |
.read_to_end(&mut line) | |
.expect("Could not read line"); | |
let buf = String::from_utf8(line).expect("Error While Convert STDIN to String"); | |
for l in buf.lines().skip(2).step_by(2) { | |
let str_nums: Vec<&str> = l.split_whitespace().collect(); | |
let mut nums: Vec<u32> = str_nums | |
.into_iter() | |
.map(|num| num.parse().expect("Error While Converting To u32")) | |
.collect(); | |
println!("{}", can_construct(&mut nums)); | |
} | |
} | |
#[cfg(test)] | |
mod tests { | |
use super::*; | |
#[test] | |
fn test_can_construct() { | |
assert_eq!(can_construct(&mut [1, 3, 5]), "Yes"); // 135 | |
assert_eq!(can_construct(&mut [2, 3, 5, 2]), "Yes"); // 2352 | |
assert_eq!(can_construct(&mut [90, 40, 50]), "Yes"); // 904050 | |
assert_eq!(can_construct(&mut [1999, 2000, 2001, 2003]), "No"); // 1999200020012003 | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
No | |
No | |
Yes | |
No | |
No | |
Yes | |
No | |
No | |
Yes | |
No | |
No | |
Yes | |
No | |
No | |
Yes | |
No | |
No | |
Yes | |
No | |
No | |
No | |
Yes | |
No | |
No | |
Yes | |
No | |
No | |
Yes | |
No | |
No | |
Yes | |
No | |
No | |
Yes | |
No | |
No | |
Yes | |
No | |
No | |
Yes | |
Yes | |
No | |
No | |
No | |
No | |
No | |
Yes | |
Yes | |
Yes | |
Yes | |
Yes | |
Yes | |
No | |
No | |
No | |
No | |
No | |
Yes | |
No | |
Yes | |
Yes | |
No | |
No | |
No | |
Yes | |
Yes | |
Yes | |
No | |
Yes | |
No | |
No | |
Yes | |
No | |
No | |
Yes | |
Yes | |
Yes | |
No | |
No | |
No | |
Yes | |
No | |
No | |
Yes | |
No | |
No | |
No | |
No | |
No | |
No | |
No | |
No | |
No | |
No | |
No | |
Yes | |
No | |
No | |
No | |
No |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment