Skip to content

Commit b57e82c

Browse files
authored
day 8 files upload
1 parent eb435c0 commit b57e82c

10 files changed

+249
-0
lines changed

Day08/Caesar Cipher - step1.py

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# step 1
2+
# ----------------------------------------------
3+
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'
5+
]
6+
7+
direction = input("Type 'encode' to encrypt, type 'decode' to decrypt:\n")
8+
text = input("Type your message:\n").lower()
9+
shift = int(input("Type the shift number:\n"))
10+
11+
12+
#TODO-1: Create a function called 'encrypt' that takes the 'text' and 'shift' as inputs.
13+
def encrypt(plain_text, shift_amount):
14+
#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.
15+
#e.g.
16+
#plain_text = "hello"
17+
#shift = 5
18+
#cipher_text = "mjqqt"
19+
#print output: "The encoded text is mjqqt"
20+
cipher_text = ""
21+
for eachletter in plain_text:
22+
index_after_shift = alphabet.index(eachletter) + shift_amount
23+
cipher_text += alphabet[index_after_shift]
24+
print(f"The encoded text is {cipher_text}")
25+
26+
27+
##HINT: How do you get the index of an item in a list:
28+
#https://stackoverflow.com/questions/176918/finding-the-index-of-an-item-in-a-list
29+
30+
##🐛Bug alert: What happens if you try to encode the word 'civilization'?🐛
31+
# solve by copy pasting a to z once more
32+
# or by using, if index_after_shift>25 then, new_index = index_after_shift % 25
33+
34+
#TODO-3: Call the encrypt function and pass in the user inputs. You should be able to test the code and encrypt a message.
35+
encrypt(plain_text=text, shift_amount=shift)

Day08/Caesar Cipher - step2.py

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# step 2
2+
# ----------------------------------------------
3+
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'
5+
]
6+
7+
direction = input("Type 'encode' to encrypt, type 'decode' to decrypt:\n")
8+
9+
if (direction == 'encode') or (direction == 'decode'):
10+
text = input("Type your message:\n").lower()
11+
shift = int(input("Type the shift number:\n"))
12+
13+
def encrypt(plain_text, shift_amount):
14+
cipher_text = ""
15+
for eachletter in plain_text:
16+
index_after_shift = alphabet.index(eachletter) + shift_amount
17+
cipher_text += alphabet[index_after_shift]
18+
print(f"The encoded text is {cipher_text}")
19+
#TODO-1: Create a different function called 'decrypt' that takes the 'text' and 'shift' as inputs.
20+
def decrypt(cipher_text, shift_amount):
21+
#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.
22+
decoded_text = ""
23+
for eachletter in cipher_text:
24+
index_after_shift = alphabet.index(eachletter) - shift_amount
25+
decoded_text += alphabet[index_after_shift]
26+
print(f"The decoded text is {decoded_text}")
27+
28+
#e.g.
29+
#cipher_text = "mjqqt"
30+
#shift = 5
31+
#plain_text = "hello"
32+
#print output: "The decoded text is hello"
33+
34+
35+
#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.
36+
if direction == 'encode':
37+
encrypt(plain_text=text, shift_amount=shift)
38+
else:
39+
decrypt(cipher_text = text, shift_amount = shift)
40+
41+
else:
42+
print("Wrong command.")
43+

Day08/Caesar Cipher - step3.py

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# step 3
2+
# ----------------------------------------------
3+
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']
5+
6+
direction = input("Type 'encode' to encrypt, type 'decode' to decrypt:\n")
7+
8+
if (direction == 'encode') or (direction == 'decode'):
9+
text = input("Type your message:\n").lower()
10+
shift = int(input("Type the shift number:\n"))
11+
12+
#TODO-1: Combine the encrypt() and decrypt() functions into a single function called caesar().
13+
def caesar(start_text, shift_amount, cipher_direction):
14+
end_text = ""
15+
16+
if cipher_direction == 'decode':
17+
shift_amount *= -1 # dhyaan se
18+
19+
for eachletter in start_text:
20+
index = alphabet.index(eachletter)
21+
# if cipher_direction == 'decode':
22+
# shift_amount *= -1 # yaha nahi rakh sakte
23+
index_after_shift = index + shift_amount
24+
end_text += alphabet[index_after_shift]
25+
print(f"The {cipher_direction}d text is {end_text}")
26+
27+
28+
#TODO-2: Call the caesar() function, passing over the 'text', 'shift' and 'direction' values.
29+
caesar(start_text = text, shift_amount = shift, cipher_direction = direction)
30+
31+
else:
32+
print("Wrong command.")

Day08/Caesar Cipher - step4.py

+58
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
2+
# step 4
3+
# ----------------------------------------------
4+
5+
alphabet = [
6+
'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o',
7+
'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'a', 'b', 'c', 'd',
8+
'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's',
9+
't', 'u', 'v', 'w', 'x', 'y', 'z'
10+
]
11+
12+
#TODO-1: Import and print the logo from art.py when the program starts.
13+
from art import logo
14+
print(logo)
15+
16+
def caesar(start_text, shift_amount, cipher_direction):
17+
end_text = ""
18+
if cipher_direction == 'decode':
19+
shift_amount *= -1 # dhyaan se
20+
21+
for eachletter in start_text:
22+
#TODO-3: What happens if the user enters a number/symbol/space?
23+
#Can you fix the code to keep the number/symbol/space when the text is encoded/decoded?
24+
#e.g. start_text = "meet me at 3"
25+
#end_text = "•••• •• •• 3"
26+
if eachletter not in alphabet:
27+
end_text += eachletter
28+
else:
29+
index = alphabet.index(eachletter)
30+
index_after_shift = index + shift_amount
31+
end_text += alphabet[index_after_shift]
32+
33+
print(f"The {cipher_direction}d text is: {end_text}")
34+
35+
36+
#TODO-4: Can you figure out a way to ask the user if they want to restart the cipher program?
37+
#e.g. Type 'yes' if you want to go again. Otherwise type 'no'.
38+
#If they type 'yes' then ask them for the direction/text/shift again and call the caesar() function again?
39+
#Hint: Try creating a while loop that continues to execute the program if the user types 'yes'.
40+
should_run = True
41+
while should_run:
42+
direction = input("Type 'encode' to encrypt, type 'decode' to decrypt:\n")
43+
if (direction == 'encode') or (direction == 'decode'):
44+
text = input("Type your message:\n").lower()
45+
shift = int(input("Type the shift number:\n"))
46+
47+
#TODO-2: What if the user enters a shift that is greater than the number of letters in the alphabet?
48+
#Try running the program and entering a shift number of 45.
49+
#Add some code so that the program continues to work even if the user enters a shift number greater than 26.
50+
#Hint: Think about how you can use the modulus (%).
51+
shift %= 26
52+
caesar(start_text=text, shift_amount=shift, cipher_direction=direction)
53+
result = input("\nType 'yes' if you want to go again. Otherwise type 'no'\n")
54+
if result == 'no':
55+
should_run = False
56+
print("Goodbye!")
57+
else:
58+
print("Wrong command.Try again\n")
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
'''
2+
You are painting a wall.
3+
The instructions on the paint can says that 1 can of paint can cover 5 square meters of wall.
4+
Given a random height and width of wall, calculate how many cans of paint you'll need to buy.
5+
6+
number of cans = (wall height x wall width) ÷ coverage per can.
7+
8+
e.g. Height = 2, Width = 4, Coverage = 5
9+
10+
number of cans = (2 * 4) / 5
11+
12+
= 1.6
13+
14+
But because you can't buy 0.6 of a can of paint, the result should be rounded up to 2 cans.
15+
16+
Example Input
17+
test_h = 3
18+
test_w = 9
19+
Example Output
20+
You'll need 6 cans of paint.
21+
'''
22+
23+
import math
24+
def paint_calc(height, width, cover):
25+
noc = (height * width) / cover
26+
rounded_noc = math.ceil(noc)
27+
print(f"You'll need {rounded_noc} cans of paint")
28+
29+
#Write your code above this line 👆
30+
# Define a function called paint_calc() so that the code below works.
31+
32+
# 🚨 Don't change the code below 👇
33+
test_h = int(input("Height of wall: "))
34+
test_w = int(input("Width of wall: "))
35+
coverage = 5
36+
paint_calc(height=test_h, width=test_w, cover=coverage)
37+

Day08/Exercise 2 - Prime Numbers.py

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#You need to write a function that checks whether if the number passed into it is a prime number or not.
2+
3+
#Write your code below this line 👇
4+
def prime_checker(number):
5+
isPrime = True
6+
if number < 1:
7+
print("Enter positive number greater than 1")
8+
if (number == 1) or (number == 2):
9+
print("It's a prime number.")
10+
else:
11+
for i in range(2,number):
12+
if number % i == 0:
13+
isPrime = False
14+
if isPrime == True:
15+
print("It's a prime number.")
16+
else:
17+
print("It's not a prime number.")
18+
19+
20+
#Write your code above this line 👆
21+
22+
#Do NOT change any of the code below👇
23+
n = int(input("Check this number: "))
24+
prime_checker(number=n)

Day08/art.py

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
logo = """
2+
$$$$$$$\ $$$$$$\ $$$$$$\ $$$$$$$\ $$$$$$\ $$$$$$\
3+
$$ _____|\____$$\ $$ __$$\ $$ _____| \____$$\ $$ __$$\
4+
$$ / $$$$$$$ |$$$$$$$$ |\$$$$$$\ $$$$$$$ |$$ | \__|
5+
$$ | $$ __$$ |$$ ____| \____$$\ $$ __$$ |$$ |
6+
\$$$$$$$\\$$$$$$$ |\$$$$$$$\ $$$$$$$ |\$$$$$$$ |$$ |
7+
\_______|\_______| \_______|\_______/ \_______|\__|
8+
9+
$$\ $$\
10+
\__| $$ |
11+
$$$$$$$\ $$\ $$$$$$\ $$$$$$$\ $$$$$$\ $$$$$$\
12+
$$ _____|$$ |$$ __$$\ $$ __$$\ $$ __$$\ $$ __$$\
13+
$$ / $$ |$$ / $$ |$$ | $$ |$$$$$$$$ |$$ | \__|
14+
$$ | $$ |$$ | $$ |$$ | $$ |$$ ____|$$ |
15+
\$$$$$$$\ $$ |$$$$$$$ |$$ | $$ |\$$$$$$$\ $$ |
16+
\_______|\__|$$ ____/ \__| \__| \_______|\__|
17+
$$ |
18+
$$ |
19+
\__|
20+
"""

Day08/cs.gif

210 KB
Loading

Day08/cs.mp4

9.05 MB
Binary file not shown.

Day08/thumbnail.jpg

58.9 KB
Loading

0 commit comments

Comments
 (0)