Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions inject_solutions.bash
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/bin/bash

gh repo clone project-lovelace/lovelace-solutions
ln -s ../lovelace-solutions/python problems/solutions
3 changes: 3 additions & 0 deletions problems/ada_lovelaces_note_g.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,9 @@ def generate_test_case(test_type: TestCaseType) -> ProblemTestCase:
elif test_type is TestCaseType.LARGE_ODD:
n = 2 * randint(50, 125) + 1

else:
raise ValueError(f"Unrecognized test case: {test_type}")

test_case.input['n'] = n
test_case.output['numerator'], test_case.output['denominator'] = bernoulli(n)

Expand Down
9 changes: 6 additions & 3 deletions problems/almost_pi.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,15 +42,18 @@ def generate_test_case(test_type: TestCaseType) -> ProblemTestCase:
if test_type is TestCaseType.SMALL_N:
N = randint(2, 10)

if test_type is TestCaseType.MEDIUM_N:
elif test_type is TestCaseType.MEDIUM_N:
N = randint(11, 100)

if test_type is TestCaseType.LARGE_N:
elif test_type is TestCaseType.LARGE_N:
N = randint(101, 1000)

if test_type is TestCaseType.HUGE_N:
elif test_type is TestCaseType.HUGE_N:
N = randint(10000, 100000)

else:
raise ValueError(f"Unrecognized test case: {test_type}")

test_case.input['N'] = N
test_case.output['pi'] = almost_pi(N)

Expand Down
3 changes: 3 additions & 0 deletions problems/babylonian_square_roots.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@ def generate_test_case(test_type: TestCaseType) -> ProblemTestCase:
elif test_type is TestCaseType.SQUARE:
n = randint(5, 100)**2

else:
raise ValueError(f"Unrecognized test case: {test_type}")

test_case.input['n'] = float(n)
test_case.output['sqrt_n'] = float(babylonian_sqrt(n))

Expand Down
2 changes: 1 addition & 1 deletion problems/blood_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ def generate_test_case(test_type: TestCaseType) -> ProblemTestCase:
donated_blood = []

else:
raise ValueError("Unrecognized test case: {}".format(test_type))
raise ValueError(f"Unrecognized test case: {test_type}")

test_case.input = {"patient_blood_type": patient_blood_type, "donated_blood": donated_blood}

Expand Down
3 changes: 3 additions & 0 deletions problems/caesar_cipher.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,9 @@ def generate_test_case(test_type: TestCaseType) -> ProblemTestCase:
elif test_type is TestCaseType.THE_WIRE:
plaintext = "This drug thing this aint police work I mean I can send any fool with a badge and a gun to a corner to jack a crew and grab vials But policing I mean you call something a war and pretty soon everyone is going to be running around acting like warriors They gonna be running around on a damn crusade storming corners racking up body counts And when you at war you need a fucking enemy And pretty soon damn near everybody on every corner is your fucking enemy And soon, the neighborhood youre supposed to be policing thats just occupied territory".lower()

else:
raise ValueError(f"Unrecognized test case: {test_type}")

known_word = random.choice(plaintext.split())

shift = random.randint(1, 25)
Expand Down
3 changes: 3 additions & 0 deletions problems/chaos.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@ def generate_test_case(test_type: TestCaseType) -> ProblemTestCase:
elif test_type is TestCaseType.DIVERGENCE:
r = 3.6

else:
raise ValueError(f"Unrecognized test case: {test_type}")

test_case.input['r'] = float(r)
test_case.output['x'] = logistic_map(r)

Expand Down
3 changes: 3 additions & 0 deletions problems/colorful_resistors.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,9 @@ def generate_test_case(test_type: TestCaseType) -> ProblemTestCase:
tolerance_color = str(np.random.choice(list(tolerance.keys())))
colors = [band_color1, band_color2, band_color3, multiplier_color, tolerance_color]

else:
raise ValueError(f"Unrecognized test case: {test_type}")

test_case.input['colors'] = colors

nominal_R, minimum_R, maximum_R = resistance(colors)
Expand Down
3 changes: 3 additions & 0 deletions problems/correlation_does_not_imply_causation.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,9 @@ def generate_test_case(test_type: TestCaseType) -> ProblemTestCase:
dataset_filename = "random_xy.csv"
test_case.input['DYNAMIC_RESOURCES'] = [dataset_filename]

else:
raise ValueError(f"Unrecognized test case: {test_type}")

test_case.input['x'] = x
test_case.input['y'] = y
test_case.input['dataset_filename'] = dataset_filename
Expand Down
3 changes: 3 additions & 0 deletions problems/definite_integrals.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,9 @@ def generate_test_case(test_type: TestCaseType) -> ProblemTestCase:
dx = (x2 - x1) / N
f = uniform(-1, 1, size=N).tolist()

else:
raise ValueError(f"Unrecognized test case: {test_type}")

test_case.input = {
"rectangle_heights": f,
"rectangle_width": float(dx)
Expand Down
3 changes: 3 additions & 0 deletions problems/dna_transcription.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@ def generate_test_case(test_type: TestCaseType) -> ProblemTestCase:
elif test_type == TestCaseType.INSULIN:
dna_str = "GAGAGCCACTGCATGCTGGGCCTGGCCGGCGTTGGCACCTGTGGGCACCCAGAGAGCGTGGAGAGAGCTGGGAGGGGCTCACAACAGTGCCGGGAAGTGGGGCTTGGCCCAGGGCCCCCAAGACACACAGACGGCACAGCAGGGCTGGTTCAAGGGCTTTATTCCATCTCTCTCGGTGCAGGAGGCGGCGGGTGTGGGGCTGCCTGCGGGCTGCGTCTAGTTGCAGTAGTTCTCCAGCTGGTAGAGGGAGCAGATGCTGGTACAGCATTGTTCCACAATGCCACGCTTCTGCAGGGACCCCTCCAGGGCCAAGGGCTGCAGGCTGCCTGCACCAGGGCCCCCGCCCAGCTCCACCTGCCCCACTGCCAGGACGTGCCGCGCAGAGCAGGTTCCGGAACAGCGGCGAGGCAGAGGGACACAGGAGGACACAGTCAGGGAGACACAGTGCCCGCCTGCCCGCCAGCCCTAGGTCGCACTCCCACCCATCTCCAGCCGGGCTGGACCCAGGTTAGAGGGAGGGTCACCCACACTGGGTGTGGACCTACAGGCCCCAACGCCCACATGTC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GGGTGGGGGGGTCGGCAGATGGCTGGGGGCTGAGGCTGCAATTTCCGGACCATTT"

else:
raise ValueError(f"Unrecognized test case: {test_type}")

test_case.input['dna_str'] = dna_str
test_case.output['rna_str'] = rna(dna_str)

Expand Down
3 changes: 3 additions & 0 deletions problems/earthquake_epicenters.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,9 @@ def generate_test_case(test_type: TestCaseType) -> ProblemTestCase:
r2 = r0 + d * array([cos(theta[1]), sin(theta[1])])
r3 = r0 + d * array([cos(theta[2]), sin(theta[2])])

else:
raise ValueError(f"Unrecognized test case: {test_type}")

v = PHYSICAL_CONSTANTS['v']

t1 = norm(r1-r0) / v
Expand Down
12 changes: 12 additions & 0 deletions problems/el_nino_intensities.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,29 +44,41 @@ def generate_test_case(test_type: TestCaseType) -> ProblemTestCase:

if test_type is TestCaseType.QUIET_YEAR:
year = 1996

elif test_type is TestCaseType.WEAK_EL_NINO:
year = 1948

elif test_type is TestCaseType.WEAK_LA_NINA:
year = 1871

elif test_type is TestCaseType.MODERATE_EL_NINO:
year = 1914

elif test_type is TestCaseType.MODERATE_LA_NINA:
year = 2000

elif test_type is TestCaseType.STRONG_EL_NINO:
year = 1993

elif test_type is TestCaseType.STRONG_LA_NINA:
year = 1890

elif test_type is TestCaseType.VERY_STRONG_EL_NINO:
year = 2016

elif test_type is TestCaseType.VERY_STRONG_LA_NINA:
year = 1955

elif test_type is TestCaseType.RANDOM_YEAR:
year = randint(1871, 2016)

else:
raise ValueError(f"Unrecognized test case: {test_type}")

test_case.input['year'] = year

# Delay solution import until tests can handle static resources:
# https://github.com/project-lovelace/lovelace-problems/issues/60
from problems.solutions.el_nino_intensities import enso_classification
classification, intensity = enso_classification(year)

Expand Down
25 changes: 14 additions & 11 deletions problems/flight_paths.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,72 +92,75 @@ def generate_test_case(test_type: TestCaseType) -> ProblemTestCase:
lat2 = lat1
lon2 = lon1

if test_type is TestCaseType.EQUATORIAL:
elif test_type is TestCaseType.EQUATORIAL:
lat1 = 0
lon1 = uniform(-180, 180)
lat2 = 0
lon2 = uniform(-180, 180)

if test_type is TestCaseType.POLE_TO_POLE:
elif test_type is TestCaseType.POLE_TO_POLE:
lat1 = 90
lon1 = uniform(-180, 180)
lat2 = -90
lon2 = uniform(-180, 180)

if test_type is TestCaseType.NEW_YORK_TO_MADRID:
elif test_type is TestCaseType.NEW_YORK_TO_MADRID:
lat1 = PHYSICAL_CONSTANTS['New_York_lat']
lon1 = PHYSICAL_CONSTANTS['New_York_lon']
lat2 = PHYSICAL_CONSTANTS['Madrid_lat']
lon2 = PHYSICAL_CONSTANTS['Madrid_lon']

if test_type is TestCaseType.VANCOUVER_TO_ST_JOHNS:
elif test_type is TestCaseType.VANCOUVER_TO_ST_JOHNS:
lat1 = PHYSICAL_CONSTANTS['Vancouver_lat']
lon1 = PHYSICAL_CONSTANTS['Vancouver_lon']
lat2 = PHYSICAL_CONSTANTS['St_Johns_lat']
lon2 = PHYSICAL_CONSTANTS['St_Johns_lon']

if test_type is TestCaseType.DAKAR_TO_ANTANANARIVO:
elif test_type is TestCaseType.DAKAR_TO_ANTANANARIVO:
lat1 = PHYSICAL_CONSTANTS['Dakar_lat']
lon1 = PHYSICAL_CONSTANTS['Dakar_lon']
lat2 = PHYSICAL_CONSTANTS['Antananarivo_lat']
lon2 = PHYSICAL_CONSTANTS['Antananarivo_lon']

if test_type is TestCaseType.ROME_TO_ISTANBUL:
elif test_type is TestCaseType.ROME_TO_ISTANBUL:
lat1 = PHYSICAL_CONSTANTS['Rome_lat']
lon1 = PHYSICAL_CONSTANTS['Rome_lon']
lat2 = PHYSICAL_CONSTANTS['Istanbul_lat']
lon2 = PHYSICAL_CONSTANTS['Istanbul_lon']

if test_type is TestCaseType.BENGALURU_TO_LHASA:
elif test_type is TestCaseType.BENGALURU_TO_LHASA:
lat1 = PHYSICAL_CONSTANTS['Bengaluru_lat']
lon1 = PHYSICAL_CONSTANTS['Bengaluru_lon']
lat2 = PHYSICAL_CONSTANTS['Lhasa_lat']
lon2 = PHYSICAL_CONSTANTS['Lhasa_lon']

if test_type is TestCaseType.MANAUS_TO_BANDUNG:
elif test_type is TestCaseType.MANAUS_TO_BANDUNG:
lat1 = PHYSICAL_CONSTANTS['Manaus_lat']
lon1 = PHYSICAL_CONSTANTS['Manaus_lon']
lat2 = PHYSICAL_CONSTANTS['Bandung_lat']
lon2 = PHYSICAL_CONSTANTS['Bandung_lon']

if test_type is TestCaseType.USHUAIA_TO_ALERT:
elif test_type is TestCaseType.USHUAIA_TO_ALERT:
lat1 = PHYSICAL_CONSTANTS['Ushuaia_lat']
lon1 = PHYSICAL_CONSTANTS['Ushuaia_lon']
lat2 = PHYSICAL_CONSTANTS['Alert_lat']
lon2 = PHYSICAL_CONSTANTS['Alert_lon']

if test_type is TestCaseType.EPIA_TO_PPIA:
elif test_type is TestCaseType.EPIA_TO_PPIA:
lat1 = PHYSICAL_CONSTANTS['EPIA_lat']
lon1 = PHYSICAL_CONSTANTS['EPIA_lon']
lat2 = PHYSICAL_CONSTANTS['PPIA_lat']
lon2 = PHYSICAL_CONSTANTS['PPIA_lon']

if test_type is TestCaseType.RANDOM_POINTS:
elif test_type is TestCaseType.RANDOM_POINTS:
lat1 = uniform(-90, 90)
lon1 = uniform(-180, 180)
lat2 = uniform(-90, 90)
lon2 = uniform(-180, 180)

else:
raise ValueError(f"Unrecognized test case: {test_type}")

test_case.input = {
"lat1": lat1,
"lon1": lon1,
Expand Down
3 changes: 3 additions & 0 deletions problems/game_of_life.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,9 @@ def generate_test_case(test_type: TestCaseType) -> ProblemTestCase:
steps = randint(3, 10)
grid = choice([ALIVE, DEAD], N*M, p=[0.5, 0.5]).reshape(N, M)

else:
raise ValueError(f"Unrecognized test case: {test_type}")

test_case.input['board'] = grid.tolist()
test_case.input['steps'] = steps

Expand Down
3 changes: 3 additions & 0 deletions problems/habitable_exoplanets.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,9 @@ def generate_test_case(test_type: TestCaseType) -> ProblemTestCase:
L_star = float(uniform(0.1, 5.0, 1)[0])
r = float(uniform(0.1, 5.0, 1)[0])

else:
raise ValueError(f"Unrecognized test case: {test_type}")

test_case.input['L_star'] = L_star
test_case.input['r'] = r
test_case.output['habitability'] = habitable_exoplanet(L_star, r)
Expand Down
8 changes: 7 additions & 1 deletion problems/molecular_mass_calculator.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,15 @@ def generate_test_case(test_type: TestCaseType) -> ProblemTestCase:
elif test_type is TestCaseType.RANDOM_CHEMICAL:
chemical_formula = choice(["CO2", "CH4", "C6H12O6", "PuCoGa5", "CH3NH2", "W", "C2H5OH"], 1)[0]

test_case.input['chemical_formula'] = chemical_formula
else:
raise ValueError(f"Unrecognized test case: {test_type}")


# Delay solution import until tests can handle static resources:
# https://github.com/project-lovelace/lovelace-problems/issues/60
from problems.solutions.molecular_mass_calculator import molecular_mass

test_case.input['chemical_formula'] = chemical_formula
test_case.output['mass'] = molecular_mass(chemical_formula)

return test_case
3 changes: 3 additions & 0 deletions problems/nand_gate.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@ def generate_test_case(test_type: TestCaseType) -> ProblemTestCase:
elif test_type is TestCaseType.ONE_ONE:
p, q = 1, 1

else:
raise ValueError(f"Unrecognized test case: {test_type}")

test_case.input["p"], test_case.input["q"] = p, q
test_case.output["nand"] = NAND(p, q)

Expand Down
9 changes: 6 additions & 3 deletions problems/rock_star_climate.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,21 +72,24 @@ def generate_test_case(test_type: TestCaseType) -> ProblemTestCase:
a = 1
ε = PHYSICAL_CONSTANTS['ε_Earth']

if test_type is TestCaseType.MARS:
elif test_type is TestCaseType.MARS:
S = PHYSICAL_CONSTANTS['S_Mars']
a = PHYSICAL_CONSTANTS['a_Mars']
ε = PHYSICAL_CONSTANTS['ε_Mars']

if test_type is TestCaseType.PLUTO:
elif test_type is TestCaseType.PLUTO:
S = PHYSICAL_CONSTANTS['S_Pluto']
a = PHYSICAL_CONSTANTS['a_Pluto']
ε = PHYSICAL_CONSTANTS['ε_Pluto']

if test_type is TestCaseType.RANDOM:
elif test_type is TestCaseType.RANDOM:
S = uniform(1000, 10000)
a = uniform(0, 1)
ε = uniform(0, 1)

else:
raise ValueError(f"Unrecognized test case: {test_type}")

test_case.input = {'solar_constant': S, 'albedo': a, 'emissivity': ε}
test_case.output['T_rock'] = rock_temperature(S, a, ε)

Expand Down
3 changes: 3 additions & 0 deletions problems/rocket_science.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,9 @@ def generate_test_case(test_type: TestCaseType) -> ProblemTestCase:
elif test_type is TestCaseType.RANDOM:
v = float(uniform(1.0, 100.0, 1)[0])

else:
raise ValueError(f"Unrecognized test case: {test_type}")

test_case.input['v'] = v
test_case.output['m_fuel'] = rocket_fuel(v)

Expand Down
3 changes: 3 additions & 0 deletions problems/scientific_temperatures.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,9 @@ def generate_test_case(test_type: TestCaseType) -> ProblemTestCase:
elif test_type is TestCaseType.SUN_SURFACE:
F = 9940.73

else:
raise ValueError(f"Unrecognized test case: {test_type}")

test_case.input['F'] = F
test_case.output['C'] = fahrenheit_to_celsius(F)
return test_case
3 changes: 3 additions & 0 deletions problems/sha_256.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ def generate_test_case(test_type: TestCaseType) -> ProblemTestCase:
elif test_type is TestCaseType.QUICK_BROWN_FOX:
M = "The quick brown fox jumps over the lazy dog"

else:
raise ValueError(f"Unrecognized test case: {test_type}")

test_case.input['message'] = M
test_case.output['digest'] = SHA256(M)

Expand Down
3 changes: 3 additions & 0 deletions problems/speed_of_light.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,9 @@ def generate_test_case(test_type: TestCaseType) -> ProblemTestCase:
c = PHYSICAL_CONSTANTS['c']
distance = uniform(c, 60*c)

else:
raise ValueError(f"Unrecognized test case: {test_type}")

test_case.input['distance'] = distance
test_case.output['time'] = light_time(distance)

Expand Down
3 changes: 3 additions & 0 deletions problems/temperature_variations.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,9 @@ def generate_test_case(test_type: TestCaseType) -> ProblemTestCase:
elif test_type is TestCaseType.SAN_JUAN:
T = PHYSICAL_CONSTANTS['T']['San Juan, Argentina']

else:
raise ValueError(f"Unrecognized test case: {test_type}")

test_case.input['T'] = T
test_case.output['T_avg'], test_case.output['T_std'] = temperature_statistics(T)

Expand Down
Loading