Skip to content

Instantly share code, notes, and snippets.

@ckcr4lyf
Created October 17, 2020 02:24
Show Gist options
  • Save ckcr4lyf/ca726a861e51a1560accbe546b35f10f to your computer and use it in GitHub Desktop.
Save ckcr4lyf/ca726a861e51a1560accbe546b35f10f to your computer and use it in GitHub Desktop.
An example solution for A1 Q2
# COMP1117 A1Q2 sample solution
# By Raghu Saxena
result = []
previous_letter = 'a'
input_letter = input()
count = 0
while input_letter != '!':
if input_letter == previous_letter:
count += 1
else:
#If the count is zero, then we should not append it!
# (E.g. we type first letter as b, 'b' != 'a', but we never typed 'a')
# So we do not append 'a' and 0 to our result.
if count != 0:
result.append(previous_letter)
result.append(count)
count = 0
count += 1
previous_letter = input_letter
input_letter = input()
# Again, we check if count is not zero before appending the final letter
if count != 0:
result.append(previous_letter)
result.append(count)
print(result)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment