Skip to content

Instantly share code, notes, and snippets.

View msyvr's full-sized avatar
🎯
Focusing

Monica Spisar msyvr

🎯
Focusing
View GitHub Profile
@msyvr
msyvr / lyric_freqs.py
Created October 18, 2021 00:53
mit 6.0001 dictionaries - lyric frequencies: id the most frequent word
def lyric_frequencies(lyrics_db):
'''
create a dictionary with
keys = unique lyrics (words)
vals = number of occurrences in the lyrics db
'''
lyrics_dict = {}
# loop through lyrics_db elements
for lyric in lyrics_db:
if lyric in lyrics_dict:
@msyvr
msyvr / pal_rec.py
Last active October 17, 2021 22:22
mit 6.0001 - recursive check for palindrome
def pal_rec(s):
'''
check if the alphabetical characters in a string read the same backwards and forewards
'''
# convert the input string to a list of lowercase alphanumeric characters only (i.e., punctuation, etc, omitted)
def to_char(s):
ls = list(s.lower())
labc09 = list('abcdefghijklmnopqrstuvwxyz0123456789')
char_s = [i for i in ls if i in labc09]
return char_s
@msyvr
msyvr / pal.py
Last active October 17, 2021 20:27
palindrome check - compare first half and reversed second half; this works on any input (not just alphanumeric)
def pal(user_string):
'''
check if a word/phrase is a palindrome
'''
lus = list(user_string)
max_ind = int(len(lus)/2)
l1 = [lus[i] for i in range(max_ind)]
l2 = [lus[-(i+1)] for i in range(max_ind)]
if l1 == l2:
return True
@msyvr
msyvr / ispowerof.py
Created October 15, 2021 23:16
Given a base, is a number a power of that base?
def ispowerof(n, base):
'''
if n is a power of b, return True, else return False
'''
test = False
if n % base == 0:
k = int(n / base)
for i in range(k+1):
print(i)
print(base**i)
@msyvr
msyvr / fibo.py
Last active October 15, 2021 23:17
mit 6.0001 - recursion - compute the nth term of the Fibonacci series
def fibo(n):
'''return element 'i' of the Fibonacci series (its initial element is element '1')'''
if n == 0 or n == 1:
return 1
elif n > 1:
return fibo(n-2) + fibo(n-1)
if __name__ == "__main__":
i = int(input('Which element to compute in the Fibonacci sequence?: '))
print(f'Element {i} in the Fibonacci sequence: {fibo(i-1)}')
@msyvr
msyvr / additup.py
Last active October 13, 2021 06:32
recursion - sum recursively, no loops
def additup(num_list):
''' function that returns the sum of the passed list - without using loops; eg:
sum([1, 2, 3, 4, 5]) -> 15
sum([]) -> 0
'''
if len(num_list) == 1:
return num_list[0]
else:
return num_list[len(num_list)-1] + additup(num_list[0:len(num_list)-1])
@msyvr
msyvr / highest_index.py
Created October 13, 2021 06:30
recursion: find the highest index for a number in a list
def hi_index(n, l):
if l[-1] == n:
return len(l)-1
else:
return hi_index(n, l[0:len(l)-1])
if __name__ == "__main__":
l = [3, 5, 4, 3, 4, 9, 5, 7, 7, 9]
print(f'given the list {l}\n')
n = int(input('pick a number to find its largest index: '))
@msyvr
msyvr / towers_rec.py
Last active November 24, 2021 05:37
mit 6.0001 recursion - towers of hanoi
def towers_rec(n, home, destination, temp):
if n == 1:
moves.append((home+' to '+destination))
else:
towers_rec(n-1, home, temp, destination)
towers_rec(1, home, destination, temp)
towers_rec(n-1, temp, destination, home)
if __name__ == "__main__":
print(f'Elements are stacked at position A, which is \'home\'.\nMove them to position B, which is \'destination\'.\nImportant! The home stack is ranked as:\nbottom biggest, top smallest...\nand the destination stack must be ranked the same way.\nThere\'s one spare position you can use called \'temp\' which starts at position C.\nTwo rules:\n1. move one element at a time, and\n2. never place an element on top of a smaller element.')
@msyvr
msyvr / recursion_fun.py
Last active October 12, 2021 21:50
mit 6.0001 recursion - multiplication and factorial implemented via recursion (vs built in '*' or 'math.factorial()')
def mult_rec(x, y):
if y == 1:
return x
else:
return x + mult_rec(x, y-1)
def fact_rec(x):
if x == 1:
return x
elif x > 1:
@msyvr
msyvr / pylogging.py
Last active October 4, 2021 23:36
python logger - from socratica https://www.socratica.com/lesson/logging
import logging
import math #since we'll need the sqrt fn
# Create and configure logger
LOG_FORMAT = "%(levelname)s %(asctime)s - %(message)s"
logging.basicConfig(filename = "log_file.log", level = logging.DEBUG, format = LOG_FORMAT, filemode = 'w')
# we'll call the logger without a name (aka 'root logger') because there are subtle issues with logger names (*what are they?*)
logger = logging.getLogger()
def quadratic_formula(a, b, c):