adds a rot with key example

This commit is contained in:
waldek 2022-04-11 22:22:31 +02:00
parent 698436b1bb
commit f22248e104
1 changed files with 51 additions and 0 deletions

View File

@ -1624,6 +1624,51 @@ The third letter by ROT2 so will become `n`.
Now, the fourth letter will be offset again by `a` so ROT0 and will become `l`.
And so on...
<details>
<summary>Spoiler warning</summary>
```python
import string
def encrypt_message(msg, rot=13):
encoded_message = []
for letter in msg:
if letter in alphabet:
letter_index = alphabet.index(letter)
encoded_letter_index = (letter_index + rot) % 26
encoded_letter = alphabet[encoded_letter_index]
encoded_message.append(encoded_letter)
else:
encoded_message.append(letter)
encoded_message = "".join(encoded_message)
return encoded_message
def encrypt_with_key(msg, key):
encrypted_message = []
counter = 0
for letter in msg:
key_letter = key[counter % len(key)]
key_index = alphabet.index(key_letter)
encrypted_letter = encrypt_message(letter, key_index)
counter += 1
encrypted_message.append(encrypted_letter)
return "".join(encrypted_message)
if __name__ == "__main__":
alphabet = list(string.ascii_lowercase)
key = "mehdi"
print("hello, I'm an encryption program!")
result = input("what is your secret? ")
secret = encrypt_with_key(result, key)
print("your secret message is: {}".format(secret))
```
</details>
# Coding challenge - Christmas Tree
Can you make me a program that draws various size Christmas trees?
@ -1834,6 +1879,12 @@ For some inspiration for a simple question/response system you can have a look [
# Dictionaries as data containers
## Two dimensional lists
TODO - Currency converter with lists.
## Dictionaries
Of the *built-in* types we first say `str`, `int` and `float`.
Next we saw *sequence* types such as `list` and `tupple`.
Now we'll dive into a **mapping type** called `dict`.