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 convert(value): | |
assert isinstance(value, str) | |
value = value.lower() | |
if value == 'rock': | |
return 0 | |
elif value == 'paper': | |
return 1 | |
elif value == 'scissors': | |
return 2 | |
return -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
def perfect_cube(n): | |
return [ | |
tuple(map(int, row[2:].rjust(n, '0'))) | |
for row in map(bin, range(2**n)) | |
] | |
if __name__ == "__main__": | |
from pprint import pprint |
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 functools import partial | |
import operator as op | |
import numpy as np | |
def length(array): | |
assert np.ndim(array) == 1 | |
return np.shape(array)[0] | |
def all_positive(array): |
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
class Node: | |
def __init__(self, identifier, priority): | |
self.identifier = identifier | |
self.priority = priority | |
def set_priority(self, value): | |
self.priority = value | |
def __gt__(self, other): | |
return self.priority > other.priority |
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 matrix_multiplication(A, B): | |
m = len(A) | |
n = len(A[0]) | |
n2 = len(B) | |
k = len(B[0]) | |
assert n == n2 | |
assert all(len(row)==n for row in A) | |
assert all(len(row)==k for row in B) |
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 datetime as dt | |
import mimetypes | |
import requests | |
import json | |
import os | |
BASE_URL = "https://api.github.com/users/{username}/gists" | |
def clean(clause, fill='-', encoding='ascii'): | |
return clause.strip().replace(' ', fill).encode(encoding, 'ignore').decode(encoding) |
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 = 11 | |
cols = 3 | |
k = 0 | |
while k < n: | |
k += 1 | |
if k%cols == 0: | |
print(k) | |
else: | |
print(k, end=', ') |
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 transform(x, a, b): | |
''' projects x in [a, b] like a cycle. a-1=>b, b+1=>a ''' | |
n = b - a + 1 | |
return (x - a) % n + a | |
if __name__ == "__main__": | |
a = 1 | |
b = 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
from math import comb | |
def pascal_nth_row(n): | |
return [comb(n, i) for i in range(n+1)] | |
def pascal_triangle(rows): | |
return list(map(pascal_nth_row, range(rows))) | |
def print_pascal(rows): | |
for idx, row in enumerate(pascal_triangle(rows)): |
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 in_range(x, a, b): | |
''' checks if x is in [a, b] ''' | |
if x == a: return True | |
u = (b - a) / (x - a) | |
return u >= 1 | |
if __name__ == "__main__": | |
assert in_range(10, 1, 100) | |
assert in_range(10, 9, 11) |
NewerOlder