Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to convert a digit character to its integer value.
DSA Rust
let digit = s.chars().next().unwrap(); let num = (digit as u8) [1] b'0';
Attempts:
3 left
💡 Hint
Common Mistakes
Using '+' instead of '-' causes wrong number calculation.
Using '*' or '/' causes incorrect results or errors.
✗ Incorrect
Subtracting '0' from the digit character's byte value gives the integer value of the digit.
2fill in blank
mediumComplete the code to multiply the current result by 10 before adding the new digit.
DSA Rust
result = result [1] 10 + digit_value;
Attempts:
3 left
💡 Hint
Common Mistakes
Using '+' instead of '*' causes incorrect number formation.
Using '-' or '/' causes wrong results.
✗ Incorrect
Multiplying by 10 shifts the number left by one decimal place before adding the new digit.
3fill in blank
hardFix the error in the code to check if a character is a digit.
DSA Rust
if c [1] '0' && c [2] '9' { /* valid digit */ }
Attempts:
3 left
💡 Hint
Common Mistakes
Using == or != instead of range comparisons.
Using > or < without equals excludes boundary digits.
✗ Incorrect
To check if c is between '0' and '9', use c >= '0' and c <= '9'.
4fill in blank
hardFill both blanks to parse the sign and adjust the index accordingly.
DSA Rust
let mut sign = 1; let mut i = 0; if s.chars().nth(0) == Some([1]) { sign = -1; i = [2]; }
Attempts:
3 left
💡 Hint
Common Mistakes
Using '+' instead of '-' for negative sign.
Not updating index after sign detection.
✗ Incorrect
If the first character is '-', sign is -1 and index moves to 1 to skip the sign.
5fill in blank
hardFill all three blanks to build the final integer with sign and digit parsing.
DSA Rust
let mut result = 0; for c in s.chars().skip([1]) { if c >= '0' && c <= '9' { let digit = (c as u8) [2] b'0'; result = result [3] 10 + digit as i32; } else { break; } } result *= sign;
Attempts:
3 left
💡 Hint
Common Mistakes
Not skipping sign index causes wrong parsing.
Adding '0' instead of subtracting causes wrong digit.
Adding instead of multiplying result by 10 causes wrong number.
✗ Incorrect
Skip index i to start after sign, subtract '0' to get digit, multiply result by 10 before adding digit.
