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 / 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 / selection_sort.py
Last active October 29, 2021 07:57
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):
for i in range(uoindex, len(ulist)):
if ulist[i] < ulist[uoindex]:
@msyvr
msyvr / bubble_sort.py
Last active October 29, 2021 06:44
mit 6.0001 - bubble sort
import random
def bubble_sorted_list(ulist):
'''
sort ulist by pairwise checks:
pairwise comparisons from start (index 0) to end (index n), shifting higher values toward the end
aka BUBBLE SORT
'''
swap = True
while swap:
@msyvr
msyvr / make_maxpalindrome.py
Last active October 26, 2021 23:45
make the longest palindrome in a string with a single "_"
def make_longest_palindrome(s):
'''
From a string object with a single "_", return the length of the longest possible palindrome and the letter to insert in place of "_"
'''
l = list(s)
print(l)
maxlen = 0
blank = "_"
blank_temp = "_"
# check all possible ordered subsets of the list for palindrome ticker
@msyvr
msyvr / length_maxpalindrome.py
Created October 26, 2021 20:09
find the length of the longest palindrome section in any string (word, phrase, ...)
def longest_palindrome(s):
'''
find the longest palindrome in any string object
'''
l = list(s)
print(l)
maxlen = 0
# check all possible same-ordered subsets of the list and return the length of the longest palindrome
for lefti in range(len(l)-1):
for righti in range(lefti + 1, len(l)):
@msyvr
msyvr / bunny_class.py
Last active October 25, 2021 02:54
mit 6.0001 classes - define class method
# implement and call a class method
class Animal(object):
def __init__(self, age, name = "anonymous bunny"):
assert type(age) == int
self.age = age
self.name = name
def __str__(self):
return "Hey! Here's an Animal of age: " + str(self.age) + " years."
@msyvr
msyvr / classespy.py
Last active October 24, 2021 23:25
mit 6.0001 implement and use classes, subclasses, and class variables
# walk through principles of implementing and calling classes and sub/superclasses
# implement and use a class variable
class Animal(object):
def __init__(self, age):
assert type(age) == int
self.age = age
self.name = []
def __str__(self):
return "Hey! An Animal of age: " + str(self.age) + " years."
@msyvr
msyvr / fraction_class.py
Created October 20, 2021 06:25
mit 6.0001 - define Fraction class and methods
class Fraction(object):
def __init__(self, num, den):
assert type(num) == int and type(den) == int and den != 0
self.num = num
self.den = den
def __str__(self):
return self.num + "/" + self.den
def __add__(self, other):
@msyvr
msyvr / fib_eff.py
Last active November 24, 2021 19:27
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)
dict[n] = fibn
return fibn