• Categories
    • Unread
    • Recent
    • Popular
    • Users
    • Groups
    • Register
    • Login

    First bit of Python in a long time

    Scheduled Pinned Locked Moved Development and Coding
    4 Posts 2 Posters 181 Views 1 Watching
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • ScuzzS Offline
      Scuzz
      last edited by Scuzz

      My manager’s son is learning Python and for some reason he thinks i’m a pro in Python.
      His son is doing some online course and he was struggling with a question.

      def satisfiesF(L):
          """
          Assumes L is a list of strings
          Assume function f is already defined for you and it maps a string to a Boolean
          Mutates L such that it contains all of the strings, s, originally in L such
          that f(s) returns True, and no other elements. Remaining elements in L should be in the same order.
          Returns the length of L after mutation
          """
          # Your function implementation here
      
      run_satisfiesF(L, satisfiesF)
      
      

      After a while I managed to get something working and it passed 100% when he submitted it.

      def f(s):
          return 'a' in s
      
      def satisfiesF(L):
          
          for item in L:
              if not f(item)
                  L.remove(item)
                  
          return len(L)
      
      L = ['a', 'b', 'a', 'josh', 'gary', 'mark', 'dave', 'chris']
      print satisfiesF(L)
      print L
      
      
      1 Reply Last reply Reply Quote
      • AlmostA Offline
        Almost
        last edited by Almost

        Man, this is really awkward way to phrase a question about filtering.

        Nice job, though. Good to see you doing shit again :P

        EDIT: Is it safe to modify a list while iterating over it in python? A quick google search suggests no.

        1 Reply Last reply Reply Quote
        • ScuzzS Offline
          Scuzz
          last edited by Scuzz

          See, i kinda figured that out while i was testing some things.
          It would print out incorrect data as the list was getting smaller, skipping some strings.

          I then tried with a second empty list that i then made L equal to. This would not modify L globally which is what this question wanted.

          EDIT:

          Modified the code to be what i think is “correct”

          def f(s):
              return 'b' in s
          
          def satisfiesF(L):
              
              x = []
              for item in L:
                  if f(item):
                      x.append(item)  
              return x
          
          L = ['a', 'b', 'a', 'a', 'b', 'a','a', 'b', 'a',]
          L = satisfiesF(L)
          print len(L)
          print L
          
          

          I think the example test code and the question are giving off the wrong idea as they want you to make global changes to L from within the function which can only be done via returning a value from the function. The test code says you need to return the length of L and make changes to L globally from within the function. You can use global to make global changes but doing this declares L as global and local which throws errors.

          Seems a shitty way of asking a question and using shitty example code?

          def f(s):
              return 'a' in s
                
          L = ['a', 'b', 'a']
          print satisfiesF(L)
          print L
          
          #Should print:
          #2
          #['a', 'a']
          
          1 Reply Last reply Reply Quote
          • AlmostA Offline
            Almost
            last edited by

            Maybe they expected you to do two passes?

            def satisfiesF(L):
                x = []
                for item in L:
                    if f(item):
                        x.append(item)
            
                for item in x:
                    L.remove(item)
            
                return len(L)
            

            Seems like a bad question to me.

            If we’re going with the second interpretation (where you return a new list) I think this would work:

            def satisfiesF(L):
                return [x for x in L if f(x)]
            
            1 Reply Last reply Reply Quote
            • 1 / 1
            • First post
              Last post
            Online Users