Георги качи първо решение на 05.11.2019 16:15 (преди почти 6 години)
Кода минава тестовете, но можеше да бъде по-чист. Подозирам, че си бързал да го довършиш преди крайния срок... Затова е добре да си дадеш повече време преди срока :).
Compiling solution v0.1.0 (/tmp/d20200111-2173579-5c5qcn/solution) warning: unused variable: `val` --> src/lib.rs:35:21 | 35 | let val = match c { | ^^^ help: consider prefixing with an underscore: `_val` | = note: `#[warn(unused_variables)]` on by default warning: unused variable: `val` --> src/lib.rs:66:13 | 66 | let val = match c { | ^^^ help: consider prefixing with an underscore: `_val` warning: variable `quotient` is assigned to, but never used --> src/lib.rs:86:17 | 86 | let mut quotient; | ^^^^^^^^ | = note: consider using `_quotient` instead warning: value assigned to `quotient` is never read --> src/lib.rs:89:13 | 89 | quotient = sum / 16; | ^^^^^^^^ | = note: `#[warn(unused_assignments)]` on by default = help: maybe it is overwritten before being read? warning: unused variable: `val` --> src/lib.rs:126:13 | 126 | let val = match c { | ^^^ help: consider prefixing with an underscore: `_val` warning: variable `quotient` is assigned to, but never used --> src/lib.rs:143:17 | 143 | let mut quotient; | ^^^^^^^^ | = note: consider using `_quotient` instead warning: value assigned to `quotient` is never read --> src/lib.rs:146:13 | 146 | quotient = sum / 8; | ^^^^^^^^ | = help: maybe it is overwritten before being read? warning: unused variable: `val` --> src/lib.rs:172:13 | 172 | let val = match c { | ^^^ help: consider prefixing with an underscore: `_val` warning: variable `quotient` is assigned to, but never used --> src/lib.rs:189:17 | 189 | let mut quotient; | ^^^^^^^^ | = note: consider using `_quotient` instead warning: value assigned to `quotient` is never read --> src/lib.rs:192:13 | 192 | quotient = sum / 2; | ^^^^^^^^ | = help: maybe it is overwritten before being read? warning: variable does not need to be mutable --> src/lib.rs:40:13 | 40 | let mut digit = to_digit(c,10); | ----^^^^^ | | | help: remove this `mut` | = note: `#[warn(unused_mut)]` on by default warning: variable does not need to be mutable --> src/lib.rs:80:17 | 80 | let mut digit = to_digit(cc,16); | ----^^^^^ | | | help: remove this `mut` warning: variable does not need to be mutable --> src/lib.rs:138:17 | 138 | let mut digit = to_digit(cc,8); | ----^^^^^ | | | help: remove this `mut` warning: variable does not need to be mutable --> src/lib.rs:184:17 | 184 | let mut digit = to_digit(cc,2); | ----^^^^^ | | | help: remove this `mut` warning: function is never used: `main` --> src/lib.rs:207:1 | 207 | fn main() { | ^^^^^^^^^ | = note: `#[warn(dead_code)]` on by default warning: variable `newSum` should have a snake case name --> src/lib.rs:44:17 | 44 | let mut newSum = 0; | ^^^^^^ help: convert the identifier to snake case: `new_sum` | = note: `#[warn(non_snake_case)]` on by default warning: variable `vecHex` should have a snake case name --> src/lib.rs:63:13 | 63 | let mut vecHex = Vec::new(); | ^^^^^^ help: convert the identifier to snake case: `vec_hex` warning: variable `vecHex` should have a snake case name --> src/lib.rs:123:13 | 123 | let mut vecHex = Vec::new(); | ^^^^^^ help: convert the identifier to snake case: `vec_hex` warning: variable `vecHex` should have a snake case name --> src/lib.rs:169:13 | 169 | let mut vecHex = Vec::new(); | ^^^^^^ help: convert the identifier to snake case: `vec_hex` warning: unused variable: `val` --> src/lib.rs:35:21 | 35 | let val = match c { | ^^^ help: consider prefixing with an underscore: `_val` | = note: `#[warn(unused_variables)]` on by default warning: unused variable: `val` --> src/lib.rs:66:13 | 66 | let val = match c { | ^^^ help: consider prefixing with an underscore: `_val` warning: variable `quotient` is assigned to, but never used --> src/lib.rs:86:17 | 86 | let mut quotient; | ^^^^^^^^ | = note: consider using `_quotient` instead warning: value assigned to `quotient` is never read --> src/lib.rs:89:13 | 89 | quotient = sum / 16; | ^^^^^^^^ | = note: `#[warn(unused_assignments)]` on by default = help: maybe it is overwritten before being read? warning: unused variable: `val` --> src/lib.rs:126:13 | 126 | let val = match c { | ^^^ help: consider prefixing with an underscore: `_val` warning: variable `quotient` is assigned to, but never used --> src/lib.rs:143:17 | 143 | let mut quotient; | ^^^^^^^^ | = note: consider using `_quotient` instead warning: value assigned to `quotient` is never read --> src/lib.rs:146:13 | 146 | quotient = sum / 8; | ^^^^^^^^ | = help: maybe it is overwritten before being read? warning: unused variable: `val` --> src/lib.rs:172:13 | 172 | let val = match c { | ^^^ help: consider prefixing with an underscore: `_val` warning: variable `quotient` is assigned to, but never used --> src/lib.rs:189:17 | 189 | let mut quotient; | ^^^^^^^^ | = note: consider using `_quotient` instead warning: value assigned to `quotient` is never read --> src/lib.rs:192:13 | 192 | quotient = sum / 2; | ^^^^^^^^ | = help: maybe it is overwritten before being read? warning: variable does not need to be mutable --> src/lib.rs:40:13 | 40 | let mut digit = to_digit(c,10); | ----^^^^^ | | | help: remove this `mut` | = note: `#[warn(unused_mut)]` on by default warning: variable does not need to be mutable --> src/lib.rs:80:17 | 80 | let mut digit = to_digit(cc,16); | ----^^^^^ | | | help: remove this `mut` warning: variable does not need to be mutable --> src/lib.rs:138:17 | 138 | let mut digit = to_digit(cc,8); | ----^^^^^ | | | help: remove this `mut` warning: variable does not need to be mutable --> src/lib.rs:184:17 | 184 | let mut digit = to_digit(cc,2); | ----^^^^^ | | | help: remove this `mut` warning: variable `newSum` should have a snake case name --> src/lib.rs:44:17 | 44 | let mut newSum = 0; | ^^^^^^ help: convert the identifier to snake case: `new_sum` | = note: `#[warn(non_snake_case)]` on by default warning: variable `vecHex` should have a snake case name --> src/lib.rs:63:13 | 63 | let mut vecHex = Vec::new(); | ^^^^^^ help: convert the identifier to snake case: `vec_hex` warning: variable `vecHex` should have a snake case name --> src/lib.rs:123:13 | 123 | let mut vecHex = Vec::new(); | ^^^^^^ help: convert the identifier to snake case: `vec_hex` warning: variable `vecHex` should have a snake case name --> src/lib.rs:169:13 | 169 | let mut vecHex = Vec::new(); | ^^^^^^ help: convert the identifier to snake case: `vec_hex` Finished test [unoptimized + debuginfo] target(s) in 1.94s 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 ... ok test solution_test::test_invalid ... ok test solution_test::test_octal_basic ... ok test solution_test::test_zeroes ... ok test result: ok. 6 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out Doc-tests solution running 0 tests test result: ok. 0 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out
Кода минава тестовете, но можеше да бъде по-чист. Подозирам, че си бързал да го довършиш преди крайния срок... Затова е добре да си дадеш повече време преди срока :).
Хмм, не съм сигурен, че кода ще бъде по-бърз, actually :). Пак изпълняваш същия код и случая 0-9 е първи случай. Match statement-а се превежда до същото нещо. Евентуално ако radix-а е под 11, правиш по-малко проверки в случай на грешен вход, но тук сравняваш едно-единствено число. Единствено спестяваш 2 if-а в случай на грешка, на цената на 1 допълнителен
if
във всеки случай.Няма как да съм сигурен без да го benchmark-на, но си признавам, че 1) ме мързи, и 2) това е дооооста малка оптимизация :).
Ех, горе си мислил за performance, а тук правиш още един if за да провериш дали символа е валиден, вместо да се довериш на собствената си
to_digit
функция :).Тук можеше да използваш
char::from_digit
за да избегнеш тези if-клаузи.Има какво да се желае от имената на променливите. Какво е
c
? Може биchar
? Тогава защо вектора се казваvecHex
, а не, примерноchars
илиhex_chars
? А защоcc
?