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
Last active December 31, 2024 04:16
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 / towers_rec.py
Last active December 31, 2024 04:07
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('Elements are stacked at position A, which is \'home\'.\n \
@msyvr
msyvr / fib_eff.py
Last active December 31, 2024 04:02
mit 6.0001 Fibonacci - dynamic programming
def fib_eff(n, dict):
'''
performace-aware recursive algorithm:
keep track of computed Fibonacci terms in a dictionary;
limit computation to terms not in the dictionary
'''
if n in dict:
return dict[n]
else:
fibn = fib_eff(n-1, dict) + fib_eff(n-2, dict)
@msyvr
msyvr / bubble_sort.py
Last active December 31, 2024 04:01
mit 6.0001 - bubble sort
import random
def bubble_sorted_list(ulist):
'''
sort ulist by pairwise checks (aka BUBBLE SORT):
pairwise comparisons from start (index 0) to end (index n),
shifting higher values toward the end
'''
swap = True
while swap:
@msyvr
msyvr / selection_sort.py
Last active December 31, 2024 03:59
mit 6.0001 - selection sort
import random
def selection_sorted_list(ulist):
'''
sort a list recursively by shifting the minimum value of the portion of the
list to the right of a comparator index to the comparator index; the index
increases by 1 on each pass up to len(list)
'''
uoindex = 0
while uoindex != len(ulist):
@msyvr
msyvr / rails 5 asset pipeline management: fonts
Last active August 15, 2023 22:53
Getting custom fonts to work for Rails 5 app deployed to Heroku.
I deployed to Heroku and custom fonts didn't work. Details of my starting point below.
Fixed in 2 steps:
1. In application.rb file, in class Application < Rails::Application, added:
config.assets.paths << Rails.root.join("app","assets","fonts")
2. In application.scss file, in @font-face, changed src: url('...') to font-url('...'); e.g.:
src: font-url('Aileron/Aileron-Regular.otf') format('opentype');
Redeployed to Heroku and custom fonts worked.
---
Starting point details (this all worked for a local deploy, but not when deployed on Heroku):
@msyvr
msyvr / pysets.py
Last active December 17, 2021 16:56
Python sets: unique, unsorted, unchangeable
def setpy():
"""
python sets are collections of objects which are:
unique
unordered
unchangeable*
* where unchangeable means individual items can't be replaced in place, but items can be removed and new items added
"""
# unique and unsorted
@msyvr
msyvr / wordguess.py
Last active November 25, 2021 07:06
hangman: guess the mystery word
# select letters one at a time to guess mystery word
import string
import random
from words_json import words
def get_valid_word(words):
word = random.choice(words)
while '-' in word or ' ' in word:
word = random.choice(words)
@msyvr
msyvr / rc_mastermind.py
Last active November 25, 2021 07:06
mastermind: guess the color code
import random
def is_win(g, s):
'''check for a win, assign win value, return boolean'''
if all(g[i] == s[i] for i in range(len(s))):
win = True
print(f'You won! The code was {s}')
return win
else:
win = False
@msyvr
msyvr / hangman.py
Last active November 24, 2021 04:22
simple word guessing game with limited guesses (hangman)
# select letters one at a time to guess mystery word
import string
import random
from words_json import words
def get_valid_word(words):
word = random.choice(words)
while '-' in word or ' ' in word:
word = random.choice(words)