Notice
Recent Posts
Recent Comments
Link
«   2024/07   »
1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30 31
Tags
more
Archives
Today
Total
관리 메뉴

JINIers

Day 8 cipher 본문

공부/Python

Day 8 cipher

JINIers 2024. 6. 17. 11:03

cipher 1 

alphabet = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z','a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']

direction = input("Type 'encode' to encrypt, type 'decode' to decrypt:\n")
text = input("Type your message:\n").lower()
shift = int(input("Type the shift number:\n"))

#TODO-1: Create a function called 'encrypt' that takes the 'text' and 'shift' as inputs.
#TODO-1: 'text'와 'shift'를 입력으로 사용하는 'encrypt'라는 함수를 만듭니다.
def encrypt(plain_text, shift_amount):
    cipher_text = ""
    for letter in plain_text:
        position = alphabet.index(letter)
        new_position = position + shift_amount
        new_letter = alphabet[new_position]
        cipher_text += new_letter
    print(f"This : {cipher_text}")
        
    #TODO-2: Inside the 'encrypt' function, shift each letter of the 'text' forwards in the alphabet by the shift amount and print the encrypted text.
    #TODO-2: 'encrypt' 함수 내에서 'text'의 각 문자를 알파벳에서 이동량만큼 앞으로 이동하고 암호화된 텍스트를 인쇄합니다.
    #e.g. 
    #plain_text = "hello"
    #shift = 5
    #cipher_text = "mjqqt"
    #print output: "The encoded text is mjqqt"

    
    ##HINT: How do you get the index of an item in a list:
    # https://stackoverflow.com/questions/176918/finding-the-index-of-an-item-in-a-list

    ##🐛Bug alert: What happens if you try to encode the word 'civilization'?🐛

#TODO-3: Call the encrypt function and pass in the user inputs. You should be able to test the code and encrypt a message.
#TODO-3: 암호화 기능을 호출하고 사용자 입력을 전달합니다. 코드를 테스트하고 메시지를 암호화할 수 있어야 합니다.
encrypt(plain_text=text, shift_amount=shift)

 

 

# cipher 2

alphabet = [
    'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o',
    'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'a', 'b', 'c', 'd',
    'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's',
    't', 'u', 'v', 'w', 'x', 'y', 'z'
]

direction = input("Type 'encode' to encrypt, type 'decode' to decrypt:\n")
text = input("Type your message:\n").lower()
shift = int(input("Type the shift number:\n"))


def encrypt(plain_text, shift_amount):
    cipher_text = ""  # 빈 변수 정의
    for letter in plain_text:
        position = alphabet.index(letter)
        new_position = position + shift_amount
        cipher_text += alphabet[new_position]
    print(f"The encoded text is {cipher_text}")


#TODO-1: Create a different function called 'decrypt' that takes the 'text' and 'shift' as inputs.
#TODO-1: 'text'와 'shift'를 입력으로 사용하는 'decrypt'라는 다른 함수를 만듭니다.


#TODO-2: Inside the 'decrypt' function, shift each letter of the 'text' *backwards* in the alphabet by the shift amount and print the decrypted text.
#TODO-2: 'decrypt' 함수 내에서 'text'의 각 문자를 알파벳에서 *뒤로* 이동한 양만큼 이동하고 해독된 텍스트를 인쇄합니다.
#e.g.
#cipher_text = "mjqqt"
#shift = 5
#plain_text = "hello"
#print output: "The decoded text is hello"
def decrypt(cipher_text, shift_a):
    plain_text = ""  # 빈 변수 정의
    for i in cipher_text:
        de_position = alphabet.index(i)
        plain_position = de_position - shift_a
        decrypt_text = alphabet[plain_position]
        plain_text += decrypt_text
        # decrypt_text += alphabet[plain_position] 와 같다.
    print(f"The decoded text is {plain_text}")


#TODO-3: Check if the user wanted to encrypt or decrypt the message by checking the 'direction' variable. Then call the correct function based on that 'drection' variable. You should be able to test the code to encrypt *AND* decrypt a message.
#TODO-3: 'direction' 변수를 확인하여 사용자가 메시지를 암호화하거나 해독하기를 원했는지 확인하세요. 그런 다음 해당 '방향' 변수를 기반으로 올바른 함수를 호출하십시오. 메시지를 암호화하고 *복호화하는 코드를 테스트할 수 있어야 합니다.

if direction == "encode":
  encrypt(plain_text=text, shift_amount=shift)
elif direction == "decode":
  decrypt(cipher_text=text, shift_a=shift)

 

이건 다시 혼자 복습한거

cipher 1+2

alphabet = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z','a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']

direction = input("Type 'encode' to encrypt, type 'decode' to decrypt:\n")
text = input("Type your message:\n").lower()    # svt
shift = int(input("Type the shift number:\n"))  # 3

# TODO-1: 'text'와 'shift'를 입력으로 사용하는 'encrypt'라는 함수를 만듭니다.
# TODO-2: 'encrypt' 함수 내에서 'text'의 각 문자를 알파벳에서 이동량만큼 앞으로 이동하고 암호화된 텍스트를 인쇄합니다.
# TODO-3: 암호화 기능을 호출하고 사용자 입력을 전달합니다. 코드를 테스트하고 메시지를 암호화할 수 있어야 합니다.
def encrypt(pt, sh):    # def encrypt(svt, 3)
    encrypt_txt = ""
    for letter in pt:   # for letter in s = 27
        position = alphabet.index(letter)   # position = 27
        new_position = position + sh  # 30
        new_letter = alphabet[new_position]
        encrypt_txt += new_letter
    print(f"encrypt text : {encrypt_txt}")


#TODO-1: 'text'와 'shift'를 입력으로 사용하는 'decrypt'라는 다른 함수를 만듭니다.
#TODO-2: 'decrypt' 함수 내에서 'text'의 각 문자를 알파벳에서 *뒤로* 이동한 양만큼 이동하고 해독된 텍스트를 인쇄합니다.
#TODO-3: 'direction' 변수를 확인하여 사용자가 메시지를 암호화하거나 해독하기를 원했는지 확인하세요. 그런 다음 해당 '방향' 변수를 기반으로 올바른 함수를 호출하십시오. 메시지를 암호화하고 *복호화하는 코드를 테스트할 수 있어야 합니다.
def decrypt(entxt, desh):       #def decrypt(vyw, 3)
    decrypt_txt = ""
    for letter in entxt:    # for letter in vyw -> v
        position = alphabet.index(letter)  #alphabet.index(v) = 26
        new_position = position - desh  # 23
        new_letter = alphabet[new_position] # s
        decrypt_txt += new_letter
    print (f"decrypt_text : {decrypt_txt}")

if direction == "encode":
    encrypt(pt = text, sh = shift)  # pt = plain text = svt / sh = 3
elif direction == "decode":
    decrypt(entxt = text, desh = shift) # decrypt(entxt = vyw, desh = 3)
    # entxt = encrypt text / desh = decrypt shift

 

thonny가 진짜 많이 도움이 됐다

처음엔 뭔말인지 모름+이해안됨 콜라보로 안젤라가 작성한 코드를 토니 돌려서 이해했는데

이해하고나니 작성하기 훨씬 쉬웠다.

 

# cipher 3(내가 작성한 코드)

alphabet = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z','a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']

direction = input("Type 'encode' to encrypt, type 'decode' to decrypt:\n")
text = input("Type your message:\n").lower()    # svt
shift = int(input("Type the shift number:\n"))  # 3

# cipher 3
#TODO-1: encrypt() 및 decrypt() 함수를 caesar()라는 단일 함수로 결합합니다.
#TODO-2: 'text', 'shift' 및 'direction' 값을 전달하여 Caesar() 함수를 호출합니다.
def caesar(txt, sh, dr):
    if dr == "encode":
        encrypt_txt = ""
        for letter in txt:
            position = alphabet.index(letter)
            new_position = position + sh
            new_letter = alphabet[new_position]
            encrypt_txt += new_letter
        print(f"encrypt text : {encrypt_txt}")
    elif dr == "decode":
        decrypt_txt = ""
        for letter in txt:
            position = alphabet.index(letter)
            new_position = position - sh
            new_letter = alphabet[new_position]
            decrypt_txt += new_letter
        print (f"decrypt_text : {decrypt_txt}")

caesar(txt = text, sh = shift, dr = direction)

 

# 안젤라가 작성한 코드

alphabet = [
    'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o',
    'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'a', 'b', 'c', 'd',
    'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's',
    't', 'u', 'v', 'w', 'x', 'y', 'z'
]

direction = input("Type 'encode' to encrypt, type 'decode' to decrypt:\n")
text = input("Type your message:\n").lower()
shift = int(input("Type the shift number:\n"))


#TODO-1: Combine the encrypt() and decrypt() functions into a single function called caesar().
#TODO-1: encrypt() 및 decrypt() 함수를 caesar()라는 단일 함수로 결합합니다.

def caesar(start_text, shift_amount, cipher_direction):
    end_text = ""
    if cipher_direction == "decode":
        shift_amount *= -1
    for letter in start_text:
        position = alphabet.index(letter)
        new_position = position + shift_amount
        end_text += alphabet[new_position]
    print(f"The {cipher_direction}d text is {end_text}")




#TODO-2: Call the caesar() function, passing over the 'text', 'shift' and 'direction' values.
#TODO-2: 'text', 'shift' 및 'direction' 값을 전달하여 Caesar() 함수를 호출합니다.
caesar(start_text=text, shift_amount=shift, cipher_direction=direction)

 

안젤라가 작성한 코드를 토대로 간단하게 재작성

def caesar(txt, sh, dr):
    end_txt = ""
    if dr == "decode":
        sh = sh * -1
    for letter in txt:
        position = alphabet.index(letter)
        new_position = position + sh
        new_letter = alphabet[new_position]
        end_txt += new_letter
    print(f"{dr} → {end_txt}")

 

 

# cipher 4

alphabet = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z','a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']

# cipher 4

for art import logo    #todo-1
def caesar(txt, sh, dr):
    end_txt = ""
    if dr == "decode":
        sh = sh * -1
    # todo-3
    for char in txt:
        if char in alphabet:
            position = alphabet.index(char)
            new_position = position + sh
            new_char = alphabet[new_position]
            end_txt += new_char
        else:
            end_txt += char
    print(f"{dr} → {end_txt}")


restart = True
while restart:
    direction = input("Type 'encode' to encrypt, type 'decode' to decrypt:\n")
    text = input("Type your message:\n").lower()
    shift = int(input("Type the shift number:\n"))
    shift = shift % 26  #todo-2
    caesar(txt = text, sh = shift, dr = direction)

    #todo-4
    result = input("again = 'yes' or not = 'no'.\n")
    if result == 'no':
        restart = False
        print("Goodbye~!")

 

encode test

 

decode & end test

'공부 > Python' 카테고리의 다른 글

Day 9  (0) 2024.06.19
Day 8 def  (0) 2024.06.17
[Python 100 day challenge ] Day 6 - 허들  (1) 2024.04.20
[Python 100 day challenge ] Day 5 - for  (0) 2024.03.27
[Python 100 day challenge ] Day 4  (0) 2024.03.27
Comments