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 Python
def count_and_say(n): result = [1] return result
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using integer 1 instead of string "1".
Starting with "0" or 0 which is incorrect.
✗ Incorrect
The count-and-say sequence starts with the string "1" as the first term.
2fill in blank
mediumComplete the code to iterate from 1 to n-1 to build the sequence.
DSA Python
for i in range([1], n): pass
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Starting loop from 0 which repeats the first term.
Starting from n which skips all iterations.
✗ Incorrect
The loop starts from 1 because the first term is already initialized.
3fill in blank
hardFix the error in the inner loop that counts consecutive characters.
DSA Python
count = 1 for j in range(1, len(result)): if result[j] == result[j-1]: count [1] 1 else: count = 1
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using = which resets count instead of increasing.
Using -= which decreases count incorrectly.
✗ Incorrect
We increase count by 1 when consecutive characters match, so use +=.
4fill in blank
hardFill both blanks to append count and character to the next sequence string.
DSA Python
next_seq += str([1]) + [2]
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Appending the current character instead of previous.
Appending count without converting to string.
✗ Incorrect
Append the count as string and the previous character counted.
5fill in blank
hardFill all three blanks to complete the count-and-say function logic.
DSA Python
def count_and_say(n): result = [1] for i in range([2], n): next_seq = "" count = 1 for j in range(1, len(result)): if result[j] == result[j-1]: count [3] 1 else: next_seq += str(count) + result[j-1] count = 1 next_seq += str(count) + result[-1] result = next_seq return result
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using integer 1 instead of string "1" for result.
Starting loop from 0 instead of 1.
Using = instead of += to increment count.
✗ Incorrect
The function starts with "1" as string, loops from 1 to n, and increments count with +=.