Strings and Conditionals Exercise related Question

Question from this exercise on strings in conditionals:

Write a function called common_letters that takes two arguments, string_one and string_two and then returns a list with all of the letters they have in common.

The letters in the returned list should be unique.

I already wrote this code for a previous question to check whether a string is within another string:

def contains(big_string, little_string):
  if little_string in big_string:
    return True
    return False

For the question I copy pasted here, I used this code:

def common_letters(string_one, string_two):
  group1 = []
  group2 = []
  alphabets = "abcdefghijklmnopqrstuvwxyz"
  for a in alphabets:
    for c1 in string_one:
      if a == c1:
    for c2 in string_two:
      if a == c2:
  common = []
  for g1 in group1:
    for g2 in group2:
      if g1 == g2:
  return common

When I pressed run, I got this response:

Expected the test common_letters(‘python’, ‘ruby on rails’) to return [‘y’, ‘o’, ‘n’], instead got [‘n’, ‘o’, ‘y’]

My question is, does the sequence really matter?

And to be honest, I didn’t really know why my code worked…

It’s my first time posting a question, I wonder if the format is ok.

Hello @testing Welcome to the forums! You formatting is good. Now, for whether the sequence matters, it kind of does. For instance, say you were parsing over a string , “hello world”, and you wanted to know if the sequence of letters matched another string, “hello secret word”. It would be no good to know that the letter “h, l, o, l, e” were in the string, you would want to know if the letter combination, “h,e,l,l,o”, was in the string. I know that is a bit of a contrived example, but you can see why you might need it?

On to your code:

Do you really need this bit? Because in this code, all you’re doing is creating two lists of the input strings. I see why you’re doing it, but you don’t need it to answer the question. (If you wanted to turn the strings into list without checking for numbers/punctuation, you could use list("string")). As well, I think it is this part of your code that is causing [“n”, “o”, “y”] to be printed.

1 Like

If you want to understand why your code works the way it does, step through it on a piece of paper – write it out. Hint: You are using the alphabet string as the basis for comparison.

def contains(big_string, little_string):
  return little_string in big_string

print(contains("watermelon", "melon"))
print(contains("watermelon", "berry"))

def common_letters(string_one, string_two):
  contains_yes = []
  for letter in string_one:
    if letter in string_two and letter not in contains_yes:
  return contains_yes

print(common_letters("banana", "bacon"))


['b', 'a', 'n']
1 Like

Hello! Welcome to the forums. :slight_smile:

In this case… no, I wouldn’t say so, but it would appear that the exercise has expected you to implement the comparison in a certain way and so it’s expecting a result based on that assumption.

The exercise doesn’t have a requirement that the letters be returned in the order they’re found, so since your code (for the given test, anyhow) returned the correct three letters I’d say it works.

As @harrjt has demonstrated, by way of their solution, your code could be refactored to reduce the amount of code (and to stick with the DRY mantra) needed to arrive at the solution. :slight_smile: