Skip to content

Instantly share code, notes, and snippets.

@jjlumagbas
Created November 3, 2016 02:10
Show Gist options
  • Save jjlumagbas/8c7ea909406dbee7e4666c912a31a1a0 to your computer and use it in GitHub Desktop.
Save jjlumagbas/8c7ea909406dbee7e4666c912a31a1a0 to your computer and use it in GitHub Desktop.
Remembering previous and Range
# given a list, remove successive duplicate elements
def remove_succ_lst(lst):
result = [lst[0]]
prev = lst[0]
for curr in lst:
if ( prev != curr ):
result = result + [curr]
prev = curr
return result
print(remove_succ_lst([1, 1])) # [1]
print(remove_succ_lst([1, 1, 1, 2, 2, 3, 4, 5])) # [1, 2, 3, 4, 5]
# print(remove_succ_str("helloooooo")) # "helo"
# print(remove_succ_str("heolloooooo")) # "heolo"
def show_prev(str):
prev = str[0]
for curr in str:
print("prev: " + prev + "/ curr: " + curr)
prev = curr
show_prev("this is a sample string")
# sum of all numbers from 0 to 9
def sum_range():
result = 0
for el in range(3, (3 * 5) + 1, 3):
result = result + el
return result
print(sum_range())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment