Skip to main content

check prop lambda

0 likes • Nov 19, 2022 • 0 views
Python
Loading...

More Python Posts

Lonely Integer

0 likes • Feb 26, 2023 • 0 views
Python
#84 48 13 20 61 20 33 97 34 45 6 63 71 66 24 57 92 74 6 25 51 86 48 15 64 55 77 30 56 53 37 99 9 59 57 61 30 97 50 63 59 62 39 32 34 4 96 51 8 86 10 62 16 55 81 88 71 25 27 78 79 88 92 50 16 8 67 82 67 37 84 3 33 4 78 98 39 64 98 94 24 82 45 3 53 74 96 9 10 94 13 79 15 27 56 66 32 81 77
# xor a list of integers to find the lonely integer
res = a[0]
for i in range(1,len(a)):
res = res ^ a[i]

Dictionary Sort

0 likes • Nov 18, 2022 • 0 views
Python
mydict = {'carl':40, 'alan':2, 'bob':1, 'danny':0}
# How to sort a dict by value Python 3>
sort = {key:value for key, value in sorted(mydict.items(), key=lambda kv: (kv[1], kv[0]))}
print(sort)
# How to sort a dict by key Python 3>
sort = {key:mydict[key] for key in sorted(mydict.keys())}
print(sort)

combine values

0 likes • Nov 19, 2022 • 0 views
Python
from collections import defaultdict
def combine_values(*dicts):
res = defaultdict(list)
for d in dicts:
for key in d:
res[key].append(d[key])
return dict(res)
d1 = {'a': 1, 'b': 'foo', 'c': 400}
d2 = {'a': 3, 'b': 200, 'd': 400}
combine_values(d1, d2) # {'a': [1, 3], 'b': ['foo', 200], 'c': [400], 'd': [400]}

screencap.py

0 likes • Jan 23, 2021 • 0 views
Python
"""
Take screenshots at x interval - make a movie of doings on a computer.
"""
import time
from datetime import datetime
import ffmpeg
import pyautogui
while True:
epoch_time = int(time.time())
today = datetime.now().strftime("%Y_%m_%d")
filename = str(epoch_time) + ".png"
print("taking screenshot: {0}".format(filename))
myScreenshot = pyautogui.screenshot()
myScreenshot.save(today + "/" + filename)
time.sleep(5)
#
# and then tie it together with: https://github.com/kkroening/ffmpeg-python/blob/master/examples/README.md#assemble-video-from-sequence-of-frames
#
"""
import ffmpeg
(
ffmpeg
.input('./2021_01_22/*.png', pattern_type='glob', framerate=25)
.filter('deflicker', mode='pm', size=10)
.filter('scale', size='hd1080', force_original_aspect_ratio='increase')
.output('movie.mp4', crf=20, preset='slower', movflags='faststart', pix_fmt='yuv420p')
.run()
)
"""

Find Coin

0 likes • Oct 4, 2023 • 8 views
Python
weigh = lambda a,b: sum(b)-sum(a)
FindCoin = lambda A: 0 if (n := len(A)) == 1 else (m := n//3) * (w := 1 + weigh(A[:m], A[2*m:])) + FindCoin(A[m*w:m*(w+1)])
print(FindCoin([1,1,1,1,1,1,1,2,1]))

delay time lambda

0 likes • Nov 19, 2022 • 0 views
Python
from time import sleep
def delay(fn, ms, *args):
sleep(ms / 1000)
return fn(*args)
delay(lambda x: print(x), 1000, 'later') # prints 'later' after one second