diff --git a/Finished Code/python/src/recitation/election/__pycache__/election_io.cpython-313.pyc b/Finished Code/python/src/recitation/election/__pycache__/election_io.cpython-313.pyc new file mode 100644 index 0000000..5171c2f Binary files /dev/null and b/Finished Code/python/src/recitation/election/__pycache__/election_io.cpython-313.pyc differ diff --git a/Finished Code/python/src/recitation/election/data/conventions.csv b/Finished Code/python/src/recitation/election/data/conventions.csv new file mode 100644 index 0000000..bc9e629 --- /dev/null +++ b/Finished Code/python/src/recitation/election/data/conventions.csv @@ -0,0 +1,51 @@ +Alabama,33.86695773,66.13304227 +Alaska,52.77777778,47.22222222 +Arizona,48.78267555,51.21732445 +Arkansas,42.51223929,57.48776071 +California,67.46493113,32.53506887 +Colorado,54.65047528,45.34952472 +Connecticut,59.11145551,40.88854449 +Delaware,59.07963005,40.92036995 +District of Columbia,64.05570061,35.94429939 +Florida,53.86044044,46.13955956 +Georgia,46.23483446,53.76516554 +Hawaii,69.88979271,30.11020729 +Idaho,39.15450428,60.84549572 +Illinois,62.38961734,37.61038266 +Indiana,43.160299,56.839701 +Iowa,51.85149188,48.14850812 +Kansas,48.11919556,51.88080444 +Kentucky,47.62026612,52.37973388 +Louisiana,40.84012539,59.15987461 +Maine,57.60145366,42.39854634 +Maryland,66.76285414,33.23714586 +Massachusetts,66.81608523,33.18391477 +Michigan,52.99960789,47.00039211 +Minnesota,58.12451744,41.87548256 +Mississippi,34.75420037,65.24579963 +Missouri,54.90702479,45.09297521 +Montana,46.72523962,53.27476038 +Nebraska,46.55953251,53.44046749 +Nevada,49.9762121,50.0237879 +New Hampshire,60.4576449,39.5423551 +New Jersey,59.03060204,40.96939796 +New Mexico,55.21274175,44.78725825 +New York,62.60577569,37.39422431 +North Carolina,49.8196006,50.1803994 +North Dakota,33.87146371,66.12853629 +Ohio,51.97145123,48.02854877 +Oklahoma,36.63899338,63.36100662 +Oregon,57.6187378,42.3812622 +Pennsylvania,53.1753379,46.8246621 +Rhode Island,55.83333333,44.16666667 +South Carolina,46.51853725,53.48146275 +South Dakota,41.58382477,58.41617523 +Tennessee,41.69142541,58.30857459 +Texas,42.34618978,57.65381022 +Utah,41.86378036,58.13621964 +Vermont,86.35511323,13.64488677 +Virginia,57.66956409,42.33043591 +Washington,59.31714516,40.68285484 +West Virginia,45.68239915,54.31760085 +Wisconsin,57.32167832,42.67832168 +Wyoming,35.02788845,64.97211155 \ No newline at end of file diff --git a/Finished Code/python/src/recitation/election/data/debates.csv b/Finished Code/python/src/recitation/election/data/debates.csv new file mode 100644 index 0000000..b4de60c --- /dev/null +++ b/Finished Code/python/src/recitation/election/data/debates.csv @@ -0,0 +1,51 @@ +Alabama,25.57864282,74.42135718 +Alaska,51.74333819,48.25666181 +Arizona,54.07366071,45.92633929 +Arkansas,39.49400799,60.50599201 +California,62.18633209,37.81366791 +Colorado,56.95519282,43.04480718 +Connecticut,62.58445946,37.41554054 +Delaware,72.243404,27.756596 +District of Columbia,86.39077906,13.60922094 +Florida,49.13971934,50.86028066 +Georgia,39.77695167,60.22304833 +Hawaii,78.54251012,21.45748988 +Idaho,38.9151281,61.0848719 +Illinois,64.01312266,35.98687734 +Indiana,51.90771961,48.09228039 +Iowa,56.57914256,43.42085744 +Kansas,55.97600369,44.02399631 +Kentucky,49.86316366,50.13683634 +Louisiana,40.59007018,59.40992982 +Maine,62.83742465,37.16257535 +Maryland,67.8948832,32.1051168 +Massachusetts,68.90841931,31.09158069 +Michigan,56.18987871,43.81012129 +Minnesota,57.14070276,42.85929724 +Mississippi,34.57598179,65.42401821 +Missouri,56.47835657,43.52164343 +Montana,45.00144886,54.99855114 +Nebraska,46.55953251,53.44046749 +Nevada,51.67688895,48.32311105 +New Hampshire,60.88105145,39.11894855 +New Jersey,56.10281254,43.89718746 +New Mexico,61.50711424,38.49288576 +New York,57.9081976,42.0918024 +North Carolina,49.3203998,50.6796002 +North Dakota,33.37239965,66.62760035 +Ohio,49.8957439,50.1042561 +Oklahoma,42.24100248,57.75899752 +Oregon,60.99816369,39.00183631 +Pennsylvania,53.47880522,46.52119478 +Rhode Island,61.11414528,38.88585472 +South Carolina,42.24614077,57.75385923 +South Dakota,49.18389554,50.81610446 +Tennessee,42.87445164,57.12554836 +Texas,45.57180257,54.42819743 +Utah,45.54707379,54.45292621 +Vermont,61.9060889,38.0939111 +Virginia,58.74475856,41.25524144 +Washington,62.40545651,37.59454349 +West Virginia,33.4566379,66.5433621 +Wisconsin,60.81730769,39.18269231 +Wyoming,34.89883052,65.10116948 \ No newline at end of file diff --git a/Finished Code/python/src/recitation/election/data/earlyPolls.csv b/Finished Code/python/src/recitation/election/data/earlyPolls.csv new file mode 100644 index 0000000..3f0ca5a --- /dev/null +++ b/Finished Code/python/src/recitation/election/data/earlyPolls.csv @@ -0,0 +1,51 @@ +Alabama,32,68 +Alaska,47.21627409,52.78372591 +Arizona,50.47318612,49.52681388 +Arkansas,43.37349398,56.62650602 +California,65.7223796,34.2776204 +Colorado,49.71098266,50.28901734 +Connecticut,54.54545455,45.45454545 +Delaware,56.75675676,43.24324324 +District of Columbia,64.05570061,35.94429939 +Florida,52.44366704,47.55633296 +Georgia,49.53703704,50.46296296 +Hawaii,69.88979271,30.11020729 +Idaho,39.50617284,60.49382716 +Illinois,61.11111111,38.88888889 +Indiana,45.12195122,54.87804878 +Iowa,51.61290323,48.38709677 +Kansas,43.90243902,56.09756098 +Kentucky,45.71890145,54.28109855 +Louisiana,40.90909091,59.09090909 +Maine,55.84415584,44.15584416 +Maryland,69.25925926,30.74074074 +Massachusetts,67.24137931,32.75862069 +Michigan,55.16431925,44.83568075 +Minnesota,55.48780488,44.51219512 +Mississippi,48.31460674,51.68539326 +Missouri,48.36065574,51.63934426 +Montana,36.85503686,63.14496314 +Nebraska,52.02799427,47.97200573 +Nevada,48.23529412,51.76470588 +New Hampshire,54.25894378,45.74105622 +New Jersey,58.62068966,41.37931034 +New Mexico,55.40540541,44.59459459 +New York,61.48373984,38.51626016 +North Carolina,50.45558087,49.54441913 +North Dakota,33.87146371,66.12853629 +Ohio,51.0851419,48.9148581 +Oklahoma,36.84210526,63.15789474 +Oregon,52.79503106,47.20496894 +Pennsylvania,53.22982276,46.77017724 +Rhode Island,55.83333333,44.16666667 +South Carolina,47.70114943,52.29885057 +South Dakota,41.58382477,58.41617523 +Tennessee,44.30379747,55.69620253 +Texas,48.35164835,51.64835165 +Utah,47.18309859,52.81690141 +Vermont,61.9047619,38.0952381 +Virginia,57.22713864,42.77286136 +Washington,56.97674419,43.02325581 +West Virginia,31.39534884,68.60465116 +Wisconsin,56.27240143,43.72759857 +Wyoming,35.02788845,64.97211155 \ No newline at end of file diff --git a/Finished Code/python/src/recitation/election/data/electoralVotes.csv b/Finished Code/python/src/recitation/election/data/electoralVotes.csv new file mode 100644 index 0000000..c4bc236 --- /dev/null +++ b/Finished Code/python/src/recitation/election/data/electoralVotes.csv @@ -0,0 +1,51 @@ +Alabama,9 +Alaska,3 +Arizona,11 +Arkansas,6 +California,55 +Colorado,9 +Connecticut,7 +Delaware,3 +District of Columbia,3 +Florida,29 +Georgia,16 +Hawaii,4 +Idaho,4 +Illinois,20 +Indiana,11 +Iowa,6 +Kansas,6 +Kentucky,8 +Louisiana,8 +Maine,4 +Maryland,10 +Massachusetts,11 +Michigan,16 +Minnesota,10 +Mississippi,6 +Missouri,10 +Montana,3 +Nebraska,5 +Nevada,6 +New Hampshire,4 +New Jersey,14 +New Mexico,5 +New York,29 +North Carolina,15 +North Dakota,3 +Ohio,18 +Oklahoma,7 +Oregon,7 +Pennsylvania,20 +Rhode Island,4 +South Carolina,9 +South Dakota,3 +Tennessee,11 +Texas,38 +Utah,6 +Vermont,3 +Virginia,13 +Washington,12 +West Virginia,5 +Wisconsin,10 +Wyoming,3 \ No newline at end of file diff --git a/Finished Code/python/src/recitation/election/election_io.py b/Finished Code/python/src/recitation/election/election_io.py new file mode 100644 index 0000000..b115073 --- /dev/null +++ b/Finished Code/python/src/recitation/election/election_io.py @@ -0,0 +1,38 @@ +import csv # csv is a library within python that lets you read csv files + + +def process_electoral_votes(file_path): + """ + takes in a file path woth the csv file with the first col as the state and the second col with how many votes tayt state gets. + This will return a dictionary mapping states to how many votes they get. + """ + + result = dict() + with open(file_path, 'r') as f: + reader = csv.reader(f) + for row in reader: + state = row[0] + votes = row[1] + result[state] = votes + print(row) + + return result + + + +def process_poll_results(file_path): + """ + Takes in a file path with the csv file woth first col = state, second col = canodate 1 results, third col = canidate 2 results, returns a dictionary mapping states to canidate results. + dictionary key = state, value = c1 results. + """ + + result = dict() + with open(file_path, 'r') as f: + reader = csv.reader(f) + for row in reader: + print(row) + state = row[0] + c1_results = row[1] + result[state] = c1_results + + return result diff --git a/Finished Code/python/src/recitation/election/main.py b/Finished Code/python/src/recitation/election/main.py new file mode 100644 index 0000000..b1d0052 --- /dev/null +++ b/Finished Code/python/src/recitation/election/main.py @@ -0,0 +1,22 @@ +import random # for generating random numbers +from election_io import * # for reading from files + +def main() : + print("Let's simulate an election!") # reading csv files! + + electoral_votes_file_path = "data/electoralVotes.csv" + electoral_votes = process_electoral_votes(electoral_votes_file_path) + + # print("electoral_votes", electoral_votes) + + print("electoral_votes", electoral_votes) + + + + + + + +if __name__ == "__main__": + main() + diff --git a/Finished Code/python/src/recitation/primes/__pycache__/main.cpython-313.pyc b/Finished Code/python/src/recitation/primes/__pycache__/main.cpython-313.pyc new file mode 100644 index 0000000..6335a41 Binary files /dev/null and b/Finished Code/python/src/recitation/primes/__pycache__/main.cpython-313.pyc differ diff --git a/Finished Code/python/src/recitation/primes/__pycache__/main.cpython-314.pyc b/Finished Code/python/src/recitation/primes/__pycache__/main.cpython-314.pyc new file mode 100644 index 0000000..fbdb4ae Binary files /dev/null and b/Finished Code/python/src/recitation/primes/__pycache__/main.cpython-314.pyc differ diff --git a/Finished Code/python/src/recitation/primes/main.py b/Finished Code/python/src/recitation/primes/main.py new file mode 100644 index 0000000..4b2c9b9 --- /dev/null +++ b/Finished Code/python/src/recitation/primes/main.py @@ -0,0 +1,200 @@ +import math + +import time + +def main(): + print("Prime finding.") + + n = 10 + + prime_booleans = trivial_prime_finder(n) + + print(prime_booleans) + + prime_booleans = sieve_of_eratosthenes(n) + + print(prime_booleans) + + prime_list = list_primes(n) + + print("The primes up to", n, "are", prime_list) + + # timing trivial_prime_finder + + n = 10000000 + + start = time.time() # START + trivial_prime_finder(n) + elapsed_trivial = time.time() - start # STOP + print(f"trivial_prime_finder took {elapsed_trivial:.6f} seconds.") + + #timing sieve + start = time.time() + sieve_of_eratosthenes(n) + elapsed_sieve = time.time() - start # STOP + print(f"sieve_of_eratosthenes took {elapsed_sieve:.6f} seconds.") + + # print the speedup + if elapsed_sieve > 0: + speedup = elapsed_trivial/elapsed_sieve + print(f"Speedup: {speedup:.2f} times faster.") + + +""" +Pseudocode below. + +TrivialPrimeFinder(n) + primeBooleans ← array of n+1 false boolean variables + for every integer p from 2 to n + if IsPrime(p) is true + primeBooleans[p] ← true + return primeBooleans + +IsPrime(p) + if p < 2 + return false + for every integer k between 2 and √p + if k is a divisor of p + return false + return true + +SieveOfEratosthenes(n) + primeBooleans ← array of n+1 true boolean variables + primeBooleans[0] ← false + primeBooleans[1] ← false + for every integer p between 2 and √n + if primeBooleans[p] = true + primeBooleans ← CrossOffMultiples(primeBooleans, p) + return primeBooleans + +CrossOffMultiples(primeBooleans, p) + n ← length(primeBooleans) - 1 + for every multiple k of p (from 2p to n) + primeBooleans[k] ← false + return primeBooleans +""" + +def trivial_prime_finder(n: int) -> list[bool]: + """ + Returns a list of boolean variables storing the primality of each nonnegative integer up to and including n. + Parameters: + - n (int): an integer + Returns: + list[bool]: a list of boolean variables storing the primality of each nonnegative integer up to and including n. + """ + + if n < 0: + raise ValueError("n must be nonnegative.") + + # initialize a bunch of values to False + prime_booleans = [False] * (n+1) + + # range over all integers from 2 to n, checking if each one is prime + for p in range(2, n+1): + prime_booleans[p] = is_prime(p) + + return prime_booleans + +def is_prime(p: int) -> bool: + """ + Test if p is prime. + Parameters: + - p (int): an integer + Returns: + bool: True if p is prime and False otherwise. + """ + + if p < 2: + return False + + # range over all possible divisors between 2 and sqrt(p), and if we find one that is a divisor of p, it can't be prime + for k in range(2, int(math.sqrt(p)+1)): + # if k is a divisor of p, return false + if p % k == 0: + return False + + # if we make it here, we never found a divisor of p, which must be prime + return True + +def sieve_of_eratosthenes(n: int) -> list[bool]: + """ + Returns a list of boolean variables storing the primality of each nonnegative integer up to and including n, + implementing the "sieve of Eratosthenes" algorithm. + Parameters: + - n (int): an integer + Returns: + list: a list of boolean variables storing the primality of each nonnegative integer up to and including n. + """ + + if n < 0: + raise ValueError("n must be nonnegative.") + + prime_booleans = [True]*(n+1) + + # we know that 0 and 1 are not prime + prime_booleans[0] = False + prime_booleans[1] = False + + # range over every p between 2 and sqrt(n) and cross off its multiples + for p in range(2, int(math.sqrt(n) + 1)): + if prime_booleans[p] == True: + # we know that p is prime, so cross off its multiples (i.e., set its multiples to false) + prime_booleans = cross_off_multiples(prime_booleans, p) + + return prime_booleans + +def cross_off_multiples(prime_booleans: list[bool], p:int) -> list[bool]: + """ + Returns an updated list in which all variables in the list whose indices are multiples of p (greater than p) have + been set to false. + Parameters: + - prime_booleans (list): a list of boolean variables storing the primality of each nonnegative integer + - p (int): an integer + Returns: + list[bool]: a list of boolean variables storing the primality of each nonnegative integer up to and including n with + multiples of p (greater than p) set to false. + """ + + # we know that len(prime_booleans) = n + 1 + # n = len(prime_booleans) - 1 + + if p <= 0: + raise ValueError("p should be positive.") + + if len(prime_booleans) == 0: + raise ValueError("empty prime_booleans list.") + + n = len(prime_booleans) - 1 + + for k in range(2*p, n+1, p): + prime_booleans[k] = False + + return prime_booleans + +def list_primes(n: int) -> list[int]: + """ + List all prime numbers up to and (possibly) including n. + Parameters: + - n (int): an integer + Returns: + list[int]: a list containing all prime numbers up to and (possibly) including n. + """ + + if n < 0: + raise ValueError("n should be nonnegative.") + + prime_list = [] + + # first, use sieve of Eratosthenes + prime_booleans = sieve_of_eratosthenes(n) + + # range over this list of booleans, and if we find something that's prime, add it to prime_list + for p in range(0, len(prime_booleans)): + if prime_booleans[p] == True: + # add it to list of primes! + prime_list.append(p) + + return prime_list + +if __name__ == "__main__": + main() \ No newline at end of file diff --git a/Finished Code/python/src/recitation/recitation1starter.py b/Finished Code/python/src/recitation/recitation1starter.py new file mode 100644 index 0000000..2ee0dfe --- /dev/null +++ b/Finished Code/python/src/recitation/recitation1starter.py @@ -0,0 +1,95 @@ +import math +from primes.main import sieve_of_eratosthenes +import matplotlib.pyplot as plt + + +def prime_count_array(n: int): + #TODO Implement this function + """ + We will return a list of how many primes we've seen so far at a given index. + input: n -> int + output: list of how many primes we've seen so far at a given index. + """ + prime_list = sieve_of_eratosthenes(n) + result = [0] * (n + 1) + counter = 0 + for i in range(len(prime_list)): + is_prime = prime_list[i] + if is_prime: + counter = counter + 1 + result[i] = counter + return result + + +def prime_count_array_plotting(): + N = 200000 + pi = prime_count_array(N) + # Sample points to keep plotting light + step = 100 + xs = list(range(2, N + 1, step)) + ys = [pi[x] for x in xs] + ys_approx = [x / math.log(x) for x in xs] # n/log n approximation + # Plot + plt.figure(figsize=(9, 6)) + plt.plot(xs, ys, label=r"$\pi(n)$ (prime count)") + plt.plot(xs, ys_approx, linestyle="--", label=r"$n/\log n$ (approximation)") + plt.title("Prime Counting Function $\\pi(n)$ vs. $n$ (with $n/\\log n$ approximation)") + plt.xlabel("n") + plt.ylabel(r"$\pi(n)$") + plt.legend() + plt.tight_layout() + plt.show() + +def ulam_coords(n: int): + coords = [(0, 0)] * n + x = y = 0 + k = 1 + step_len = 1 + + def write_and_step(dx, dy, steps, k, x, y): + for _ in range(steps): + if k > n: + break + coords[k - 1] = (x, y) + x += dx + y += dy + k += 1 + return k, x, y + while k <= n: + # right, up + k, x, y = write_and_step(1, 0, step_len, k, x, y) + k, x, y = write_and_step(0, 1, step_len, k, x, y) + step_len += 1 + # left, down + k, x, y = write_and_step(-1, 0, step_len, k, x, y) + k, x, y = write_and_step(0, -1, step_len, k, x, y) + step_len += 1 + return coords + +def plot_ulam(n: int = 20000, point_size: float = 2.0): + is_prime = sieve_of_eratosthenes(n) + coords = ulam_coords(n) + xs = [] + ys = [] + for k in range(2, n + 1): # start at 2 (first prime) + if is_prime[k]: + x, y = coords[k - 1] + xs.append(x) + ys.append(y) + plt.figure(figsize=(8, 8)) + plt.scatter(xs, ys, s=point_size) + plt.gca().set_aspect('equal', adjustable='box') + plt.axis('off') + plt.title(f"Ulam Spiral of Primes up to {n}") + plt.show() + plt.close() + + +def main(): + prime_count_array_plotting() + # plot_ulam(n=30000, point_size=1.8) + + + +if __name__ == "__main__": + main() \ No newline at end of file diff --git a/Finished Code/python/src/variables/main.py b/Finished Code/python/src/variables/main.py index 0c2a4f4..ac72f90 100644 --- a/Finished Code/python/src/variables/main.py +++ b/Finished Code/python/src/variables/main.py @@ -15,7 +15,7 @@ def main(): # Let's declare some variables j = 14 #Python gives this type int x = -2.3 #type: float - yo_world = "Hi" #type: string + yo_world = "Hi" # type: ignore #type: string statement = True # type: bool #Note: multi-word variable names use snake_case