Skip to content

Commit

Permalink
hw3
Browse files Browse the repository at this point in the history
  • Loading branch information
betich committed Nov 5, 2022
1 parent 5940197 commit d8e114b
Show file tree
Hide file tree
Showing 1,244 changed files with 13,999 additions and 463 deletions.
Binary file modified .DS_Store
Binary file not shown.
26 changes: 26 additions & 0 deletions br/Q1.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import math


def time_diff(h1, m1, s1, h2, m2, s2):
hour, min, sec = "", "", ""

return hour, min, sec


def f(x, y):
numerator = (
x ** 2 + (math.e ** (1 / (math.sin(2 * math.pi * y))))) ** (1/3)
denominator = math.log(x ** (1 / 2), 2)

# round the input to 3 decimal places


def shift_right(list_A, list_B):
"""
list A = [1,2,3,4]
list B = [a,b,c]
[a,b,c,1]
"""

pass
11 changes: 11 additions & 0 deletions br/Q2_1.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
def something(s, k):
"""
split the string s
k = 1 : return all numbers in the array
k = 2 : return all strings in the array
returns ["list", "of, "words"]
or ["1", "4", "6"]
"""
pass
16 changes: 16 additions & 0 deletions br/Q2_2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
def flow_chart(a):
"""
write from flow chart
"""
l1, l2, l3 = [], [], []

i = 0
while i < len(a):
if a[i] % 2 == 0:
l1.append(a[i])
elif a[i] % 3 == 0:
l2.append(a[i])
else:
l3.append(a[i])

return [l1, l2, l3]
38 changes: 38 additions & 0 deletions br/Q3.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# file input

def split_words(filename):
"""
read the file, clean the words (only alnums), convert to lowercase and return them as an array
returns ["the", "clean", "words"]
"""

pass


def get_word_frequency(w):
"""
returns the word frequencies, sorted by descending order of the frquencies and character order
returns [["the", 2], ["a", 1], ["words", 1]]
"""
pass


def cut_threshold(w, thresh=2):
"""
returns the word frequency array, but cut by a threshold
input
[["the", 2], ["a", 1], ["words", 1]]
1
returns
[["the", 2]]
"""
pass


def main():
# call all functions
pass
2 changes: 2 additions & 0 deletions br/Q4.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# bwt econding
# https://en.wikipedia.org/wiki/Burrows%E2%80%93Wheeler_transform
Binary file added exam preparation/.DS_Store
Binary file not shown.
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# TODO Provide more examples and explanations

# String

s = "Hello"
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# TODO Provide more advanced examples and explanations

# Repetition

# 'While' loop
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# TODO Provide more examples and explanations


# Open a File

Expand Down Expand Up @@ -108,6 +110,7 @@

f3 = open("result.txt", "w")

b = [" ".join(i) for i in a] # ["6210202002 22", "6210202002 22", "6210202002 22"]
# ["6210202002 22", "6210202002 22", "6210202002 22"]
b = [" ".join(i) for i in a]

f3.write("\n".join(b)) # "6210202002 22\n6210202002 22\n6210202002 22"
f3.write("\n".join(b)) # "6210202002 22\n6210202002 22\n6210202002 22"
File renamed without changes.
172 changes: 172 additions & 0 deletions exam preparation/08 - dictionary.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,172 @@
# Dictionary

# Dictionaries are used to store data values in key:value pairs.

# =================================

# Accessing data from a dictionry

# A dictionary is kinda like a list
# For a list, you use an index to get a value

import time
a = [1, 2, 3, 4]
print(a[1]) # 2

# For a dictionary, you use a key to get a value

a = {"one": 1, "two": 2, "three": 3, "four": 4}
print(a["one"]) # 1


grades = {"6130186221": "A", "6230221221": "A",
"6231009821": "B", "6230543921": "C",
"6230431521": "B", "6230276821": "A"}

print(grades["6130186221"]) # A

# You can also use a variable as a key

a = {"one": 1, "two": 2, "three": 3, "four": 4}
key = "one"
print(a[key]) # 1

# =================================

# Assigning data to a dictionary
a = {"one": 1, "two": 2, "three": 3, "four": 4}
a["five"] = 5

b = "six"
a[b] = 6

# =================================

# Keys and Values can be any type of data

my_dict = {"weather": "sunny", "temperature": 25, "wind": "calm",
"humidity": 0.5, "id101": [1, 2, 3, 4], 101: 100, }


# But floating point keys are not encouraged

my_dict = {0.1: 'a', 0.2: 'b', 0.3: 'c', 0.4: 'd'}
k = 0.1
print(my_dict[k]) # a
k += 0.1 # 0.2
print(my_dict[k]) # b
k += 0.1 # 0.30000000000000001
print(my_dict[k]) # erro

# =================================

# Checking if k is in a dictionary
# Used for checking if a key is in a dictionary

a = {"one": 1, "two": 2, "three": 3, "four": 4}
print("one" in a) # True
print("five" in a) # False

# =================================

# Example: Checking if id is in dictionary


def read_data():
x = input().split(", ")
d = {"ID": x[0],
"Name": x[1],
"Birthdate": [int(x[2]), int(x[3]), int(x[4])]}
return d


student1 = {"ID": "5830000021",
"Name": "Pranpriya M.",
"Birthdate": [27, 3, 1997]}
student2 = read_data()
print(student2)
# =================================


# Looping over a dictionary
# You can loop over a dictionary using a for loop

a = {"one": 1, "two": 2, "three": 3, "four": 4}
for key in a:
print(key, a[key])

# OUTPUT
# one 1
# two 2
# three 3
# four 4

# =================================

# dict.keys(), dict.values(), dict.items()
# Returns a list of keys, values, or key:value pairs

a = {"one": 1, "two": 2, "three": 3, "four": 4}
print(a.keys()) # ['one', 'two', 'three', 'four']
print(a.values()) # [1, 2, 3, 4]
print(a.items()) # [('one', 1), ('two', 2), ('three', 3), ('four', 4)]


# You can also loop over a dictionary using the items() method

a = {"one": 1, "two": 2, "three": 3, "four": 4}
for key, value in a.items(): # destructing from a list
print(key, value)

# =================================

# Bonus

# looping over a list of sorted keys

a = {"one": 1, "two": 2, "three": 3, "four": 4}

for key in sorted(a.keys()):
print(key, a[key])


# sorting dictionary.items() by value

a = {"one": 1, "two": 2, "three": 3, "four": 4}
for v, k in sorted([(v, k) for k, v in a.items()]):
print(v, k)

# =================================

# Bonus: Dictionary Comprehension

source = [("one", 1), ("two", 2), ("three", 3), ("four", 4)]
d = {k: v for k, v in source}

# =================================

# Example: Dict access time


def search_all(X):
b = time.time()
n = len(X)
for i in range(n):
if i in X: # True
pass
for i in range(n):
if (n+1) in X: # False
pass
print(time.time() - b)


n = 10000
L = []
for i in range(n):
L.append(i)
D = {}
for i in range(n):
D[i] = i

search_all(L)
search_all(D)
Loading

0 comments on commit d8e114b

Please sign in to comment.