from string import ascii_uppercase as alpha import itertools as it def decrypt(enc_alpha, key_alpha): if alpha.index(enc_alpha) >= alpha.index(key_alpha): return alpha[alpha.index(enc_alpha) – alpha.index(key_alpha)] else: return alpha[len(alpha) + alpha.index(enc_alpha) – alpha.index(key_alpha)] def find_key(repeated): length = 1 while length <= len(repeated): check, iter_key =…
CheckIO: What does the cow say?
COW = r”’ \ ^__^ \ (oo)\_______ (__)\ )\/\ ||—-w | || || ”’ TOP = ‘_’ BOTTOM = ‘-‘ BORDERS = ‘/\\|’ MAX_ROW = 39 def make_rows(text): rows, num = [”], 0 words = text.split(‘ ‘) for i, w…
CheckIO: Numbers Factory
def checkio(number): ret = [] for i in range(9, 1, -1): while not number % i: number /= i ret.append(i) if number == 1: return int(”.join(map(str, sorted(ret)))) return 0 if __name__ == ‘__main__’: #These “asserts” using only for self-checking and…
CheckIO: Moria doors
import itertools def calc_coefficient(x, y): score = 0 if x[0] == y[0]: score += 10 if x[-1] == y[-1]: score += 10 score += (len(x) / len(y)) * 30 if len(x) <= len(y) else (len(y) / len(x)) * 30 score…
CheckIO: Open Labyrinth
class Node(object): def __init__(self, index, crd): self.index = index self.crd = crd self.links = None def __sub__(self, other): return ( self.crd[0] – other.crd[0], self.crd[1] – other.crd[1] ) def map_to_graph(maze_map): graph = [] def get_index(crd): for n in graph: if n.crd…
CheckIO: Weak Point
def weak_point(matrix): check = lambda x: [sum(y) for y in x] rows, cols = check(matrix), check(zip(*matrix)) return [rows.index(min(rows)), cols.index(min(cols))] if __name__ == ‘__main__’: #These “asserts” using only for self-checking and not necessary for auto-testing assert isinstance(weak_point([[1]]), (list, tuple)), “The result…
CheckIO: Brackets
import re def checkio(expression): pattern = ‘({}|\[\]|\(\))’ matcher = re.compile(pattern) brackets = ”.join(x for x in expression if x in ‘{}[]()’) while True: m = matcher.search(brackets) if not m: break brackets = brackets.replace(m.group(), ”) return False if brackets else True…
Python: Exchange rows and columns
# ABC # DEF # GHI >>> rows = [‘ABC’, ‘DEF’, ‘GHI’] >>> cols = [”.join(x) for x in zip(*rows)] >>> cols [‘ADG’, ‘BEH’, ‘CFI’]
Python: datetime.today()
datetime.today() does’t return today’s date only but today’s datetime. And using datetime.combine, date object can be converted to datetime. >>> from datetime import datetime, date # datetime.today() returns datetime not only date >>> datetime.today() datetime.datetime(2015, 6, 21, 15, 19, 33,…
Python: calculate weeks or months
from dateutil.relativedelta import relativedelta from datetime import datetime today = datetime.today() today.strftime(‘%Y-%m-%d’) # ‘2015-06-14’ next_week = today + relativedelta(weeks=1) next_week.strftime(‘%Y-%m-%d’) # ‘2015-06-21’ next_month = today + relativedelta(months=1) next_month.strftime(‘%Y-%m-%d’) # ‘2015-07-14’ from calendar import monthrange lastday_of_month = monthrange(2015, 6)[1] lastday_of_month #…