목록Python/고전 컴퓨터 알고리즘 (6)
일상 코딩
maze.py from enum import Enum from typing import List, NamedTuple, Callable, Optional import random from math import sqrt from urllib.parse import MAX_CACHE_SIZE from generic_search import dfs, node_to_path, Node #, bfs, astar class Cell(str, Enum): EMPTY = " " BLOCKED = "X" START = "S" GOAL = "G" PATH = "*" class MazeLocation(NamedTuple): row: int column: int class Maze: def __init__(self, rows..

from enum import Enum from typing import List, NamedTuple, Callable, Optional import random from math import sqrt from urllib.parse import MAX_CACHE_SIZE # from generic_search import dfs, bfs, node_to_path, astar, Node class Cell(str, Enum): EMPTY = " " BLOCKED = "X" START = "S" GOAL = "G" PATH = "*" class MazeLocation(NamedTuple): row: int column: int class Maze: def __init__(self, rows: int = ..
class CompressedGene: def __init__(self, gene: str) -> None: self._compress(gene) # "_"은 클래스 외부에서 사용되지 않게 하기 위한 비공개 처리 문구이다. def _compress(self, gene: str) -> None: self.bit_string: int = 1 # 1로 시작/ 유전코드를 2진수로 변환 후 저장할 변수. for nucleotide in gene.upper(): self.bit_string > i & 0b11 # 마지막 2비트를 추출한다. if bits == 0b00: # A gene += "A" elif bits == 0b01: # C gene += "C" elif bits == 0b10: # G gene += ..
암호화 전체 코드는 아래와 같다. from secrets import token_bytes from typing import Tuple def random_key( length: int ) -> int: # length 만큼 임의의 바이트를 생성한다. tb: bytes = token_bytes(length) # token_byte() 메서드 사용하여 pseudo-random 데이터 생성한다. # 바이트를 비트 문자열로 변환한 후 반환한다. return int.from_bytes(tb,"big") # 암호화 과정 메소드 def encrypt( original: str ) -> Tuple[int, int]: original_bytes: bytes = original.encode() # 문자열을 encode(..

위의 식은 파이 계산을 위한 무한급수 라이프 니츠 공식(Leibniz Formula)이다. 이를 무한히 더하면 파이값을 가질 수 있게 된다. 이를 코드로 나타내면 import numpy as np import time def calculate_pi(n_terms:int) -> float: upNum: float = 4.0 downNum: float = 1.0 multiNum: float = 1.0 pi: float = 0.0 start = time.time() for _ in range(n_terms): pi += multiNum * ( upNum / downNum ) downNum += 2.0 multiNum *= -1.0 end = time.time() return pi, end-start if __..
part.00 메모이제이션을 이용한 피보나치 알고리즘 n = int(input()) memo = {0: 0, 1: 1} def fibo(n): if n in memo: return memo[n] else: ret = fibo(n-1) + fibo(n-2) memo[n] = ret return ret print(fibo(n)) 일반적인 리스트를 이용하는 것이 아닌, dictionary()를 이용하여 key값으로 value를 빠르게 찾도록 만들었다. part.01 아래 코드는 해보나치 수열 단일값을 출력하는 코드이다. from functools import lru_cache @lru_cache( maxsize = None ) def fib4( n: int ) -> int: if n < 2: # 기저조건 re..