Skip to content

Instantly share code, notes, and snippets.

@slapec
slapec / main.py
Created August 8, 2023 08:06
tkinter_late_binding
from functools import partial
import tkinter as tk
class GameTable(tk.Tk):
def __init__(self):
super().__init__()
for i in range(4):
label = tk.Label(self, text=f'Lap {i + 1}')
@slapec
slapec / gamel_of_life.py
Created April 10, 2021 21:00
python_derby_20210410
# coding: utf-8
"""
A móka odalent, a __main__-ben kezdődik.
Néhány gondolat:
Semmi kedvem nem volt indexekkel szórakozni, meg állandóan kétdimenziós tömböket bejárni,
ezért az volt az ötletem, hogy minden sejt egy objektum, aminek referenciája van a szomszédjaira.
Bár a sejtek bejárása nem e referenciák mentén történik, de ilyen szempontból egy nagy
láncolt lista az egész program.
import collections
def smallest_unique_number(numbers: list) -> int:
try:
least_common, population = collections.Counter(numbers).most_common()[-1]
assert population == 1
return least_common
except (AssertionError, IndexError):
return -1
import re
def patch(match):
incremented = str(int(match.group(0)) + 1)
padded = incremented.zfill(len(match.group(0)))
return padded
def increment_if_ends_with_int(text):
patched = re.sub('(\d+)$', patch, text)
if not patched[-1].isdigit():
import collections
def scramble(word, chars):
word_char_count = collections.Counter(word)
chars_char_count = collections.Counter(chars)
chars_char_count.subtract(word_char_count)
return all(chars_char_count[word_char] >= 0 for word_char in word_char_count.keys())
print(scramble('door', 'abcdeooor'))
import collections
def josephus_survivor(n, k):
population = collections.deque(range(1, n + 1))
retval = None
while population:
population.rotate(1 - k)
retval = population.popleft()
return retval
def longest_palindrome(text):
text_length = len(text)
def find(longest_possible):
for window in range(text_length - longest_possible + 1):
subtext = text[window:window + longest_possible]
if subtext == subtext[::-1]:
return longest_possible
else:
return find(longest_possible - 1)
@slapec
slapec / order.py
Last active January 1, 2021 23:02
import re
items = ('burger', 'fries', 'chicken', 'pizza', 'sandwich', 'onionrings', 'milkshake', 'coke')
def get_order(s):
words = re.findall('|'.join(items), s, flags=re.IGNORECASE)
words.sort(key=items.index)
return ' '.join(map(str.capitalize, words))
@slapec
slapec / dump.py
Created October 22, 2016 09:54
dump and load script to migrate from the alpha version of wimm to the latest
# coding: utf-8
import json
import argparse
import os
import re
from pathlib import Path
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "wimm.settings")
@slapec
slapec / google_search_by_image.py
Created March 27, 2015 17:43
Google Search by Image from Python
# coding: utf-8
import requests
from lxml.html import fromstring
from requests_toolbelt.multipart.encoder import MultipartEncoder
def extract(soup):
for elem in soup.cssselect('.rc h3 a'):
href = elem.get('href')
if 'parom.hu' not in href: