Guest User

Untitled

a guest
Jul 22nd, 2018
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.98 KB | None | 0 0
  1. #Same Structure
  2.  
  3. #Define a procedure, same_structure, that takes two inputs. It should output
  4. #True if the lists contain the same elements in the same structure, and False
  5. #otherwise. Two values, p and q have the same structure if:
  6.  
  7. #    Neither p or q is a list.
  8.  
  9. #    Both p and q are lists, they have the same number of elements, and each
  10. #    element of p has the same structure as the corresponding element of q.
  11.  
  12.  
  13. #For this procedure, you can use the is_list(p) procedure from Homework 6:
  14.  
  15. def is_list(p):
  16.     return isinstance(p, list)
  17.  
  18.  
  19. def same_structure(a,b):
  20.     if is_list(a) == is_list(b):
  21.        
  22.  
  23.  
  24.  
  25. #Here are some examples:
  26.  
  27. #print same_structure(3, 7)
  28. #>>> True
  29.  
  30. #print same_structure([1, 0, 1], [2, 1, 2])
  31. #>>> True
  32.  
  33. #print same_structure([1, [0], 1], [2, 5, 3])
  34. #>>> False
  35.  
  36. #print same_structure([1, [2, [3, [4, 5]]]], ['a', ['b', ['c', ['d', 'e']]]])
  37. #>>> True
  38.  
  39. #print same_structure([1, [2, [3, [4, 5]]]], ['a', ['b', ['c', ['de']]]])
  40. #>>> False
Add Comment
Please, Sign In to add comment