Home > Enterprise >  Replace every letter in a string using dictionary values
Replace every letter in a string using dictionary values

Time:02-13

coders of the internet,

The goal of this assignment was to replace the letters of a sequence with their opposite. 'A' would be 'U', 'C' would be 'G', and vice versa. Here is the string that I came up with so far, but the output is wrong. I can't find a pattern in the output, so I can't reverse-engineer the problem.

sequence = input('Please enter a sequence: ')
def reverse(sequence):
  dic = { ' A ' : ' U ' , ' G ' : ' C ' , ' C ' : ' G ' , ' U ' : ' A ' }
  for i in range( len ( sequence ) ):
    if sequence[i] in dic.keys():
      sequence = sequence.replace(sequence[i], dic[sequence[i]])
  return sequence
reverse(sequence)

Does anyone know how to simply return turn an input (let's just use "GUGU"), and make the code return the output: "CACA"?

CodePudding user response:

I am seeing two issues:

  1. replace affects all occurrences. That i is only used for evaluating the arguments (sequence[i] and dic[sequence[i]]), but once they are evaluated, the call is equivalent to e.g. sequence.replace("A", "U"), and then all occurrences of A are replaced with U, not just the once at index i.

  2. Keys and values in the dictionary are wrapped with spaces. Assuming the sequence is something like ACCAGUUA, there are no matches, due to those spaces.

CodePudding user response:

When you have circular replacements, you need to only convert each letter once. The translate method can do that for you (better than replace):

dic = { ' A ' : ' U ' , ' G ' : ' C ' , ' C ' : ' G ' , ' U ' : ' A ' }

sequence = sequence.translate({ord(k):ord(v) for k,v in dic.items()})

You could also use maketrans to setup the mapping:

mapping = str.maketrans("AGCU","UCGA")

sequence  = sequence.translate(mapping)

without translate or replace, you could use the dictionary on each individual character and join the converted characters:

sequence = "".join(dic.get(c,c) for c in sequence) 

CodePudding user response:

Here's my solution.

# Convert input to list format
sequence = list(str(input('Please enter a sequence: ')))

# Define keys
keys = {
    "a": "u",
    "g": "c",
    "c": "g",
    "u": "a"
}

# Create the reverse function
def reverse(sequence: list):
    # Iterate by enumeration and item in sequence list
    for i, letter in enumerate(sequence):
        if letter in keys: # If the letter has a translation
            sequence[i] = keys[letter] # Replace that value
    return ''.join(sequence) # Turn it back into a string

# Pass old list into reverse function
new_sequence = reverse(sequence)
print(new_sequence, type(new_sequence))

# Response: caca <class 'str'>

It may contain a few extra steps but clearly outlines how to achieve what you were looking for, step-by-step. The process is essentially:

  1. Convert sequence to list.
  2. Iterate the list and replace values based on keys.
  3. Turn the list back into a string, and return the string.
  • Related