I'm trying to make a cipher based on Ceaser Cipher, which instead of letters, I used numbers. for example, 1 = 'a', 2 = 'b' and so on.
This is the full code:
import string
dict = {}
message = input("Enter a message\n")
key = input("Enter a key\n")
encrypted = ""
for i, char in enumerate(string.ascii_lowercase):
#key is from 0 to 25
dict[i] = char
print(dict)
for val in message:
if val in dict:
encrypted = dict[val]
for key, value in dict.items():
if val == value:
encrypted = str(key 1)
encrypted = " "
print(encrypted)
in print(dict)
, it prints the value of the key
For example:
{0: 'a', 1: 'b', 2: 'c', 3: 'd', 4: 'e', 5: 'f', 6: 'g', 7: 'h'}
What I want to do is to change the value based on the key input
For example:
key = 'D3'
My desired output:
{0: 'd', 1: 'e', 2: 'f', 3: 'a', 4: 'b', 5: 'c', 6: 'g', 7: 'h'}
Is there any solution for this?
CodePudding user response:
I assume 'd3' means starting from d you are switching 3 values to the left side. If that's the case, 'd2' will return the following output:{0: 'd', 1: 'e', 2: 'c', 3: 'a', 4: 'b', 5: 'f', 6: 'g', 7: 'h'}
See if this code will work, I wrote a loop and it allows you to customize your key every time. d
is the initial dictionary and k
is the 'D3' key.
def swipe(d,k):
start = k[0].lower()
num = int(k[1])
count = 0
for i in d:
if d[i] == start:
break
else:
count =1
for i in range(count,count num):
d[i-count], d[i] = d[i],d[i-count]
return d
CodePudding user response:
This solution works only for the first time that the value_of_interest is found within the dict. In your case example - that s not the case, but I am not sure whether a more generalized approach of your cipher (I don t really know what exactly this is) ought to have this considered beforehand.
my_dict={0: 'a', 1: 'b', 2: 'c', 3: 'd', 4: 'e', 5: 'f', 6: 'g', 7: 'h'}
print(f'Init dict: {my_dict}')
key_list=list(my_dict.keys())
#let's say ... (please make a more generalized parsing method for swaper)
swaper='D3'
print(f'Input swaper: {swaper}')
value_of_interest=swaper[0].lower()
shift_=int(swaper[1])
new_dict={}
for counter,key in enumerate(key_list):
if my_dict[key]==value_of_interest :
if len(key_list)>=counter shift_: #Added a method for checking whether there are enough keys within the dict - after the D value is found.
new_dict = {i:my_dict[key] for i,key in enumerate(key_list[counter:counter shift_])}
for i,key in enumerate(key_list[0:counter]):
new_dict[shift_ i]=my_dict[key]
for key in key_list[counter shift_:]:
new_dict[key]=my_dict[key]
else:
print(f'There are not more than {shift_} keys following the {value_of_interest} value')
print(f'Output dict: {new_dict}')
Output:
Init dict: {0: 'a', 1: 'b', 2: 'c', 3: 'd', 4: 'e', 5: 'f', 6: 'g', 7: 'h'}
Input swaper: D3
Output dict: {0: 'd', 1: 'e', 2: 'f', 3: 'a', 4: 'b', 5: 'c', 6: 'g', 7: 'h'}
In another case for example, Given the value of "B5" in the swaper variable, it gives:
Init dict: {0: 'a', 1: 'b', 2: 'c', 3: 'd', 4: 'e', 5: 'f', 6: 'g', 7: 'h'}
Input swaper: B5
Output dict: {0: 'b', 1: 'c', 2: 'd', 3: 'e', 4: 'f', 5: 'a', 6: 'g', 7: 'h'}