This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)}') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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: ')) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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('Elements are stacked at position A, which is \'home\'.\n \ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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): |