ProgrammingHardAccenture · Cognizant
Run-length encoding replaces each run of equal characters by the character followed by its run length. The procedure below encodes s = "aaabccdddd" and prints the length of the encoded string. What is printed?
Run-length encoding replaces each run of equal characters by the character followed by its run length. The procedure below encodes s = "aaabccdddd" and prints the length of the encoded string. What is printed?
out = empty; i = 0
while i < n
count the run starting at i (length k); append char and k
i = i + k
end while
Print length of out- A. 8
- B. 10
- C. 4
- D. 7
Correct answer: A. 8
The encoding is a3b1c2d4, which has 8 characters. The original string has length 10, the distinct letter count is 4, and omitting the count of 1 gives 7.
Practise more Pseudocode and Code Tracing questionsPseudocode and Code Tracing: notes and key points
More Pseudocode and Code Tracing questions
- A Caesar cipher shifts each lowercase letter forward by 3 places, wrapping from z back to a. The procedure below encodes the word "xyz"…
- The number of distinct arrangements (permutations of the letters, with identical letters not counted twice) of the string "AABBC" is…
- The string "Programming Language" is taken in lowercase and the procedure below counts its vowels (a, e, i, o, u). What is printed? count…
- For the bracket string "(()(()))", the procedure below tracks the nesting depth and prints the maximum depth reached. What is printed?…
- Longest common prefix of the words "flower", "flow" and "flight" is found by comparing characters column by column until a mismatch. The…
- The words of "the sky is blue" are reversed in order (the words themselves are kept). The procedure below does this and prints the result…