Results 1 to 3 of 3

Thread: Help

  1. #1
    Join Date
    Jul 2008
    Posts
    136
    Mentioned
    0 Post(s)
    Quoted
    0 Post(s)

    Default Help

    Ive been trying to do this

    Code:
    #!/usr/bin/python -tt
    # Copyright 2010 Google Inc.
    # Licensed under the Apache License, Version 2.0
    # http://www.apache.org/licenses/LICENSE-2.0
    
    # Google's Python Class
    # http://code.google.com/edu/languages/google-python-class/
    
    # Basic list exercises
    # Fill in the code for the functions below. main() is already set up
    # to call the functions with a few different inputs,
    # printing 'OK' when each function is correct.
    # The starter code for each function includes a 'return'
    # which is just a placeholder for your code.
    # It's ok if you do not complete all the functions, and there
    # are some additional functions to try in list2.py.
    
    # 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.
    def match_ends(words):
    
      count = 0
      for num in words:
       
        if len(num) > 0:
          first = num[0]
          second = num[-1]
    
    
      
        if len(num) >= 2:
          if (first == second):
            count += 1
    # +++your code here+++
      return count
    
    
    # B. front_x
    # Given a list of strings, return a list with the strings
    # in sorted order, except group all the strings that begin with 'x' first.
    # e.g. ['mix', 'xyz', 'apple', 'xanadu', 'aardvark'] yields
    # ['xanadu', 'xyz', 'aardvark', 'apple', 'mix']
    # Hint: this can be done by making 2 lists and sorting each of them
    # before combining them.
    def front_x(words):
      array1 = []
      array2 = words
    
      for findx in words:
       
        if (findx[0] == 'x'):
          array1.append(findx)
          array2.remove(findx)  
      
        
      array1.sort()
      array2.sort()
      array1.extend(array2)
    
    
    
    
     # print array1,array2,words,findx
    
      
        
    
      
      return array1
    
    
    
    # C. sort_last
    # Given a list of non-empty tuples, return a list sorted in increasing
    # order by the last element in each tuple.
    # e.g. [(1, 7), (1, 3), (3, 4, 5), (2, 2)] yields
    # [(2, 2), (1, 3), (3, 4, 5), (1, 7)]
    # Hint: use a custom key= function to extract the last element form each tuple.
    def sort_last(tuples):
      # +++your code here+++
      return
    
    
    # Simple provided test() function used in main() to print
    # what each function returns vs. what it's supposed to return.
    def test(got, expected):
      if got == expected:
        prefix = ' OK '
      else:
        prefix = '  X '
      print '%s got: %s expected: %s' % (prefix, repr(got), repr(expected))
    
    
    # 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)
    
      print
      print 'front_x'
      test(front_x(['bbb', 'ccc', 'axx', 'xzz', 'xaa']),
           ['xaa', 'xzz', 'axx', 'bbb', 'ccc'])
      test(front_x(['ccc', 'bbb', 'aaa', 'xcc', 'xaa']),
           ['xaa', 'xcc', 'aaa', 'bbb', 'ccc'])
      test(front_x(['mix', 'xyz', 'apple', 'xanadu', 'aardvark']),
           ['xanadu', 'xyz', 'aardvark', 'apple', 'mix'])
    
           
      print
      print 'sort_last'
      test(sort_last([(1, 3), (3, 2), (2, 1)]),
           [(2, 1), (3, 2), (1, 3)])
      test(sort_last([(2, 3), (1, 2), (3, 1)]),
           [(3, 1), (1, 2), (2, 3)])
      test(sort_last([(1, 7), (1, 3), (3, 4, 5), (2, 2)]),
           [(2, 2), (1, 3), (3, 4, 5), (1, 7)])
    
    
    if __name__ == '__main__':
      main()
    But for some reasons it seems to skip 'xaa' in the list array in the for loop right after once executing the if statement so please help me

  2. #2
    Join Date
    Jun 2007
    Location
    Wednesday
    Posts
    2,446
    Mentioned
    3 Post(s)
    Quoted
    1 Post(s)

    Default

    Change
    array2 = words
    to
    array2 = words[:]

    In Python, you have mutable datatypes and immutable datatypes. A mutable datatype means that when you do "x = y" and then change y you will also change x. An immutable datatype doesn't do this, so doing "x = y" and then changing y will not change x. A list - declared as x = [] - is a mutable type, while a tuple - declared as x = () - is not. Since words is a list, when you do array2 = words and then, in the loop, do array2.remove(findx), you're also removing entries from words, so the for loop terminates prematurely.

    Happy Pythoning!
    By reading this signature you agree that mixster is superior to you in each and every way except the bad ways but including the really bad ways.

  3. #3
    Join Date
    Jul 2008
    Posts
    136
    Mentioned
    0 Post(s)
    Quoted
    0 Post(s)

    Default

    thanks brother makes sense

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •