This file contains hidden or 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
#Python - Fibonacci sequence (Lazy solution). | |
def feb(first=0, second=1): | |
Next = first+second | |
yield Next | |
yield from feb(first=second, second=Next) | |
obj = feb() | |
for i in range(25): |
This file contains hidden or 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
# Python - How to check for a valid sudoku. | |
sudoku = [[8, 7, 5, 6, 2, 1, 9, 3, 4], | |
[6, 9, 4, 5, 3, 7, 2, 1, 8], | |
[1, 2, 3, 8, 4, 9, 7, 5, 6], | |
[4, 5, 1, 9, 7, 3, 6, 8, 2], | |
[3, 6, 7, 1, 8, 2, 5, 4, 9], | |
[2, 8, 9, 4, 5, 6, 1, 7, 3], | |
[5, 3, 6, 7, 9, 8, 4, 2, 1], |
This file contains hidden or 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 sys | |
from PIL import Image | |
import numpy as np | |
import math | |
def squircle(size=(50,50), color="black", level=4): | |
i = Image.new(mode = 'RGBA', size=size , color=color ) | |
l = level | |
w, h = i.size |
This file contains hidden or 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 random import random | |
from bisect import bisect | |
def weighted_choice(choices): | |
values, weights = zip(*choices) | |
total = 0 | |
cum_weights = [] | |
for w in weights: | |
total += w | |
cum_weights.append(total) |
This file contains hidden or 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 inf(n): | |
yield n | |
yield from inf(n+1) | |
def prime(s): | |
n = next(s) | |
yield n | |
yield from prime(i for i in s if i%n!=0) |
This file contains hidden or 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
joseph = lambda x: (lambda j: 1 if x == j else 2*(x-j)+1)(2**[i for i,j in enumerate(bin(x)[:1:-1]) if int(j)][::-1][0]) | |
print(joseph(10)) |
This file contains hidden or 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
n = 50 | |
prev = lambda n: [i for i,j in enumerate(bin(n)[:1:-1]) if int(j)][::-1][0] | |
powers = lambda x, t=[]: t+[1] if prev(x)==1 else powers(2**prev(x) - 1, t+[prev(x)]) | |
print(powers(n)) |
This file contains hidden or 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
feb = lambda n, t=[0, 1]: t if len(t)==n else feb(n, t+[sum(t[-2:])]) | |
print(feb(100)) |
This file contains hidden or 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 sys | |
import inspect | |
import textwrap | |
from collections import OrderedDict, UserString | |
from PyQt5 import QtCore, QtGui | |
from PyQt5.QtCore import Qt | |
from PyQt5.QtGui import QIcon | |
from PyQt5.QtWidgets import * | |