Решение на Дигитален корен от Борислав Тодоров

Обратно към всички решения

Към профила на Борислав Тодоров

Резултати

  • 7 точки от тестове
  • 0 бонус точки
  • 7 точки общо
  • 2 успешни тест(а)
  • 4 неуспешни тест(а)

Код

/// Десетична бройна система: 0-9
pub fn decimal(input: &str) -> Option<u32> {
let mut my_int = input.chars().map(|c| c.to_digit(10).unwrap()).sum::<u32>();
let mut temp = 0;
while !num_digits_decimal(my_int) {
while my_int != 0 {
temp = temp + my_int%10;
my_int = my_int / 10;
}
my_int = temp;
temp = 0;
}
Some(my_int)
}
/// Шестнадесетична бройна система: 0-9, последвано от a-f
pub fn hex(input: &str) -> Option<u32> {
unimplemented!()
}
/// Осмична бройна система: 0-7
pub fn octal(input: &str) -> Option<u32> {
let mut my_int = input.chars().map(|c| c.to_digit(10).unwrap()).sum::<u32>();
let mut temp = 0;
while !num_digits_octal(my_int) {
while my_int != 0 {
temp = temp + my_int%10;
my_int = my_int / 10;
}
my_int = temp;
temp = 0;
while my_int != 0 {
temp = temp + my_int%8;
my_int = my_int/8;
}
my_int = temp;
temp = 0;
}
Some(my_int)
}
/// Двоична бройна система: 0-1
pub fn binary(input: &str) -> Option<u32> {
let mut my_int = input.chars().map(|c| c.to_digit(10).unwrap()).sum::<u32>();
let mut temp = 0;
while !num_digits_binary(my_int) {
while my_int != 0 {
temp = temp + my_int%10;
my_int = my_int / 10;
}
my_int = temp;
temp = 0;
while my_int != 0 {
temp = temp + my_int%2;
my_int = my_int/2;
}
my_int = temp;
temp = 0;
}
Some(my_int)
}
pub fn num_digits_octal(number: u32) -> bool{
if number >= 0 && number <= 7 {
true
}
else {
false
}
}
pub fn num_digits_binary(number: u32) -> bool {
if number == 1 || number == 0 {
true
}
else {
false
}
}
pub fn num_digits_decimal(number: u32) -> bool {
if (number / 10) == 0 {
true
}
else {
false
}
}
fn main() {
//assert_eq!(decimal("345"), Some(3));
//assert_eq!(binary("011110000111"), Some(0));
assert_eq!(binary("116"), Some(1));
//println!("{}", my_int);
}

Лог от изпълнението

Compiling solution v0.1.0 (/tmp/d20200111-2173579-1qegrkl/solution)
warning: unused variable: `input`
  --> src/lib.rs:20:12
   |
20 | pub fn hex(input: &str) -> Option<u32> {
   |            ^^^^^ help: consider prefixing with an underscore: `_input`
   |
   = note: `#[warn(unused_variables)]` on by default

warning: function is never used: `main`
   --> src/lib.rs:103:1
    |
103 | fn main() {
    | ^^^^^^^^^
    |
    = note: `#[warn(dead_code)]` on by default

warning: comparison is useless due to type limits
  --> src/lib.rs:77:5
   |
77 |     if number >= 0 && number <= 7 {
   |        ^^^^^^^^^^^
   |
   = note: `#[warn(unused_comparisons)]` on by default

warning: unused variable: `input`
  --> src/lib.rs:20:12
   |
20 | pub fn hex(input: &str) -> Option<u32> {
   |            ^^^^^ help: consider prefixing with an underscore: `_input`
   |
   = note: `#[warn(unused_variables)]` on by default

warning: comparison is useless due to type limits
  --> src/lib.rs:77:5
   |
77 |     if number >= 0 && number <= 7 {
   |        ^^^^^^^^^^^
   |
   = note: `#[warn(unused_comparisons)]` on by default

    Finished test [unoptimized + debuginfo] target(s) in 2.31s
     Running target/debug/deps/solution-a73e64ec87929bd0

running 0 tests

test result: ok. 0 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out

     Running target/debug/deps/solution_test-38971695424b36d5

running 6 tests
test solution_test::test_binary ... ok
test solution_test::test_decimal_basic ... ok
test solution_test::test_hex_basic ... FAILED
test solution_test::test_invalid ... FAILED
test solution_test::test_octal_basic ... FAILED
test solution_test::test_zeroes ... FAILED

failures:

---- solution_test::test_hex_basic stdout ----
thread 'main' panicked at 'not yet implemented', src/lib.rs:21:5
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace.

---- solution_test::test_invalid stdout ----
thread 'main' panicked at 'called `Option::unwrap()` on a `None` value', src/libcore/option.rs:378:21

---- solution_test::test_octal_basic stdout ----
thread 'main' panicked at 'assertion failed: `(left == right)`
  left: `Some(3)`,
 right: `Some(5)`', tests/solution_test.rs:34:5

---- solution_test::test_zeroes stdout ----
thread 'main' panicked at 'not yet implemented', src/lib.rs:21:5


failures:
    solution_test::test_hex_basic
    solution_test::test_invalid
    solution_test::test_octal_basic
    solution_test::test_zeroes

test result: FAILED. 2 passed; 4 failed; 0 ignored; 0 measured; 0 filtered out

error: test failed, to rerun pass '--test solution_test'

История (2 версии и 1 коментар)

Борислав качи първо решение на 05.11.2019 16:41 (преди почти 6 години)

Борислав качи решение на 05.11.2019 16:55 (преди почти 6 години)

/// Десетична бройна система: 0-9
pub fn decimal(input: &str) -> Option<u32> {
let mut my_int = input.chars().map(|c| c.to_digit(10).unwrap()).sum::<u32>();
let mut temp = 0;
while !num_digits_decimal(my_int) {
while my_int != 0 {
temp = temp + my_int%10;
my_int = my_int / 10;
}
my_int = temp;
temp = 0;
}
Some(my_int)
}
/// Шестнадесетична бройна система: 0-9, последвано от a-f
pub fn hex(input: &str) -> Option<u32> {
unimplemented!()
-
-
}
/// Осмична бройна система: 0-7
pub fn octal(input: &str) -> Option<u32> {
- unimplemented!()
+ let mut my_int = input.chars().map(|c| c.to_digit(10).unwrap()).sum::<u32>();
+ let mut temp = 0;
+
+ while !num_digits_octal(my_int) {
+ while my_int != 0 {
+ temp = temp + my_int%10;
+ my_int = my_int / 10;
+ }
+
+ my_int = temp;
+ temp = 0;
+
+ while my_int != 0 {
+ temp = temp + my_int%8;
+ my_int = my_int/8;
+ }
+
+ my_int = temp;
+ temp = 0;
+ }
+
+ Some(my_int)
}
/// Двоична бройна система: 0-1
pub fn binary(input: &str) -> Option<u32> {
let mut my_int = input.chars().map(|c| c.to_digit(10).unwrap()).sum::<u32>();
let mut temp = 0;
while !num_digits_binary(my_int) {
while my_int != 0 {
temp = temp + my_int%10;
my_int = my_int / 10;
}
my_int = temp;
temp = 0;
while my_int != 0 {
temp = temp + my_int%2;
my_int = my_int/2;
}
my_int = temp;
temp = 0;
}
Some(my_int)
}
+pub fn num_digits_octal(number: u32) -> bool{
+ if number >= 0 && number <= 7 {
+ true
+ }
+ else {
+ false
+ }
+}
+
pub fn num_digits_binary(number: u32) -> bool {
- if ((number == 1) || (number == 0)) {
+ if number == 1 || number == 0 {
true
}
else {
false
}
}
pub fn num_digits_decimal(number: u32) -> bool {
if (number / 10) == 0 {
true
}
else {
false
}
}
fn main() {
//assert_eq!(decimal("345"), Some(3));
- assert_eq!(binary("011110000111"), Some(0));
-
+ //assert_eq!(binary("011110000111"), Some(0));
+ assert_eq!(binary("116"), Some(1));
//println!("{}", my_int);
}