For instance, the message ‘Save our Soul’ would be encrypted as: "Sv u oliaeorSu" and then I want to decrypt "Sv u oliaeorSu" to its original ones. I had performed the encryption but the decryption looks difficult. And at the end I want to concatenate items of both list into a new one.
for example lst1=[a,c,e,g]; lst2=[b,d,f]
the list I want is lst3=[a,b,c,d,e,f,g]
str1=input("Enter the String ??")
mystr=""
list1=[]
list2=[]
for i in range(len(str1)):
if i%2==0:
list1.append(str1[i])
else:
list2.append(str1[i])
list1.append("i")
encrypted="".join(map(str,list1 list2))
print("Encryption :",encrypted)
# (b) Decryption
list3=[];list4=[]
dec1=[];dec2=[]
length=len(list1 list2)
for i in range(length):
list3.append(encrypted[i])
else:
list4.append(encrypted[i])
if "i" in (list3):
list3.pop(list3.index("i"))
print(*list3)
len1=len(list3)
if len1%2==0:
for i in range(len1//2):
dec1.append(list3[i])
dec2.append(list3[i len1//2])
print(dec1)
print(dec2)
decX=[]
i=0
while(i<len(dec1)):
print(dec1[i],end="")
print(dec2[i],end="")
i =1
else:
for i in range(len1//2 1):
dec1.append(list3[i])
dec2.append(list3[i len1//2])
if len1%2!=0:
dec2.pop(0)
print(dec1)
print(dec2)
i=0
while(i<len(dec1)):
print(dec1[i],end="")
if len(dec2) != len(dec1):
i =1
print(dec2[i-1],end="")
i =1
i-=1
Output
Enter the String ??1234567
Traceback (most recent call last):
File "C:/Users/SHUBHAM/PycharmProjects/Hello World/Basic Programs/Ass 4.py", line 460, in <module>
Encryption : 1357i246
1 3 5 7 2 4 6
['1', '3', '5', '7']
['2', '4', '6']
1234567 print(dec2[i-1],end="")
IndexError: list index out of range
Process finished with exit code 1
CodePudding user response:
You can use the zip() method. Then flatten the list using list comprehension.
lst1 = ['a','c','e','g']
lst2 = ['b','d','f']
def merge(lst1, lst2):
lst3 = list(zip(lst1, lst2))
lst3 = [l for y in lst3 for l in y]
extra = None
if len(lst1) > len(lst2):
lst3.append(lst1[-1])
elif len(lst1) < len(lst2):
lst3.append(lst1[-2])
return lst3
merge_list = merge(lst1,lst2)
print(merge_list)
CodePudding user response:
you can do the following to get the final_list
def merge_list(lst1, lst2):
lst3 = []
while lst1 and lst2:
lst3.append(lst1.pop(0))
lst3.append(lst2.pop(0))
if lst1:
lst3.extend(lst1)
if lst2:
lst3.extend(lst2)
return lst3
lst1 = ['a', 'c', 'e', 'g']
lst2 = ['b', 'd', 'f']
print(merge_list(lst1, lst2))
CodePudding user response:
You can use itertools.zip_longest
:
from itertools import zip_longest
def encrypt(s):
odd = s[::2]
even = s[1::2]
return odd even
def decrypt(s):
odd = s[:(len(s) 1)//2]
even = s[(len(s) 1)//2:]
lst = [x y for x, y in zip_longest(odd, even, fillvalue='')]
return(''.join(lst))
s = "Save our Soul"
print(encrypt(s)) # Sv u olaeorSu
print(decrypt(encrypt(s))) # Save our Soul
If you don't want to use zip_longest
for some reason, then you can use zip
but need to take care of the last element of odd
when odd
is longer than even
:
def decrypt(s):
odd = s[:(len(s) 1)//2]
even = s[(len(s) 1)//2:]
lst = [x y for x, y in zip(odd, even)]
if len(odd) > len(even):
lst.append(odd[-1])
return(''.join(lst))