def greedy_make_change(n): C = Array.indexed(1, 6) d = Array([1, 2, 5, 10, 20, 50]) for i in rbetween(d.length, 1): C[i] = math.floor(n / d[i]) n %= d[i] return C
def get_shortest_bitonic_path_length_bruteforce(points): n = points.length min_length = math.inf for k in between(0, n - 2): for right_path in itertools.combinations(between(2, n - 1), k): left_path = [x for x in rbetween(n - 1, 2) if x not in right_path] path_length = get_path_length(points, [1] + list(right_path) + [n] + left_path + [1]) min_length = min(min_length, path_length) return min_length
def greedy_activity_selector_(s, f): n = s.length - 2 A = {'a' + str(n)} i = n for m in rbetween(n - 1, 1): if f[m] <= s[i]: A.add('a' + str(m)) i = m return A
def get_assembly_time_based_on_lines(lines, last_line, a, t, e, x, n): i = last_line assembly_time = x[i] for j in rbetween(n, 2): assembly_time += a[i, j] if i != lines[i, j]: i = lines[i, j] assembly_time += t[i, j - 1] assembly_time += a[i, 1] + e[i] return assembly_time
def josephus(n, m): T = RedBlackTree(sentinel=OSNode(None)) for j in between(1, n): x = OSNode(j) os_insert(T, x) j = 1 for k in rbetween(n, 1): j = (j + m - 1) % k + 1 x = os_select(T.root, j) print(x.key) os_delete(T, x)
def counting_sort(A, B, k): C = Array.indexed(0, k) for i in between(0, k): C[i] = 0 for j in between(1, A.length): C[A[j]] = C[A[j]] + 1 for i in between(1, k): C[i] = C[i] + C[i - 1] for j in rbetween(A.length, 1): B[C[A[j]]] = A[j] C[A[j]] = C[A[j]] - 1
def _sort_by_character(A, p, r, position): k = ord('z') - ord('a') C = Array([0] * (k + 1), start=0) for j in between(p, r): x = ord(A[j][position - 1]) - ord('a') C[x] += 1 for i in between(1, k): C[i] += C[i - 1] B = Array.indexed(1, r - p + 1) for j in rbetween(r, p): x = ord(A[j][position - 1]) - ord('a') B[C[x]] = A[j] C[x] -= 1 A.elements[p - 1:r] = B.elements
def josephus_simulate(n, m): L = List() singly_linked_list_insert(L, SNode(n)) x = L.head for i in rbetween(n - 1, 1): singly_linked_list_insert(L, SNode(i)) x.next = L.head for i in between(1, n): for j in between(1, m): x = x.next print(x.next.key) if L.head is x.next: L.head = x.next.next x.next = x.next.next
def fuzzy_partition(A, p, r): j = random(p, r) A[r], A[j] = A[j], A[r] x = A[r].low i = p - 1 for j in between(p, r - 1): if A[j].low <= x: i = i + 1 A[i], A[j] = A[j], A[i] A[i + 1], A[r] = A[r], A[i + 1] q = i + 1 for k in rbetween(i, p): if A[k].high >= x: q = q - 1 A[q], A[k] = A[k], A[q] return q, i + 1
def jobs_scheduling(t, p, d): n = p.length a = Array(list(between(1, n))) _sort_jobs_by_deadlines(a, t, p, d) for i in between(1, n): d[i] = min(d[i], n ** 2) P = Array.indexed(0, d[n]) s = Array([Array.indexed(0, d[n]) for _ in between(1, n)]) for j in between(0, d[n]): P[j] = 0 for i in between(1, n): for j in between(0, d[n]): s[i, j] = 0 for i in between(1, n): for j in rbetween(d[n], t[i]): if P[min(j, d[i]) - t[i]] + p[i] > P[j]: P[j] = P[min(j, d[i]) - t[i]] + p[i] s[i, j] = 1 return P, s, a
def jobs_scheduling(t, p, d): n = p.length a = Array(list(between(1, n))) _sort_jobs_by_deadlines(a, t, p, d) for i in between(1, n): d[i] = min(d[i], n**2) P = Array.indexed(0, d[n]) s = Array([Array.indexed(0, d[n]) for _ in between(1, n)]) for j in between(0, d[n]): P[j] = 0 for i in between(1, n): for j in between(0, d[n]): s[i, j] = 0 for i in between(1, n): for j in rbetween(d[n], t[i]): if P[min(j, d[i]) - t[i]] + p[i] > P[j]: P[j] = P[min(j, d[i]) - t[i]] + p[i] s[i, j] = 1 return P, s, a
def test_print_stations(self): n = random.randint(1, 10) l = Array([Array.indexed(1, n), Array.indexed(1, n)]) l[1, 1], l[2, 1] = 0, 0 for i in between(2, n): l[1, i], l[2, i] = random.choice([(1, 1), (1, 2), (2, 2)]) l_star = random.randint(1, 2) captured_output = io.StringIO() with redirect_stdout(captured_output): print_stations(l, l_star, n) actual_output = captured_output.getvalue().splitlines() expected_output = [] i = l_star expected_output.append('line ' + str(i) + ', station ' + str(n)) for j in rbetween(n, 2): i = l[i, j] expected_output.append('line ' + str(i) + ', station ' + str(j - 1)) assert_that(actual_output, is_(equal_to(expected_output)))
def test_print_stations_(self): n = random.randint(1, 10) l = Array([Array.indexed(1, n), Array.indexed(1, n)]) l[1, 1], l[2, 1] = 0, 0 for i in between(2, n): l[1, i], l[2, i] = random.choice([(1, 1), (1, 2), (2, 2)]) l_star = random.randint(1, 2) captured_output = io.StringIO() with redirect_stdout(captured_output): print_stations_(l, l_star, n) actual_output = captured_output.getvalue().splitlines() expected_output = [] i = l_star expected_output.append('line ' + str(i) + ', station ' + str(n)) for j in rbetween(n, 2): i = l[i, j] expected_output.append('line ' + str(i) + ', station ' + str(j - 1)) expected_output.reverse() assert_that(actual_output, is_(equal_to(expected_output)))
def activity_scheduler(s, f): n = s.length A = Array.indexed(1, n) F = Array(list(rbetween(n, 1))) F.top = n B = RedBlackTree() # events contains triples (a, b, c) where a = 0 if the event is finish of an activity and 1 if it is start, # b as the activity number, and c as the start time or the finish time events = [(0, i + 1, finish_time) for i, finish_time in enumerate(f)] + \ [(1, i + 1, start_time) for i, start_time in enumerate(s)] events.sort(key=lambda e: (e[2], e[0])) for e in events: if e[0] == 1: hall_number = pop(F) A[e[1]] = hall_number rb_insert(B, Node(e[1], data=hall_number), sentinel=B.nil) else: hall = rb_search(B.root, e[1], sentinel=B.nil) push(F, hall.data) rb_delete(B, hall, sentinel=B.nil) return A
def heapsort(A): build_max_heap(A) for i in rbetween(A.length, 2): A[1], A[i] = A[i], A[1] A.heap_size = A.heap_size - 1 max_heapify(A, 1)
def bubble_sort(A): for i in between(1, A.length): for j in rbetween(A.length, i + 1): if A[j] < A[j - 1]: A[j], A[j - 1] = A[j - 1], A[j]
def _build_min_heap(A): A.heap_size = A.length for i in rbetween(math.floor(A.length / 2), 1): _min_heapify(A, i)
def direct_address_maximum(T): m = T.length for i in rbetween(m - 1, 0): if T[i] is not None: return T[i] return None
def print_stations(l, l_star, n): i = l_star print('line ' + str(i) + ', station ' + str(n)) for j in rbetween(n, 2): i = l[i, j] print('line ' + str(i) + ', station ' + str(j - 1))