This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def blackjack(*abc): | |
val = sum(abc) | |
if val <= 21: | |
return val | |
if 11 in abc: | |
val -= 10 | |
if val <= 21: | |
return val | |
return 'BUST' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from itertools import takewhile, count; list(takewhile(lambda x: x[1] != 'password' and not print(f"wrong password. attempt {x[0]}"), ((i, input('enter password: ')) for i in count(1)))) == 1 or print('access granted') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Super simple data entry: just type the numers joined together and hit enter | |
# Ctrl-C to exit. | |
# Caveat: only works with each 0 <= score < 10. | |
# Copy the follwoing line to Python 3 and hit enter twice | |
while True: print(f'sum: {sum(int(a) for a in input("scores: "))}') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"strconv" | |
"strings" | |
) | |
func pop2(stack []float64) ([]float64, float64, float64) { | |
var ab []float64 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
import sys | |
import re | |
def list_files(directory, extension): | |
result = [] | |
paths = [os.path.join(directory, name) for name in os.listdir(directory)] | |
dirs = filter(os.path.isdir, paths) | |
matches_ext = lambda path: os.path.splitext(path)[1] == extension | |
files_matching_ext = filter(matches_ext, paths) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import re | |
def uicolor2hex(s): | |
""" Convert UIColor to HTML hex value. | |
I thought invisionapp only supports this format but eventually found the switch in settings. | |
Leaving it here in case someone needs it. | |
uicolor2hex('UIColor(red:0.08, green:0.14, blue:0.24, alpha:1)') == '#14233dff' | |
""" | |
m = re.match('UIColor\(red:(.*), green:(.*), blue:(.*), alpha:(.*)\)', s) | |
r, g, b, a = m.groups() | |
def hex_value(c): |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import itertools | |
from collections import defaultdict | |
dist = defaultdict(lambda: float('inf')) | |
cities = set() | |
with open('input9.txt') as f: | |
for line in f: | |
frags = line.strip().split() | |
src_dest = frags[0], frags[2] | |
dist[frozenset(src_dest)] = int(frags[4]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
total = 0 | |
with open('input8.txt') as f: | |
for line in f: | |
line = line.strip() | |
# part1: | |
# total += len(line) - len(eval(line)) | |
# part2: | |
new_repr = '"' + line.replace('\\', '\\\\').replace('"', '\\"') + '"' | |
total += len(new_repr) - len(line) | |
print(total) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from collections import namedtuple | |
import copy | |
Gate = namedtuple('Gate', 'op in1 in2 out') | |
gates = set() # gates that have not been calculated yet | |
wires = {} # identifier -> value | |
def get_value(gate_in): | |
if gate_in is None: | |
return None |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import itertools | |
on = set() | |
def get_min_max(fragments): | |
return tuple(int(v) for v in (fragments[0] + ',' + fragments[2]).split(',')) | |
def walk(min_x, min_y, max_x, max_y, func): | |
for coord in itertools.product(xrange(min_x, max_x+1), xrange(min_y, max_y+1)): | |
func(coord) |
NewerOlder