PYTHON   57
satisfiesF
Guest on 26th August 2023 12:36:51 AM


  1. def satisfiesF(L):
  2.     """
  3.    Assumes L is a list of strings
  4.    Assume function f is already defined for you and it maps a string to a Boolean
  5.    Mutates L such that it contains all of the strings, s, originally in L such
  6.            that f(s) returns True, and no other elements. Remaining elements in L
  7.            should be in the same order.
  8.    Returns the length of L after mutation
  9.    """
  10.     r=[]
  11.     for i in L:
  12.         if f(i)==True:
  13.             r.append(i)
  14.     L[:] = r  
  15.     return len(L)
  16.  
  17. run_satisfiesF(L, satisfiesF)
  18.  
  19.  
  20.  
  21. def f(s):
  22.     return 'a' in s
  23.      
  24. L = ['a', 'b', 'a']
  25. print ( satisfiesF(L) )
  26. print ( L )

Raw Paste

Login or Register to edit or fork this paste. It's free.