Advertisement
UME14

Note: MITx 6.00.1x_Week 2

Jan 1st, 2018
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.47 KB | None | 0 0
  1. 1) 'Recursion on Non-Numerics' is to be revised for PSet, Problem 3.
  2. 2) fex: is in
  3. Exercise: is in
  4. 0.0/5.0 points (graded)
  5. ESTIMATED TIME TO COMPLETE: 18 minutes
  6. ---------------------------------------
  7. We can use the idea of bisection search to determine if a character is in a string, so long as the string is sorted in alphabetical order.
  8.  
  9. First, test the middle character of a string against the character you're looking for (the "test character"). If they are the same, we are done - we've found the character we're looking for!
  10.  
  11. If they're not the same, check if the test character is "smaller" than the middle character. If so, we need only consider the lower half of the string; otherwise, we only consider the upper half of the string. (Note that you can compare characters using Python's < function.)
  12.  
  13. Implement the function isIn(char, aStr) which implements the above idea recursively to test if char is in aStr. char will be a single character and aStr will be a string that is in alphabetical order. The function should return a boolean value.
  14.  
  15. As you design the function, think very carefully about what the base cases should be.
  16. -------------------------------------------------------------------------------------------------------------------------------------
  17. def isIn(char, aStr):
  18. '''
  19. char: a single character
  20. aStr: an alphabetized string
  21.  
  22. returns: True if char is in aStr; False otherwise
  23. '''
  24. # Your code here
  25. ----------------
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement