Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to initialize the first term of the count and say sequence.
DSA Rust
let mut result = String::from([1]);
Attempts:
3 left
💡 Hint
Common Mistakes
Using "0" or other digits as the first term.
Forgetting to use quotes to make it a string.
✗ Incorrect
The count and say sequence always starts with "1" as the first term.
2fill in blank
mediumComplete the code to iterate through the sequence from 2 to n.
DSA Rust
for _ in 2..=[1] {
Attempts:
3 left
💡 Hint
Common Mistakes
Using the length of the current result instead of n.
Starting the loop from 0 or 1 incorrectly.
✗ Incorrect
We iterate from 2 up to n to build each term of the sequence.
3fill in blank
hardFix the error in the inner loop that counts consecutive characters.
DSA Rust
while j < result.len() && result.chars().nth(j) == result.chars().nth(i) [1] j += 1;
Attempts:
3 left
💡 Hint
Common Mistakes
Resetting count inside the loop causing wrong counts.
Decreasing count leading to negative values.
✗ Incorrect
We increment count for each matching character to count consecutive digits.
4fill in blank
hardFill both blanks to append count and character to the next sequence string.
DSA Rust
next.push_str(&count.to_string()); next.push([1]); // Move i to [2]
Attempts:
3 left
💡 Hint
Common Mistakes
Appending the wrong character from position j.
Moving i by 1 instead of count causing infinite loops.
✗ Incorrect
We append the count and the character at position i, then move i forward by count.
5fill in blank
hardFill all three blanks to complete the count and say function.
DSA Rust
fn count_and_say(n: usize) -> String {
let mut result = String::from("1");
for _ in 2..=[1] {
let mut next = String::new();
let mut i = 0;
while i < result.len() {
let mut count = 1;
let mut j = i + 1;
while j < result.len() && result.chars().nth(j) == result.chars().nth(i) {
count += 1;
j += 1;
}
next.push_str(&count.to_string());
next.push(result.chars().nth(i).unwrap());
i = [2];
}
result = next;
}
[3]
}Attempts:
3 left
💡 Hint
Common Mistakes
Returning the initial string instead of the last sequence.
Incorrectly updating i causing infinite loops.
✗ Incorrect
Loop runs to n, i moves by count, and the final result is the last generated string.
