def takeAction(self, action): newState = deepcopy(self) newBoard = rle.decode(newState.board, (10, 8)) newBoard = quark.play(newBoard, action.x, action.y, action.player) newState.currentPlayer = self.currentPlayer * -1 newState.board = rle.encode(newBoard) return newState
def pic_to_file(self, fh): pic = self.pic data = pack("HHB", len(pic[0]), len(pic), 11) pic_data = [] for ln in pic: pic_data += ln pic_data = rle.encode(pic_data) pic_data = lzw.compress(pic_data) pic_data = bytearray(pic_data) hdr_data = pack('HH', 0x3058, len(data) + len(pic_data)) fh.write(hdr_data) fh.write(data) fh.write(pic_data)
def compress(path, file_number): with open(path, 'rb') as file: content_file = str(file.read(), 'utf-8') values, counts = rle.encode(content_file) values_string = "[" for value in values[:-1]: values_string = values_string + "\'" + value + "\'" + "," else: values_string = values_string + "\'" + value + "\'" values_string = values_string + "]" counts_string = "[" for count in counts[:-1]: counts_string = counts_string + "\'" + str(count) + "\'" + "," else: counts_string = counts_string + "\'" + str(count) + "\'" counts_string = counts_string + "]" string_compress = values_string + "," + counts_string f = open("rle/" + file_number + ".txt", 'w') f.write(string_compress) f.close()
def test_symbol(): assert encode('virker ikke') == '1v1i1r1k1e1r1 1i2k1e'
def test_digit(): assert encode('www111111qqq') == '3w613q'
def encode(text): values, counts = rle.encode(text) result = "".join(f"{x[0]}{x[1]}" for x in zip(values, counts)) result = result.replace("1", "") return result
def test_rle(): assert encode('kkk') == [(3, 'k')]
def test_space(): assert encode('kkk ooo') == '3k3 3o'
def test_digit(): assert encode('www111111qqq') == [(3, 'w'), (6, '1'), (3, 'q')]
def test_int(): with pytest.raises(MyException) as exc_info: encode(123)
def test_empty(): assert encode('') == []
def test_None(): with pytest.raises(MyException) as info: encode(None) == ''
def test_empty(): assert encode('') == "error, empty input"
def test_encode(): assert encode('aa') == '2a' assert encode('wwwwwbbb') == '5w3b'
def test_rle2(): assert encode('kkkkkkoooo') == '6k4o'
def test_encoder(self): self.assertEqual("10ab10c",encode("aaaaaaaaaabcccccccccc"))
def test_single_encode(self): self.assertEqual("a",encode("a"))
for i in compressed: parseo = str(i) arch.write(parseo) arch.close() aux1 = time.perf_counter() decompressed = decompress(compressed) aux2 = time.perf_counter() tiempo = aux2 - aux1 print(tiempo) print(decompressed) elif opcion == 2: w = [] archivo = open(path) contenido = archivo.read(128) aux1 = time.perf_counter() cod = rle.encode(contenido) aux2 = time.perf_counter() tiempo = aux2 - aux1 print(tiempo) print(cod) archivo.close() arch = open("./comprimido.txt", "w") for i in cod: parseo = str(i) arch.write(parseo) arch.close() aux1 = time.perf_counter() decod = rle.decode(cod[0], cod[1]) aux2 = time.perf_counter() tiempo = aux2 - aux1 print(tiempo)
def test_escape(): assert encode('\\' * 4) == '4\\'
def test_encode2(): assert encode('kkkkkkoooo') == [(6, 'k'), (4, 'o')]
def test_number(): assert encode('3777') == '1337'
def test_emoji(): assert encode('😇😇😇😇😇😇😇😇😍😍😍😍😍😍😍') == [(8, '😇'), (7, '😍')]
def test_hypo(x): print(x) decode(encode(x)) == x
def test_space(): assert encode('kkk ooo') == [(3, 'k'), (3, ' '), (3, 'o')]
def test_rle(): assert encode('kkk') == '3k'
def test_escape(): assert encode('\\' * 4) == [(4, '\\')]
def all_chains(run, choices=[1, 2, 3]): steps = len(run) rnge = max(run) - min(run) combos = [list(it.combinations(choices, r=i)) for i in range(1, steps)] combos = [x for sublist in combos for x in sublist] print(combos) possibles = list(filter(lambda sub: sum(sub) % rnge == 0, combos)) return possibles diffs = np.diff(nums, axis=0) MAX_DIFF = 3 cheat_dict = {1: 1, 2: 2, 3: 4, 4: 7} streaks = rle.encode(diffs) runs = [streaks[1][i] for i in range(len(streaks[0])) if streaks[0][i] != 3] permutes = [cheat_dict.get(num) for num in runs] ans2 = ft.reduce(lambda x, y: x * y, permutes) print(ans2) # <codecell> # <codecell Day 14> import re import numpy as np import functools as ft import itertools as it import collections as col
def test_encode_decode(s): #s = '2222aaaaaaaaafffffuuuu9999' print(s) assert decode(encode(s)) == s
def __init__(self): self.board = rle.encode(np.zeros((10, 8), np.int8)) self.currentPlayer = 1
def test_encode(): assert encode('aa') == '2a' assert encode('kkkbbb') == '3k3b' assert encode('') == ''
def test_emoji(): assert encode('😇😇😇😇😇😇😇😇😍😍😍😍😍😍😍') == '8😇7😍'