Skip to content

Instantly share code, notes, and snippets.

View opethe1st's full-sized avatar
🕶️
.

Opemipo opethe1st

🕶️
.
View GitHub Profile
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):
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 += '{'
class State:
def __init__(self, state):
self.state = state
def transistion(self, inp):
pass
class Start(State):
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:
@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
@opethe1st
opethe1st / context_manager.py
Created February 24, 2020 09:26
A context manager for managing contextvars
import contextlib
import contextvars
@contextlib.contextmanager
def context(contextvar: contextvars.ContextVar, value):
token = contextvar.set(value)
yield
contextvar.reset(token)
@opethe1st
opethe1st / idea.md
Last active February 16, 2020 13:13
A proposed DSL for data validation.

Data validation language

So I think it would be nice to have a DSL for just data validation (think jsonschema but just the bits that do with validation, no annotations and a simpler referencing system)

Why a DSL?

A DSL means that the language can be designed from the ground up for the best user experience and then compile to native languages ( the approach protobufs use for example). I don't propose any syntax or semantics that is radically different from what exists in standard programming languages today so I don't expect the learning curve to be high

Challenges with a DSL?

@opethe1st
opethe1st / json_pointer.py
Created November 30, 2019 17:52
Implementation of JsonPointer
def dereference(value, pointer):
tokens = get_reference_tokens(pointer)
current_value = value
while tokens:
current = tokens.pop(0)
if current == "":
return current_value
elif isinstance(current_value, dict):
@opethe1st
opethe1st / range_int.go
Created November 21, 2019 12:44
range_int that works like range in Python
package main
import (
"fmt"
)
func range_int(start int, end int, step int) func() (int, bool) {
current := start
previous := 0
return func() (int, bool) {
@opethe1st
opethe1st / underline.go
Created August 31, 2019 02:53
Underline a character - this is a snippet I found useful
package main
func underline(char rune) string {
return string([]rune{char, 204, 179})
}
func main(){
}