Advertisement
Guest User

links

a guest
Sep 24th, 2022
29
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.84 KB | None | 0 0
  1. empty ='empty'
  2.  
  3.  
  4.  
  5. def is_link(s):
  6.     """s is a linked list if it is empty or a (first, rest) pair."""
  7.     return s == empty or (len(s) == 2 and is_link(s[1]))
  8.  
  9. def link(first, rest):
  10.         """Construct a linked list from its first element and the rest."""
  11.         assert is_link(rest), "rest must be a linked list."
  12.         return [first, rest]
  13.  
  14. def first(s):
  15.         """Return the first element of a linked list s."""
  16.         assert is_link(s), "first only applies to linked lists."
  17.         assert s != empty, "empty linked list has no first element."
  18.         return s[0]
  19.  
  20. def rest(s):
  21.         """Return the rest of the elements of a linked list s."""
  22.         assert is_link(s), "rest only applies to linked lists."
  23.         assert s != empty, "empty linked list has no rest."
  24.         return s[1]
  25.  
  26.  
  27. def len_link(s):
  28.         """Return the length of linked list s."""
  29.         length = 0
  30.         while s != empty:
  31.             s, length = rest(s), length + 1
  32.         return length
  33.  
  34. def getitem_link(s, i):
  35.         """Return the element at index i of linked list s."""
  36.         while i > 0:
  37.             s, i = rest(s), i - 1
  38.         return first(s)
  39.  
  40. def extend_link(s, t):
  41.         """Return a list with the elements of s followed by those of t."""
  42.         assert is_link(s) and is_link(t)
  43.         if s == empty:
  44.             return t
  45.         else:
  46.             return link(first(s), extend_link(rest(s), t))
  47.        
  48. def apply_to_all_link(f, s):
  49.         """Apply f to each element of s."""
  50.         assert is_link(s)
  51.         if s == empty:
  52.             return s
  53.         else:
  54.             return link(f(first(s)), apply_to_all_link(f, rest(s)))
  55.  
  56. def keep_if_link(f, s):
  57.         """Return a list with elements of s for which f(e) is true."""
  58.         assert is_link(s)
  59.         if s == empty:
  60.             return s
  61.         else:
  62.             kept = keep_if_link(f, rest(s))
  63.             if f(first(s)):
  64.                 return link(first(s), kept)
  65.             else:
  66.                 return kept
  67.  
  68. def join_link(s, separator):
  69.         """Return a string of all elements in s separated by separator."""
  70.         if s == empty:
  71.             return ""
  72.         elif rest(s) == empty:
  73.             return str(first(s))
  74.         else:
  75.             return str(first(s)) + separator + join_link(rest(s), separator)
  76.  
  77. def partitions(n, m):
  78.         """Return a linked list of partitions of n using parts of up to m.
  79.        Each partition is represented as a linked list.
  80.        """
  81.         if n == 0:
  82.             return link(empty, empty) # A list containing the empty partition
  83.         elif n < 0 or m == 0:
  84.             return empty
  85.         else:
  86.             using_m = partitions(n-m, m)
  87.             with_m = apply_to_all_link(lambda s: link(m, s), using_m)
  88.             without_m = partitions(n, m-1)
  89.             return extend_link(with_m, without_m)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement