Skip to content

Instantly share code, notes, and snippets.

View opethe1st's full-sized avatar
🕶️
.

Ope opethe1st

🕶️
.
View GitHub Profile
@opethe1st
opethe1st / bisect.py
Last active April 3, 2020 14:05
Binary Search! - How would you write binary search?
def partition(nums, condition):
"""
nums is ordered so that all the cases where condition is false at the beginning and it is true towards the end.
We are tryng to find the first index where the condition is true.
e.g
nums = [1, 1, 1, 1, 2]
condition = lambda val: val < 2
partition will return 4
import re
def solution(formatString, replacements):
formatString = re.sub(r'([^{]){(\d+)}}', r'\g<1>{\g<2>}}}', formatString)
formatString = re.sub(r'{{(\d+)}([^}])', r'{{\g<1>}}\g<2>', formatString)
try:
return formatString.format(*replacements)
except ValueError:
class State:
def __init__(self, state):
self.state = state
def transistion(self, inp):
pass
class Start(State):
def solution(template, replacements):
ans = ''
i = 0
error = False
while i < len(template):
print(i)
if template[i] == '{':
if (i+1) < len(template) and template[i+1] == '{':
ans += '{'
def solution(template, replacements):
ans = ''
i = 0
iterator = TemplateIterator(template)
error = False
while template.hasnext():
if is_literal_brace(template):
ans += '{'
template.advance(2)
elif is_in_brace(template):