list1.py

458 views
Skip to first unread message

Bleys Andromeda-Focht

unread,
Jul 15, 2014, 2:23:31 PM7/15/14
to python-g...@googlegroups.com
Good day,
 
I have been plugging along on Google's python class and I have encountered my first problem. In the first problem of the list1.py exercise, I can't seem to find the process. For those that have forgotten the requirements:
 
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.
 
My current code isn't worth showing, because I have completely re-written it a few times with no success.
 
Also, I am confused on what the += operator does. Thus far I believe that it only adds a value to an existing list.
 
Thank you for your help.

Robert Mandić

unread,
Jul 15, 2014, 2:28:36 PM7/15/14
to python-g...@googlegroups.com

You just need to loop through the list and when the condition in question is true, increment the count by one - this is where count +=1 will be needed. It's the same as count = count + 1. If you still have problems, I can provide with full solution.

--
You received this message because you are subscribed to the Google Groups "Python GCU Forum" group.
To unsubscribe from this group and stop receiving emails from it, send an email to python-gcu-for...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Message has been deleted

Bleys Andromeda-Focht

unread,
Jul 15, 2014, 3:11:45 PM7/15/14
to python-g...@googlegroups.com

Thank you for the timely answer. That really put me on the right path. Can you please take a look at what I've done so far and let me know if I have the right idea? 

 
def match_ends(words):
  i=3
  count=0
  print words
  while i>0:
    a=words.pop(0)
    if len(a)>0:
      if a[0]==a[-1]:
        count=count+1
      else:
        return
 
 
  i=i-1
  return
 
Eventaully I get the following errors:
 Traceback (most recent call last):
  File "C:\Python27\ArcGIS10.1\Lib\site-packages\pythonwin\pywin\framework\scriptutils.py", line 326, in RunScript
    exec codeObject in __main__.__dict__
  File "C:\Users\bfocht\Bleys\Python\Google\basic\list1.py", line 103, in <module>
    main()
  File "C:\Users\bfocht\Bleys\Python\Google\basic\list1.py", line 78, in main
    test(match_ends(['aba', 'xyz', 'aa', 'x', 'bbb']), 3)
  File "C:\Users\bfocht\Bleys\Python\Google\basic\list1.py", line 28, in match_ends
    a=words.pop(0)
IndexError: pop from empty list

Robert Mandić

unread,
Jul 16, 2014, 2:11:58 AM7/16/14
to python-g...@googlegroups.com
for word in words:
  c = 0
  # length is 2 or more
  if len(word) >= 2:
    # first and last char in the word are the same
    if word[0] == word[-1]:
      c += 1
  return c




--
You received this message because you are subscribed to the Google Groups "Python GCU Forum" group.
To unsubscribe from this group and stop receiving emails from it, send an email to python-gcu-for...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
Lp, Robert
Reply all
Reply to author
Forward
0 new messages