Skip to content

Instantly share code, notes, and snippets.

View lwiecek's full-sized avatar

Łukasz Więcek lwiecek

View GitHub Profile
@lwiecek
lwiecek / blackjack.py
Created September 29, 2022 21:08
blackjack
def blackjack(*abc):
val = sum(abc)
if val <= 21:
return val
if 11 in abc:
val -= 10
if val <= 21:
return val
return 'BUST'
@lwiecek
lwiecek / enter_password_oneline.py
Created October 22, 2021 20:12
Enter password with attempts counter - one line
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')
@lwiecek
lwiecek / sum_scores_oneline.py
Created October 18, 2021 22:04
Simple Scores Sum Tool
# 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: "))}')
@lwiecek
lwiecek / ReversePolishNotation.go
Last active December 14, 2021 11:57
Reverse Polish Notation Calculator in Golang
package main
import (
"fmt"
"strconv"
"strings"
)
func pop2(stack []float64) ([]float64, float64, float64) {
var ab []float64
@lwiecek
lwiecek / copy_strings.py
Last active July 25, 2018 14:45
Get strings that look like a copy string in a given directory (recursively)
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)
@lwiecek
lwiecek / convertUIColor.py
Created July 2, 2018 13:54
Convert UIColor to HTML hex value
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):
@lwiecek
lwiecek / day9.py
Created December 20, 2015 23:52
day9
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])
@lwiecek
lwiecek / day8.py
Created December 20, 2015 12:32
day8
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)
@lwiecek
lwiecek / day7.py
Created December 20, 2015 01:20
day7
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
@lwiecek
lwiecek / day6.py
Created December 19, 2015 20:35
adventofcode - day6
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)