Great Deal! Get Instant $10 FREE in Account on First Order + 10% Cashback on Every Order Order Now

import copy import sys import random def cons(x, l): new_l = copy.copy(l) new_l.insert(0,x) return new_l def head (l): if l == []: print("Cannot call head on the empty list.", file=sys.stderr)...

1 answer below »
import copy
    
    
    import sys
    
    import random
    
    
    
    def cons(x, l):
    
     new_l = copy.copy(l)
    
     new_l.insert(0,x)
    
     return new_l
    
    
    
    def head (l):
    
     if l == []:
    
     print("Cannot call head on the empty list.", file=sys.stde
)
    
     sys.exit()
    
     x , *xs = l
    
     return x
    
    
    
    def tail (l):
    
     if l == []: return []
    
     x, *xs = l
    
     return xs
    
    
    
    def append(l1,l2):
    
     return l1 + l2
    
    
    
    def reverse(l):
    
     new_l = copy.copy(l)
    
     new_l.reverse()
    
     return new_l
    
    
    
    def repeat(x,n):
    
     if n < 0:
    
     print ("Cannot call repeat with negative values.", file=sys.stde
)
    
     sys.exit()
    
     return [x] * n
    
    
    
    def _take(n,acc,l):
    
     if n == 0:
    
     return acc
    
     elif n < 0:
    
     print ("Cannot call take with negagive values.", file=sys.stde
)
    
     sys.exit()
    
     x = head(l)
    
     rest = tail(l)
    
     return _take (n-1,cons(x,acc),rest)
    
    
    
    def take(n,l):
    
     if l == []: return []
    
     return reverse (_take(n,[],l))
    
    
    
    def drop(n,l):
    
     if n == 0:
    
     return l
    
     elif n < 0:
    
     print ("Cannot call drop with negative values.", file=sys.stde
)
    
     sys.exit()
    
     es = tail (l)
    
     return drop (n-1, es)
    
    
    
    def _length(acc, l):
    
     if l == []:
    
     return acc
    
     return _length(1+acc,tail(l))
    
    
    
    def length(l):
    
     return _length(0,l)
    
    
    
    def _randint_list(n, acc, low, high):
    
     if n == 0: return acc
    
     x = random.randint(low,high)
    
     return _randint_list (n - 1, cons(x,acc), low, high)
    
    
    
    def randint_list(n, low, high):
    
     if n < 0:
    
     print ("Cannot call randint_list with negative values.", file=sys.stde
)
    
     sys.exit()
    
     return _randint_list(n,[], low, high)
    
    
    
    def _randfloat_list(n, acc, low, high):
    
     if n == 0: return acc
    
     x = random.randint(low,high)
    
     return _randfloat_list (n - 1, cons(x,acc), low, high )
    
    
    
    def randfloat_list(n, low, high):
    
     if n < 0:
    
     print ("Cannot call randint_list with negative values.", file=sys.stde
)
    
     sys.exit()
    
     return _randfloat_list(n,[], low, high)
    
    
    
    def unique(l):
    
     if l == []:
    
     return []
    
     e = head (l)
    
     es = tail (l)
    
     new_tail = [x for x in es if x != e]
    
     return cons(e, unique(new_tail))
    
    
    
    def list_range(low, high):
    
     return list(range(low, high))
Answered Same Day Apr 29, 2021

Solution

Neha answered on May 01 2021
152 Votes
56071/__pycache__/list_lib.cpython-38.pyc
56071/intersect.py
# DO NOT CHANGE THE FOLLOWING CODE:
import list_lib as ll
# END SECTION OF CODE YOU'RE NOT CHANGING
# Define an function called intersect that takes two a
ays as input,
# and then returns a new a
ay that contains the elements which appea
# in both of the input a
ays. For example,
#
# intersect([1,2,42,5], [7,1,43,2,76,84]) = [1,2]
# intersect([1,2,42,5], [42,43,76,5,1]) = [1,42,5]
def intersect(list1, list2):
tempList = set(list2)
list3 = [value for value in list1 if value in tempList]
return list3
print(intersect([1,2,42,5], [7,1,43,2,76,84]))
print(intersect([1,2,42,5], [42,43,76,5,1]))
56071/list_lib.py
import copy    
import sys
import random
    
def cons(x, l):
new_l = copy.copy(l)
new_l.insert(0,x)
return new_l
    
def head (l):
if l == []:
...
SOLUTION.PDF

Answer To This Question Is Available To Download

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here