-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTasks.py
More file actions
63 lines (46 loc) · 2.02 KB
/
Tasks.py
File metadata and controls
63 lines (46 loc) · 2.02 KB
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# 1
def to_base10(binary):
""""Function that take a binary number (base 2), convert it into a
decimal number (base 10)
Args:
binary(int):represents the binary number to be converted
Returns:
int: the number converted to decimal
"""
reversal = str(binary)[::-1]
# reverses binary number backwards.The power of each number is the
# same with the index when reversed.
output = 0
for i in range(len(reversal)):
output += (int(reversal[i])*(2**i))
# the individual value of each number is multiplied by the base
# 2 raised to power of the index.
return output
print(to_base10(11111111))
def encrypt(message, shifts, alphabet):
""""Encrypts a plain text message using an improved Caesar cipher encryption method.
Args:
- message (str): The plain text message to be encrypted.
- shifts (list): A list of n positive integers representing the shift values for each character in the message.
- alphabet (str): A string representing the alphabet used for encryption. Can be any sequence of symbols.
Returns:
- str: The encrypted message.
"""
encryption = ""
for (letter, shift_value) in zip(message, shifts):
# to check if size of the shifts is the same as the size of the
# message
if len(shifts) != len(message):
return None
if shift_value < 0 or shift_value > len(alphabet):
# to ensure that every individual shift value is not more
# than the length of the alphabet or a negative number
return None
if letter in alphabet:
sub_index = (alphabet.index(letter)+shift_value) % len(alphabet)
elif letter not in alphabet:
# to check that every letter in the message is in the alphabet
return None
encryption += alphabet[sub_index]
return encryption
# print(encrypt(encryption_info, shift_info, alphabet_info))