[Tutor] Stuck on Challenge in Google Python Class

Adam Hurwitz adamhurwitz at google.com
Wed Jan 22 05:24:54 CET 2014


Hi,

This is a coding challenge in the Google Developers Python
Course<https://developers.google.com/edu/python/exercises/basic>.
I have been working on this challenge for hours without being able to solve.

A. match_ends
# Given a list of strings, return the count of the number of
# strings where the string length is 2 or more and the first
# and last chars of the string are the same.
# Note: python does not have a ++ operator, but += works.'

Try #1:

def match_ends(words):
  numberStrings = [ ]
  answer = ' '

  for string in words:
    if len(string) >=2 or string[0] == string[-1]:
      numberStrings.append(string)
    else:
      return ' '

  answer = len(numberStrings)
  print answer



def match_ends(words):

# Calls the above functions with interesting inputs.
def main():
  print 'match_ends'
  test(match_ends(['aba', 'xyz', 'aa', 'x', 'bbb']), 3)
  test(match_ends(['', 'x', 'xy', 'xyx', 'xx']), 2)
  test(match_ends(['aaa', 'be', 'abc', 'hello']), 1)


Tries 2 and on: I wanted to get just the part working to check to see if
the first and last character are the same. I tried setting up a for loop
within a for loop. The first loop cycles through each word in the list,
then the second for loop cycles through each letter to compare if the first
character is equal to the last or not. I cannot get it to compare the first
and last letter properly.

Help solving this would be very much appreciated!!

Thank you for the help,

Adam
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20140121/63f79660/attachment.html>


More information about the Tutor mailing list