Skip to content

Instantly share code, notes, and snippets.

@duanebester
Last active October 21, 2020 18:40
Show Gist options
  • Save duanebester/1d462f49c7aaf389e90cec82da4bdbaf to your computer and use it in GitHub Desktop.
Save duanebester/1d462f49c7aaf389e90cec82da4bdbaf to your computer and use it in GitHub Desktop.
Python Salute Challenge
def find_all(a_str, sub):
start = 0
while True:
start = a_str.find(sub, start)
if start == -1: return
yield start
start += len(sub)
def find_salutes(left, right):
accum = 0
for l in left:
for r in right:
if l < r:
accum += 1
return accum * 2
def count_salutes(s):
leftys = list(find_all(s, '>'))
rightys = list(find_all(s, '<'))
if leftys and rightys:
return find_salutes(leftys, rightys)
else: return 0
def solution(s):
return count_salutes(s)
print solution("<<>><") # 4
print solution(">----<") # 2
print solution(">---->") # 0
print solution("--->-><-><-->-") # 10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment