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
mat = {} | |
for i in range(5): | |
for j in range(5): | |
print((i,j), j+i*5) | |
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
txt = "Hello" | |
alpha = [chr(i) for i in range(65, 91)] | |
res = [] | |
for i in [txt[i:i+3] for i in range(0, len(txt), 3)]: | |
for x, y in zip([3, 5, 7], i):res.append(alpha[(ord(y)%65+x)%26]) | |
print("".join(res)) |
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
txt = "Hello".upper() | |
alpha = [chr(i) for i in range(65, 91)] | |
res = [] | |
for i in [txt[i:i+3] for i in range(0, len(txt), 3)]: | |
for x, y in zip([3, 5, 7], i):res.append(alpha[(ord(y)%65+x)%26]) | |
txt = "".join(res) | |
print("Encryption: "+txt) | |
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
text = "hello" | |
key = "key" | |
if len(key)<len(text):key=key*(len(text)//len(key)+1) #fix key length if shorter than text. | |
res = [] | |
for i,j in enumerate(text): | |
res.append(chr(ord(j)^ord(key[i]))) | |
text = "".join(res) |
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
text = "attack postponed untill two am".replace(' ','') | |
key = 4312567 | |
sliced = [ int(k) for k in str(key) ] | |
#a special function to calculate how many cells at least it would take to store the key and text into a matrix. | |
least_cells = lambda x=1: x*len(sliced) if x*len(sliced) >= len(text)+len(sliced) else least_cells(x+1) | |
rows = least_cells() // len(sliced) | |
matrix = [] |
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 numpy as np | |
text = "attack postponed untill two am".replace(' ','') | |
key = 4312567 | |
sliced = [ int(k) for k in str(key) ] | |
least_cells = lambda x=1: ( x,len(sliced) ) if x*len(sliced) >= len(text) else least_cells(x+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 format_duration(s): | |
dt = [] | |
for b, w in [(60, 'second'), (60, 'minute'), (24, 'hour'), (365, 'day'), (s+1, 'year')]: | |
s, m = divmod(s, b) | |
if m: dt.append('%d %s%s' % (m, w, 's' * (m > 1))) | |
return ' and '.join(', '.join(dt[::-1]).rsplit(', ', 1)) or 'now' |
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
board = "RBYYBBRRB" | |
hand = "YRBGB" | |
import re | |
def play(board=list(board+" "), hand=list(hand), trac=[], c=0): | |
try: | |
hand[c] | |
except: | |
trac.append(["".join(board[:-1]), c]) |
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 spiralNumbers(n): | |
m = [[0]*n for _ in range(n)] | |
dx, dy = [0,1,0,-1], [1,0,-1,0] | |
x, y, c = 0,-1,1 | |
for i in range(2*n-1): | |
for j in range((2*n-i)//2): | |
x += dx[i%4] | |
y += dy[i%4] | |
m[x][y] = c | |
c+=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 decompose(n): | |
def rec(n, i): | |
if n<0:return None | |
if n==0:return [] | |
for j in range(i-1,0,-1): | |
hold = rec(n-j**2, j) | |
if hold!=None:return hold+[j] | |
return rec(n**2, n) | |
print(decompose(11)) |