added ws202425 courses

This commit is contained in:
2024-11-14 13:11:04 +01:00
parent 800282c2e8
commit beb31897b1
461 changed files with 20368 additions and 0 deletions
+58
View File
@@ -0,0 +1,58 @@
import csv
import sys
def main():
# TODO: Check for command-line usage
# TODO: Read database file into a variable
# TODO: Read DNA sequence file into a variable
# TODO: Find longest match of each STR in DNA sequence
# TODO: Check database for matching profiles
return
def longest_match(sequence, subsequence):
"""Returns length of longest run of subsequence in sequence."""
# Initialize variables
longest_run = 0
subsequence_length = len(subsequence)
sequence_length = len(sequence)
# Check each character in sequence for most consecutive runs of subsequence
for i in range(sequence_length):
# Initialize count of consecutive runs
count = 0
# Check for a subsequence match in a "substring" (a subset of characters) within sequence
# If a match, move substring to next potential match in sequence
# Continue moving substring and checking for matches until out of consecutive matches
while True:
# Adjust substring start and end
start = i + count * subsequence_length
end = start + subsequence_length
# If there is a match in the substring
if sequence[start:end] == subsequence:
count += 1
# If there is no match in the substring
else:
break
# Update most consecutive matches found
longest_run = max(longest_run, count)
# After checking for runs at each character in seqeuence, return longest run found
return longest_run
main()
+1
View File
@@ -0,0 +1 @@
AAGGTAAGTTTAGAATATAAAAGGTGAGTTAAATAGAATAGGTTAAAATTAAAGGAGATCAGATCAGATCAGATCTATCTATCTATCTATCTATCAGAAAAGAGTAAATAGTTAAAGAGTAAGATATTGAATTAATGGAAAATATTGTTGGGGAAAGGAGGGATAGAAGG
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
+1
View File
@@ -0,0 +1 @@
CGCAAAGACTTTATTGCGCCCACAGTGGCTTTTGTCTACTGATTCCATAGATAATGACAAATGTTCAAGGGGTTCTGGTACTTAGTCCGATCTCAGTGCGACTCGGGGGCGAACGTCGTGGTTATAAACTCGTCCAGATGCCGGCGCCAAACAAATATGATCCCATTGTGCACCCCCACTGGTCAGAACTCTCGGTGCTTAAGCGATACACGCGTCCGTGAGCATTCAACACCCAACTACTAGTCCGGTAATCTGAATGCACACGTGGCCCGGGTTACCGGGATGCCGAAAGAAAGAAAG
File diff suppressed because one or more lines are too long
+1
View File
@@ -0,0 +1 @@
AGAAAGTGATGAGGGAGATAGTTAGGAAAAGGTTAAATTAAATTAAGAAAAATTATCTATCTATCTATCTATCAAGATAGGGAATAATGGAGAAATAAAGAAAGTGGAAAAAGATCAGATCAGATCTTTGGATTAATGGTGTAATAGTTTGGTGATAAAAGAGGTTAAAAAAGTATTAGAAATAAAAGATAAGGAAATGAATGAATGAGGAAGATTAGATTAATTGAATGTTAAAAGTTAA
+1
View File
@@ -0,0 +1 @@
GGGGAATATGGTTATTAAGTTAAAGAGAAAGAAAGATGTGGGTGATATTAATGAATGAATGAATGAATGAATGAATGAATGTTATGATAGAAGGATAAAAATTAAATAAAATTTTAGTTAATAGAAAAAGAATATATAGAGATCAGATCTATCTATCTATCTTAAGGAGAGGAAGAGATAAAAAAATATAATTAAGGAA
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
@@ -0,0 +1,38 @@
# TODO
quarters = 0.25
dimes = 0.10
nickels = 0.05
pennies = 0.01
coins = 0
print('Change owed: ')
while True:
try:
change = float(input())
if change >= 0:
break
else:
print("Change must be greater then 0: ")
except ValueError:
print("Change must be a numeric value: ")
while change > 0:
if change >= quarters:
change -= quarters
coins += 1
elif change >= dimes:
change -= dimes
coins += 1
elif change >= nickels:
change -= nickels
coins += 1
elif change >= pennies:
change -= pennies
coins += 1
change = round(change, 2)
print(coins)
@@ -0,0 +1,4 @@
# TODO
print('What is your name?')
name = input()
print(f'hello, {name}')
@@ -0,0 +1,14 @@
# TODO
print("Height: ")
while True:
try:
size = int(input())
if 1 <= size <= 8:
break
else:
print("Height between 1 and 8: ")
except ValueError:
print("Height with numeric value: ")
for i in range(1, size+1):
print(((size - i) * ' ') + (i * '#'))
@@ -0,0 +1,31 @@
# TODO
print('Text: ')
text = str(input())
lettercount = 0
wordcount = 0
sentencecount = 0
for i in range(len(text)):
if (65 <= ord(text[i]) <= 90) or (97 <= ord(text[i]) <= 122):
lettercount += 1
elif ord(text[i]) == 32:
wordcount += 1
elif ord(text[i]) in [33, 63, 46]:
sentencecount += 1
else:
continue
wordcount += 1
L = lettercount / wordcount * 100
S = sentencecount / wordcount * 100
index = 0.0588 * L - 0.296 * S - 15.8
if index <= 1:
print("Before Grade 1")
elif index >= 16:
print("Grade 16+")
else:
print(f"Grade {round(index)}")
+19
View File
@@ -0,0 +1,19 @@
Times:
10 simulations: 0m0.021s
100 simulations: 0m0.027s
1000 simulations: 0m0.040s
10000 simulations: 0m0.084s
100000 simulations: 0m0.728s
1000000 simulations: 0m6.556s
Questions:
Which predictions, if any, proved incorrect as you increased the number of simulations?:
With a small number of simulations.
Suppose you're charged a fee for each second of compute time your program uses.
After how many simulations would you call the predictions "good enough"?: TODO
Predictions stabilized over 10000 simulations.
+69
View File
@@ -0,0 +1,69 @@
# Simulate a sports tournament
import csv
import sys
import random
# Number of simluations to run
N = 1000
def main():
# Ensure correct usage
if len(sys.argv) != 2:
sys.exit("Usage: python tournament.py FILENAME")
teams = []
counts = {}
# TODO: Read teams into memory from file
with open(sys.argv[1], 'r') as csvfile:
reader = csv.DictReader(csvfile)
for row in reader:
teams.append({'team': row['team'], 'rating': int(row['rating'])})
counts[row['team']] = 0
# TODO: Simulate N tournaments and keep track of win counts
for _ in range(N):
winner = simulate_tournament(teams.copy())
counts[winner] += 1
# Print each team's chances of winning, according to simulation
for team in sorted(counts, key=lambda team: counts[team], reverse=True):
print(f"{team}: {counts[team] * 100 / N:.1f}% chance of winning")
def simulate_game(team1, team2):
"""Simulate a game. Return True if team1 wins, False otherwise."""
rating1 = team1["rating"]
rating2 = team2["rating"]
probability = 1 / (1 + 10 ** ((rating2 - rating1) / 600))
return random.random() < probability
def simulate_round(teams):
"""Simulate a round. Return a list of winning teams."""
winners = []
# Simulate games for all pairs of teams
for i in range(0, len(teams), 2):
if simulate_game(teams[i], teams[i + 1]):
winners.append(teams[i])
else:
winners.append(teams[i + 1])
return winners
def simulate_tournament(teams):
"""Simulate a tournament. Return name of winning team."""
# TODO
while len(teams) > 1:
teams = simulate_round(teams)
return teams[0]['team']
if __name__ == "__main__":
main()