Skip to content

Instantly share code, notes, and snippets.

@JackHowa
Created February 15, 2021 17:49
Show Gist options
  • Save JackHowa/52091442e678934d91c2110ed282314e to your computer and use it in GitHub Desktop.
Save JackHowa/52091442e678934d91c2110ed282314e to your computer and use it in GitHub Desktop.
Write a program that counts the number of occurrences of "11" in an input sequence of zeros and ones. The input of the program is just the sequence and it should return a single number, which is the number of occurrences of "11".
def count11(seq):
# define this function and return the number of occurrences as a number
# define int for occurences of num
occurences = 0
# loop through each element
# get length of seq
seq_length = len(seq)
for idx, val in enumerate(seq):
# next iteration in the index
next_index = idx + 1
# check if next index is last
if next_index >= seq_length:
break
# get the current and the next iteration in the index
current_int = val
next_int = seq[next_index]
# join the the two ints to be a string
joined_string = str(current_int) + str(next_int)
# compare to a "11" string
# if the joined int string is equal to "11"
# then iterate occurences one forward ++
# else do nothing
if joined_string == "11":
occurences += 1
return occurences
print(count11([0, 0, 1, 1, 1, 0])) # this should print 2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment