Skip to content

Instantly share code, notes, and snippets.

@slapec
slapec / js_arg_extract_w_pyv8.js
Last active August 29, 2015 14:15
Javascript function arguments extraction with PyV8
import PyV8
class Globals(PyV8.JSClass):
def __init__(self):
super(Globals, self).__init__()
self.coords = []
def PutMarkers(self, selector, coords):
coords = PyV8.convert(coords)
@slapec
slapec / tech_circle.html
Created February 26, 2015 19:48
Tech circle generator
<html>
<head>
</head>
<canvas id="c" width="800", height="600" style="display: none"></canvas>
<img id="kep">
<button id="f5">F5</button>
<script>
function newCircle(ctx){
ctx.beginPath();
@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:
@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 / 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))
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)
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
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 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 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