Skip to content

Commit cd6f6aa

Browse files
Add files via upload
1 parent 205b024 commit cd6f6aa

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

51 files changed

+8473
-0
lines changed
Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"metadata": {},
6+
"source": [
7+
"# Josephus Problem"
8+
]
9+
},
10+
{
11+
"cell_type": "markdown",
12+
"metadata": {},
13+
"source": [
14+
"The Josephus Problem is a counting problem, named after a historical event described by Flavius Josephus.\n",
15+
"In the original form, 42 objects are placed in a circle; and starting from Object #1, every 3d object is removed.\n",
16+
"We want to preserve the order in which the objects are removed; more specifically, \n",
17+
"we want to know which objects are the second-to-last and last are removed."
18+
]
19+
},
20+
{
21+
"cell_type": "code",
22+
"execution_count": null,
23+
"metadata": {},
24+
"outputs": [],
25+
"source": [
26+
"list(range(1,42))"
27+
]
28+
},
29+
{
30+
"cell_type": "code",
31+
"execution_count": null,
32+
"metadata": {},
33+
"outputs": [],
34+
"source": [
35+
"# pseudo-code\n",
36+
"\n",
37+
"# create three lists, one for circle_of_objects, one for removed_objects, and one for the skip\n",
38+
"circle_of_objects, removed_objects, skip = range(1,42), [], range(3)\n",
39+
"\n",
40+
"# while circle_of_objects == True\n",
41+
"while circle_of_objects:\n",
42+
"\n",
43+
" # for i <- skip-list reversed\n",
44+
" for i in skip[::-1]:\n",
45+
"\n",
46+
" # dequeue the next object\n",
47+
"\n",
48+
" # the logical structure of the following\n",
49+
" # if-else clause is not pythonic.\n",
50+
" # in python, we prefer: if someCollection:\n",
51+
" # meaning \"True as long as someCollection\n",
52+
" # contains an item\"\n",
53+
" # let's refactor to clause.\n",
54+
" \n",
55+
" # if i == 0\n",
56+
"\n",
57+
" # append next object to removed_objects list\n",
58+
" \n",
59+
" # else\n",
60+
" \n",
61+
" # append next object to circle_of_objects list\n",
62+
" \n",
63+
" \n",
64+
"# print removed_objects list\n",
65+
"removed_objects"
66+
]
67+
},
68+
{
69+
"cell_type": "code",
70+
"execution_count": null,
71+
"metadata": {},
72+
"outputs": [],
73+
"source": [
74+
"for i in skip[::-1]:\n",
75+
" print(i)"
76+
]
77+
},
78+
{
79+
"cell_type": "code",
80+
"execution_count": 7,
81+
"metadata": {},
82+
"outputs": [
83+
{
84+
"data": {
85+
"text/html": [
86+
"\n",
87+
" <iframe\n",
88+
" width=\"950\"\n",
89+
" height=\"800\"\n",
90+
" src=\"https://mathworld.wolfram.com/images/eps-gif/Josephus41-3_1000.gif\"\n",
91+
" frameborder=\"0\"\n",
92+
" allowfullscreen\n",
93+
" ></iframe>\n",
94+
" "
95+
],
96+
"text/plain": [
97+
"<IPython.lib.display.IFrame at 0x7f4f78309a90>"
98+
]
99+
},
100+
"execution_count": 7,
101+
"metadata": {},
102+
"output_type": "execute_result"
103+
}
104+
],
105+
"source": [
106+
"from IPython.display import IFrame\n",
107+
"IFrame('https://mathworld.wolfram.com/images/eps-gif/Josephus41-3_1000.gif', width=950, height=800)"
108+
]
109+
},
110+
{
111+
"cell_type": "code",
112+
"execution_count": null,
113+
"metadata": {},
114+
"outputs": [],
115+
"source": [
116+
"from IPython.display import IFrame\n",
117+
"IFrame('https://en.wikipedia.org/wiki/Josephus_problem', width=950, height=800)"
118+
]
119+
}
120+
],
121+
"metadata": {
122+
"kernelspec": {
123+
"display_name": "Python 3 (ipykernel)",
124+
"language": "python",
125+
"name": "python3"
126+
},
127+
"language_info": {
128+
"codemirror_mode": {
129+
"name": "ipython",
130+
"version": 3
131+
},
132+
"file_extension": ".py",
133+
"mimetype": "text/x-python",
134+
"name": "python",
135+
"nbconvert_exporter": "python",
136+
"pygments_lexer": "ipython3",
137+
"version": "3.12.5"
138+
}
139+
},
140+
"nbformat": 4,
141+
"nbformat_minor": 4
142+
}

week_12_1/codon2amino.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import pickle
2+
import os
3+
def get_amino_acid(nuclStr: str, codon2amino: dict=None, path_to_pickled_dict: str=''):
4+
''' Requires either codon2amino dict passed into function,
5+
or path to pickled codon2amino dict file passed into it.
6+
Returns tuple of amino-acid strings for three reading frames.
7+
>>>get_amino_acid('GATTACAGATTACA', codon2amino)
8+
('AspTyrArgLeu', 'IleThrAspTyr', 'LeuGlnIleThr')
9+
'''
10+
# garbage filters
11+
# to do: garbage filter to ensure nuclStr is clean
12+
assert codon2amino or os.path.isfile(path_to_pickled_dict),\
13+
"function must have dict passed into it, or dict must exist to pickle dir"
14+
if not codon2amino and os.path.isfile(path_to_pickled_dict):
15+
with open(path_to_pickled_dict, 'rb') as inFile:
16+
codon2amino = pickle.load(inFile)
17+
# implementation of algorithm
18+
# assuming nuclStr is clean AND
19+
# dict may be incomplete or have bad items
20+
# initialization
21+
nuclStr = nuclStr.upper()
22+
rf0, rf1, rf2 = '', '', ''
23+
24+
for index, value in enumerate(nuclStr[:-2]):
25+
remainder = index % 3
26+
codon = nuclStr[index:index+3]
27+
try:
28+
amino = codon2amino[codon]
29+
except KeyError as e:
30+
raise Exception(f"Codon {codon} not found in codon2amino dict.\n" \
31+
f"\tPlease check dictionary for item and codon used as key.") \
32+
from e
33+
if remainder == 0:
34+
rf0 = rf0 + amino
35+
elif remainder == 1:
36+
rf1 = rf1 + amino
37+
else:
38+
rf2 = rf2 + amino
39+
return rf0, rf1, rf2

week_12_1/creating_edge_list.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# first, we read the contents
2+
# of the hewitsoni_males datafile
3+
# into an LoL in session memory
4+
with open('datafile/hewitsoni_males.txt') as inFile:
5+
LoS = inFile.read().splitlines()
6+
LoL = [] # initialization
7+
for record in LoS:
8+
LoL.append(record.split())
9+
10+
# then we write out an edge list from the LoL
11+
with open('datafile/edge_list', 'w') as outFile:
12+
# we shorten the range object by 1
13+
# to prevent an IndexError
14+
for index in range(len(LoL)-1):
15+
if LoL[index][0] == LoL[index+1][0]:
16+
edge_record = (f"{LoL[index][0]}\t"
17+
f"{LoL[index][1]}\t"
18+
f"{LoL[index+1][1]}\n")
19+
outFile.write(edge_record)
20+

week_12_1/find_overlaps.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
with open('datafile/finding_interval_overlaps/exon.bed', 'r') as exonFile,\:
2+
with open('datafile/finding_interval_overlaps/test.sort.bed', 'r') as testFile:
3+
exonLoS, testLoS = exonFile.read().splitlines(),\
4+
testFile.read().splitlines()
5+
exonLoL, testLoL = [], []
6+
for exonRec in exonLoS:
7+
exonLoL.append(exonRec.split('\t'))
8+
for testRec in testLoS:
9+
LoL.append(testRec.split())
10+
11+
with open('overlaps', 'w') as outFile:
12+
for testRec in testLoL:
13+
for exonRec in exonLoL:
14+
if testRec[0] == exonRec[0]\
15+
and len(testRec) > 3\
16+
and range(max(int(testRec[1]), int(exonRec[1])),\
17+
min(int(testRec[2]), int(exonRec[2]))+1):
18+
overlap = range(max(int(testRec[1]), int(exonRec[1])),\
19+
min(int(testRec[2]), int(exonRec[2]))+1)
20+
record = (f"{exonRec[0]}\t{overlap[0]}\t"
21+
f"{overlap[-1]}\t{exonRec[3]}\t"
22+
f"{testRec[3]}\n")
23+
outFile.write(record)
24+
25+

week_12_1/hi_lo_partial

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
#!/usr/bin/python3
2+
3+
# we assume a fair player.
4+
5+
import math
6+
7+
# in this interactive while loop, the script explains the game, gets highest integer of range from user;
8+
# and initializes floor and ceiling variables
9+
while True:
10+
print("in this game you give me a range of integers from 1 to some number you choose." + '\n' +
11+
"then you will pick a number within this range for me to guess." + '\n' +
12+
"i will try to guess your number in the fewest number of tries." + '\n' +
13+
"for each guess, you will tell me if your number is higher than (h)," + '\n' +
14+
"lower than (l), or equal to (=) your number. ready?" + '\n')
15+
16+
floor = 1
17+
ceiling = input("please enter the highest number of your range: ")
18+
ceiling = int(ceiling)
19+
20+
# remember that the 'break' statement lets the program counter
21+
# leave the innermost loop structure that it's in.
22+
# that how we 'break out' of the infinite loop.
23+
# 'continue' is related to 'break' ---
24+
# it returns the program counter to the top of the while loop.
25+
if not isinstance(ceiling, int) and\
26+
not ceiling > 1:
27+
print("that's not a legitimate value. please enter a counting number greater than 1.")
28+
continue
29+
else:
30+
break
31+
# print the challenge of fewest guesses using this algorithm
32+
# use math.ceil() and math.log() functions
33+
34+
print(f"i can guess your number in at most {nguesses} guesses!\n")
35+
# initalize the guess counter
36+
guessCnt = 0
37+
while True:
38+
guessCnt = guessCnt + 1
39+
guess = (floor + ceiling)//2
40+
41+
answer = input("i guess " + str(guess) + "! Is your number higher (h), lower (l), or equal (=)? ")
42+
43+
# RUMPELSTILTSKIN LINES
44+
# several if-statements, each processing different user input
45+
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
gawk -F"\t" '{print $2}' datafile/edge_list | sort | uniq -c | sort -n | tail
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
gawk -F"\t" '{print $3}' datafile/edge_list | sort | uniq -c | sort -n | tail
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
gawk -F"\t" '{print $1}' datafile/hewitsoni_males.txt | uniq -c | sort -n | tail
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
gawk -F"\t" '{print $2}' datafile/hewitsoni_males.txt | uniq -c | sort -n | tail
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# returns the number of specimens that made it into edge list
2+
gawk -F"\t" '{print $1}' "$HOME"/datafile/edge_list |# extract column 1
3+
uniq |# return a single instance of each contiguous identical group of specimens
4+
wc -l # return the number of specimens

0 commit comments

Comments
 (0)