Assignment 7.1: The Quicksort (4 point)

Familiarize yourself with the principle of Quicksort from the background material and implement Quicksort in Python. Create a function qsort(A: list, i: int, j: int) which has the following input parameters:
The function has no return value since the sorting process is done to the original list. You can select the pivot index/value as you wish.

Limits:
A code template with an example program:

def qsort(A, i, j):
    # TODO
    
  
if __name__ == "__main__":
    A = [9, 7, 1, 8, 5, 3, 6, 2, 4]
    print(A)    # [9, 7, 1, 8, 5, 3, 6, 2, 4]
    qsort(A, 0, len(A)-1)
    print(A)    # [1, 2, 3, 4, 5, 6, 7, 8, 9]

Submit your solution in CodeGrade as quicksort.py.


Assignment 7.2: Car Sales (3 points)

A car shop has cars \(A = [a_1, a_2, ..., a_n]\) (one of each) where \(a_i\) is the price of the car \(i\). Customers \(B = [b_1, b_2, ..., b_m]\) arrive to the shop. \(b_i\) is the price that the customer \(i\) can afford. What is the maximum amount of sales that can be made?

For example the shop has cars \(A = [20, 10, 15, 26]\) and there are customers \(B = [11, 25, 15, 9]\) it is possible to make \(3\) sales.
Create a function sales(A: list, B: list) in Python which returns the number of possible sales.

Limits:
Target: the function performs in \(\Theta(n \log n)\) time.

A code template with an example program:

def sales(cars, customers) -> int:
    # TODO

if __name__ == "__main__":
    print(sales([20, 10, 15], [11, 25, 15]))                        # 3
    print(sales([13, 7, 2, 3, 12, 4, 19], [3, 25, 16, 14]))         # 4
    print(sales([24, 6, 20, 21, 12, 5], [25, 1, 24, 15]))           # 3
    print(sales([14, 9, 10, 15, 18, 20], [24, 17, 9, 22, 12, 4]))   # 5   

Submit your solution in CodeGrade as sales.py.


Assignment 7.3: Subsets (2 points)

A given set that has numbers from \(1\) to \(N\) in increasing order (\(\{1, 2, 3, 4, \dots, N\}\)), create a function subsets(N: int) in Python which produces a list of all possible subsets.

For example when \(N = 3\) the subsets are \([1]\), \([2]\), \([1, 2]\), \([3]\), \([1, 3]\), \([2, 3]\) and \([1, 2, 3]\).

The function must return the list of subsets in specific order (see the example program below).

Limits: \(1 \leq N \leq 20\)

A code template with an example program:

def subsets(n: int) -> list:
    # TODO


if __name__ == "__main__":
    print(subsets(3))   # [[1], [2], [1, 2], [3], [1, 3], [2, 3], [1, 2, 3]]

    print(subsets(4))   # [[1], [2], [1, 2], [3], [1, 3], [2, 3], [1, 2, 3],
                        #  [4], [1, 4], [2, 4], [1, 2, 4], [3, 4], [1, 3, 4],
                        #  [2, 3, 4], [1, 2, 3, 4]]

    S = subsets(10)
    print(S[95])    # [6, 7]
    print(S[254])   # [1, 2, 3, 4, 5, 6, 7, 8]
    print(S[826])   # [1, 2, 4, 5, 6, 9, 10]


Submit your solution in CodeGrade as subsets.py