2. Write a function num_diff(s1, s2) that takes as inputs two strings s1 and s2 (which you can assume have the same length) and returns the number of differences between the two strings i.e., the number of positions at which the two strings have different characters. For example:

>>> num_diff('alien', 'allen') # only position 2 is different
1

>>> num_diff('alien', 'alone') # the last 3 positions all differ
3

>>> num_diff('same', 'same') # no differences!
0

3. Write a function index(elem, seq) that takes as inputs an element elem and a sequence seq and returns the index of the first occurrence of elem in seq. The sequence seq can be either a list or a string. If seq is a string, elem will be a single-character string; if seq is a list, elem can be any value. If elem is not an element of seq, the function should return the length of the sequence. Dont forget that the index of the first element in a sequence is 0. Here are some examples:

>>> index(5, [4, 10, 5, 3, 7, 5])
2

>>> index('a', 'banana')
1

>>> index('i', 'team')
4

>>> index('hi', ['hello', 111, True])
3

>>> index('hi', ['well', 'hi', 'there'])
1

>>> index('a', '') # the empty string
0

>>> index(42, []) # the empty list
0

Hints:

  • The closest model for this problem is the version of the mylen function that is able to handle both lists and strings.
  • You will need at least two base cases for this function.
  • You could in theory use the not in operator to check for the case in which the element is not in the sequence by doing something like
if elem not in seq:
  • However, its possible to write this function without using not in if you correctly handle cases in which the sequence has no elements (i.e., the empty string and empty list). Either approach will be accepted, but we encourage you to see if you can solve the problem without using not in.

DNA transcription! The last two functions are based on an incredible molecular feat called transcription, in which your cells create molecules of messenger RNA that mirror the sequence of nucleotides in your DNA. The RNA is then used to create proteins that do the work of the cell.

4. Write a function called one_dna_to_rna(c) that takes as input a single-character string c representing a DNA nucleotide and returns the corresponding messenger-RNA nucleotide. Here are the DNA-to-RNA conversions:

‘A’ in DNA becomes ‘U’ in RNA.
‘C’ in DNA becomes ‘G’ in RNA.
‘G’ in DNA becomes ‘C’ in RNA.
‘T’ in DNA becomes ‘A’ in RNA.

Any other input should produce an output of the empty string (''). This function does not require recursion.

Here are some examples:

>>> one_dna_to_rna('C')
'G'

>>> one_dna_to_rna('T')
'A'

>>> one_dna_to_rna('X')
''

5. Write a function called transcribe(s) that takes as input a string s representing a piece of DNA, and returns a string representing the corresponding RNA. Any characters in the input that dont correspond to a DNA nucleotide should not appear in the returned RNA string. (Note that if you implemented the previous function correctly, you shouldnt need to do anything special here to ensure that non-DNA characters are removed.) For example:

>>> transcribe('ACGT TGCA')
'UGCAACGU'

>>> transcribe('GATTACA')
'CUAAUGU'

>>> transcribe('cs111') # lower-case letters don't count
''

Use your one_dna_to_rna function and recursion to solve this problem. As with the scrabble_score function, you may find it helpful to use the num_vowels function from lecture as a model, but in this case you will be adding together strings instead of numbers!

Academic Honesty!
It is not our intention to break the school's academic policy. Posted solutions are meant to be used as a reference and should not be submitted as is. We are not held liable for any misuse of the solutions. Please see the frequently asked questions page for further questions and inquiries.
Kindly complete the form. Please provide a valid email address and we will get back to you within 24 hours. Payment is through PayPal, Buy me a Coffee or Cryptocurrency. We are a nonprofit organization however we need funds to keep this organization operating and to be able to complete our research and development projects.