from PIL import Image

im = Image.open("../images/Dictionaries.png")
display(im)

Hacks pt 2: List sorting algorithm

import random

lst = [random.randint(1, 100) for i in range(100)]

def mergeSort(arr):
    if len(arr) > 1:
        mid = len(arr)//2
        L = arr[:mid]
        R = arr[mid:]
        mergeSort(L) # recursion, recur to sort lists
        mergeSort(R)
 
        i = 0
        j = 0
        k = 0

        while i < len(L) and j < len(R):
            if L[i] <= R[j]:
                arr[k] = L[i]
                i += 1
            else:
                arr[k] = R[j]
                j += 1
            k += 1

        while i < len(L):
            arr[k] = L[i]
            i += 1
            k += 1
 
        while j < len(R):
            arr[k] = R[j]
            j += 1
            k += 1

# sort a random list 


print(f"List before sorting: {lst}")
mergeSort(lst)
print(f"List after sorting {lst}")
 
List before sorting: [46, 94, 8, 2, 9, 48, 10, 95, 5, 74, 17, 34, 86, 48, 32, 68, 87, 88, 22, 83, 60, 66, 24, 5, 20, 11, 92, 48, 85, 47, 99, 6, 71, 91, 69, 34, 65, 31, 91, 94, 1, 53, 30, 43, 13, 15, 89, 65, 23, 8, 89, 8, 84, 41, 11, 80, 25, 35, 22, 38, 98, 93, 6, 88, 1, 56, 5, 71, 93, 52, 41, 27, 56, 18, 3, 71, 52, 14, 86, 85, 38, 66, 44, 42, 52, 26, 42, 12, 2, 35, 10, 9, 10, 5, 25, 77, 11, 43, 10, 54]
List after sorting [1, 1, 2, 2, 3, 5, 5, 5, 5, 6, 6, 8, 8, 8, 9, 9, 10, 10, 10, 10, 11, 11, 11, 12, 13, 14, 15, 17, 18, 20, 22, 22, 23, 24, 25, 25, 26, 27, 30, 31, 32, 34, 34, 35, 35, 38, 38, 41, 41, 42, 42, 43, 43, 44, 46, 47, 48, 48, 48, 52, 52, 52, 53, 54, 56, 56, 60, 65, 65, 66, 66, 68, 69, 71, 71, 71, 74, 77, 80, 83, 84, 85, 85, 86, 86, 87, 88, 88, 89, 89, 91, 91, 92, 93, 93, 94, 94, 95, 98, 99]

Hacks 4

from PIL import Image


im = Image.open("/Users/vn1/myblog/blog/_notebooks/images/kahoot.png")
display(im)

Simulation

A basic dice simulation and more complex hardy-weinburg simulations

Hardy-Weinburg

import random

per = {}

dice = int(input("What sided die would you like to test? "))
times = int(input("How many times do you want to test this? "))

for i in range(times):
    num = random.randint(1, dice)
    if num not in per:
        per[num] = 1
    else:
        per[num] += 1
keys = list(per.keys())
keys.sort()
for key in keys:
    print(f"{key} appeared {[per[key]]}/{times} or about {per[key]/times*100}%")

print(f"Expected average for a {dice} sided die: {1/dice*100}%")
1 appeared [490]/10000 or about 4.9%
2 appeared [535]/10000 or about 5.35%
3 appeared [480]/10000 or about 4.8%
4 appeared [472]/10000 or about 4.72%
5 appeared [492]/10000 or about 4.92%
6 appeared [497]/10000 or about 4.97%
7 appeared [485]/10000 or about 4.8500000000000005%
8 appeared [523]/10000 or about 5.2299999999999995%
9 appeared [493]/10000 or about 4.93%
10 appeared [529]/10000 or about 5.29%
11 appeared [537]/10000 or about 5.37%
12 appeared [497]/10000 or about 4.97%
13 appeared [491]/10000 or about 4.91%
14 appeared [528]/10000 or about 5.28%
15 appeared [500]/10000 or about 5.0%
16 appeared [500]/10000 or about 5.0%
17 appeared [487]/10000 or about 4.87%
18 appeared [516]/10000 or about 5.16%
19 appeared [473]/10000 or about 4.73%
20 appeared [475]/10000 or about 4.75%
Expected average for a 20 sided die: 5.0%