Used a brute force method; is it a bit messy?

Looks sound to me.
It can be done smarter (avoiding repeated traversals through things) but other than that it reads like the definition of common values between two things.

A different way this could be nested is:

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

  two = []
  for letter in string_two:
    if letter not in two:

  both = []
  for letter in one:
    if letter in two:

  return both

What this avoids is “for each in a: for each in b” - the number of steps in that is len(a) * len(b)
It seems likely that there would be something like 100 different characters, so the number of steps instead becomes 100 * len(a) + 100 * len(b) if a and b are millions of letters long then nesting their loops would result in a program that takes a very long time to finish.

Better yet would be use of the set datatype, that would allow for a single traversal of each input.

1 Like

Here is my attempt. As we have already created the contains(string_one, string_two) function, I reused the code in the common_letters function:

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

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

tested and works :smiley:

I’m in love with list comprehension <3

def common_letters(string_one, string_two):
  common = []
  [common.append(char) for char in string_one if char in string_two and char not in common]
  return common

Heads will turn if you ever use that code in an interview question. Even though it does work, it is not a comprehension. A comprehension is an expression that we can return, at least that’s how I interpret it.

>>> def common_letters(string_one, string_two):
	return {x for x in string_one if x in string_two}

>>> common_letters('all along the watchtower', 'stairway to heaven')
{'r', 'h', ' ', 'n', 'w', 'a', 'o', 't', 'e'}