Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to print each character of the string word using a for loop.
DSA Go
word := "hello" for [1] := 0; [1] < len(word); [1]++ { fmt.Printf("%c ", word[[1]]) }
Attempts:
3 left
💡 Hint
Common Mistakes
Using different variable names in the for loop parts.
Starting the index from 1 instead of 0.
Using a variable not declared or unrelated.
✗ Incorrect
The variable 'i' is commonly used as the index in loops. It starts at 0 and goes up to len(word)-1 to access each character.
2fill in blank
mediumComplete the code to print the ASCII value of each character in the string text.
DSA Go
text := "GoLang" for i := 0; i < len(text); i++ { fmt.Println([1]) }
Attempts:
3 left
💡 Hint
Common Mistakes
Printing the whole string instead of a character.
Converting the character to string which prints the character, not ASCII.
Printing the index variable instead of the character.
✗ Incorrect
Accessing text[i] gives the byte value (ASCII) of the character at index i.
3fill in blank
hardFix the error in the code to correctly print each character of sentence as a rune (Unicode character).
DSA Go
sentence := "Hi!" for i := 0; i < len(sentence); i++ { fmt.Printf("%c ", [1]) }
Attempts:
3 left
💡 Hint
Common Mistakes
Printing sentence[i] directly which may not handle Unicode correctly.
Using string(sentence[i]) which converts to string, not rune.
Printing the whole sentence instead of characters.
✗ Incorrect
Casting to rune ensures the byte is treated as a Unicode character for printing.
4fill in blank
hardFill both blanks to create a map of characters to their counts from the string input.
DSA Go
input := "banana" counts := make(map[rune]int) for _, [1] := range input { counts[[2]]++ }
Attempts:
3 left
💡 Hint
Common Mistakes
Using the index variable instead of the character as map key.
Using different variable names in the loop and map access.
Not using rune type for map keys.
✗ Incorrect
The variable 'ch' holds each character (rune) from the string. We use it as the key to count occurrences.
5fill in blank
hardFill all three blanks to create a slice of runes from str and print each rune.
DSA Go
str := "code" runes := []rune([1]) for [2], [3] := range runes { fmt.Printf("%c ", r) }
Attempts:
3 left
💡 Hint
Common Mistakes
Using the string directly in the loop without conversion.
Using wrong variable names in the loop.
Not converting string to runes causing incorrect character handling.
✗ Incorrect
Convert string to rune slice with []rune(str). Use 'i' and 'r' as index and rune in the loop.
