Python Forum
Alternate solutions to cipher problem?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Alternate solutions to cipher problem?
#1
I coded the following program to translate a string using a cipher dictionary:

CIPHER_DICT = {'e': 'u', 'b': 's', 'k': 'x', 'u': 'q', 'y': 'c', 'm': 'w', 'o': 'y', 'g': 'f', 'a': 'm', 'x': 'j', 'l': 'n', 's': 'o', 'r': 'g', 'i': 'i', 'j': 'z', 'c': 'k', 'f': 'p', ' ': 'b', 'q': 'r', 'z': 'e', 'p': 'v', 'v': 'l', 'h': 'h', 'd': 'd', 'n': 'a', 't': ' ', 'w': 't'}

print(' This is a cipher program, which translates text as directed by cipher_dict:')
print(CIPHER_DICT)
print(' We will operate on:')
print()
str1 = 'the quick brown fox jumped over the lazy dog' #Substituted lower for upper case t as first letter
print('', str1)
print()

count = 0
liststr1 = list(str1) #gets list of characters in string

for letter in liststr1:
    newval = CIPHER_DICT[letter]
    liststr1[count] = newval
    count = count + 1 #using list index to rewrite list

print(' Translated string list is: ',liststr1)
print()
print(" In English, that is:","".join(liststr1)) #converting back to list
I'm a newbie so I'm thinking that is quite inefficient. I took a string, converted to list, and went back to string.

What better ways could I have used to solve this?

Thanks!
Reply
#2
List comprehensions are faster than equivalent loops. We can remove some of the supporting assignments, conversions, and operations too.

CIPHER_DICT = {'e': 'u', 'b': 's', 'k': 'x', 'u': 'q', 'y': 'c', 'm': 'w', 'o': 'y', 'g': 'f', 'a': 'm', 'x': 'j', 'l': 'n', 's': 'o', 'r': 'g', 'i': 'i', 'j': 'z', 'c': 'k', 'f': 'p', ' ': 'b', 'q': 'r', 'z': 'e', 'p': 'v', 'v': 'l', 'h': 'h', 'd': 'd', 'n': 'a', 't': ' ', 'w': 't', ' ': ' '}
str1 = 'the quick brown fox jumped over the lazy dog'
print("".join([CIPHER_DICT[x] for x in str1]))
Reply
#3
(Oct-03-2019, 05:34 PM)stullis Wrote: List comprehensions are faster than equivalent loops. We can remove some of the supporting assignments, conversions, and operations too.

Can you give me a couple examples, Stullis?
Reply
#4
A nice review of list comprehensions can be found at
Socratica - Youtube on list comprehensions
This shows several examples of varying complexity.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Question Rsa Cipher Paragoon2 3 578 Nov-27-2023, 12:30 PM
Last Post: snippsat
  RSA Cipher with blocks Paragoon2 0 453 Nov-26-2023, 04:35 PM
Last Post: Paragoon2
  Where is problem in decrypting ADFGVX cipher Paragoon2 2 589 Nov-06-2023, 10:24 AM
Last Post: Paragoon2
  Positive integral solutions for Linear Diophantine Equation august 4 1,193 Jan-13-2023, 09:48 PM
Last Post: Gribouillis
  Transposition Cipher Issues beginner_geek07 0 1,045 Apr-08-2022, 07:44 PM
Last Post: beginner_geek07
  Caesar Cipher Help pbrowne 2 2,112 Jun-30-2021, 02:36 PM
Last Post: deanhystad
  using alternate names in configparser Skaperen 6 2,766 Oct-01-2020, 08:27 PM
Last Post: Skaperen
  pip unistall in alternate roots confminn 0 1,421 Aug-21-2020, 08:03 PM
Last Post: confminn
  Alternate package of win32com.client manigandanpro 0 3,702 Aug-19-2020, 07:38 AM
Last Post: manigandanpro
  Coding caesar's cipher drewbty 3 2,720 May-16-2020, 10:05 AM
Last Post: DPaul

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020