Brute Force

Introduction

무차별 대입 공격

intertools module

product

from itertools import product
list = product(A, repeeat=B)

A는 조합을 만들 내용 입력, B는 자릿수 입력

  • 여러 리스트들의 조합을 만들기 위해 사용
  • 문자열을 입력할 경우, 문자열 원소 하나하나를 인식하여 조합의 재료로 사용

데카르트곱

from itertools import product

iterator1 = [1, 2, 3]
iterator2 = ['A', 'B', 'C']

print(list(product(iterator1, iterator2)))

>>> [(1, 'A'), (1, 'B'), (1, 'C'), (2, 'A'), (2, 'B'), (2, 'C'), (3, 'A'), (3, 'B'), (3, 'C')]

print([(i,j) for i in iterator1 for j in iterator2])

>>> [(1, 'A'), (1, 'B'), (1, 'C'), (2, 'A'), (2, 'B'), (2, 'C'), (3, 'A'), (3, 'B'), (3, 'C')]

중복 순열(순서O, 중복O)

from itertools import product

iterator = ['A','B','C','D','E']

print(list(product(iterator, repeat = 1)))

>>> [('A',), ('B',), ('C',), ('D',), ('E',)]

print(list(product(iterator, repeat = 2)))

>>> [('A', 'A'), ('A', 'B'), ('A', 'C'), ('A', 'D'), ('A', 'E'), ('B', 'A'), ('B', 'B'), ('B', 'C'), ('B', 'D'), ('B', 'E'), ('C', 'A'), ('C', 'B'), ('C', 'C'), ('C', 'D'), ('C', 'E'), ('D', 'A'), ('D', 'B'), ('D', 'C'), ('D', 'D'), ('D', 'E'), ('E', 'A'), ('E', 'B'), ('E', 'C'), ('E', 'D'), ('E', 'E')]

permutations (순서O, 중복X) - 순열

from itertools import permutations

iterator = ['A','B','C']

print(list(permutations(iterator)))

>>> [('A', 'B', 'C'), ('A', 'C', 'B'), ('B', 'A', 'C'), ('B', 'C', 'A'), ('C', 'A', 'B'), ('C', 'B', 'A')]

print(list(permutations(iterator, 2)))

>>> [('A', 'B'), ('A', 'C'), ('B', 'A'), ('B', 'C'), ('C', 'A'), ('C', 'B')]

combinations (순서X, 중복X) - 조합

from itertools import combinations

iterator = ['A','B','C']

print(list(combinations(iterator, len(iterator))))

>>> [('A', 'B', 'C')]

print(list(combinations(iterator, 2)))

>>> [('A', 'B'), ('A', 'C'), ('B', 'C')]
import time
from itertools import product

password = "world1"
number = "0123456789"
lowercase = "abcdefghijklmnopqrstuvwxyz"
uppercase = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
symbol = "!@#$%^&*()_+-=`~"
possibility = lowercase + number
attempt = product(possibility, repeat=len(password))
start = time.time()

def run():
    for i in attempt:
        if "".join(i) == password:
            print("비밀번호 : " + str("".join(i)))
            print("소요시간 : " + str(time.time() - start))

run()

재귀

반복적인 패턴 찾아내기 -> 점화식 찾기

def combination(arr,r):

    wanted=[]
    
    if r==1:
        for i in arr:
            wanted.append([i])

    else:
        for i in range(len(arr)-r+1):
            for j in combination(arr[i+1:], r-1):
                wanted.append([arr[i]]+j)

    return wanted

Example

https://www.acmicpc.net/step/22

블랙잭

from itertools import combinations

N, M = map(int, input().split(' '))
cards = list(map(int, input().split(' ')))

output = 0

for card in combinations(cards, 3):
    val = card[0]+card[1]+card[2]
    if M >= val and output < val:
        output = val

print(output)

분배합

num = int(input())

output = 0
for n in range(num):
    sum = 0
    for i in str(n):
        sum += int(i)
    if n+sum == num:
        output=n
        break

print(output)

덩치

N = int(input())
people = []
for i in range(N):
    people.append(list(map(int,input().split(' '))))

output = []
for p in people:
    k=1
    for other in people:
        if p[0]<other[0] and p[1]<other[1]:
            k+=1
    output.append(k)

result = ''
for i in output:
    result = result + str(i) + ' '

print(result[:-1])

체스판 다시 칠하기

영화감독 숌

2022

Stack

1 minute read

Introduction

Stack

less than 1 minute read

Introduction

Download-only-one-directory

less than 1 minute read

Git 명령어를 사용한 하위 디렉토리 다운로드 Clone 할 로컬 저장소 생성

Sort

3 minute read

Introduction

Tree

less than 1 minute read

Introduction

Mutex library on C++

less than 1 minute read

#include <iostream> #include <thread> #include <chrono> #include <mutex> #include <atomic> #include <string.h>

TODO

less than 1 minute read

how to costom github blog using jekyll

Welcome to Jekyll!

less than 1 minute read

You’ll find this post in your _posts directory. Go ahead and edit it and re-build the site to see your changes. You can rebuild the site in many different wa...

Back to top ↑