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 / lambda_for_max_key.py
Last active September 30, 2021 03:45
use python lambda to set key for max function applied to lists and nested lists
a = (0, 1, 0)
b = (1, -1, 1)
c = [a, b, b]
d = [b, a, a]
e = [c, d]
# user selects tuple index for comparison
index_okay = False
while not index_okay:
try:
@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 / tictactoop_dumbbot.py
Last active October 4, 2021 08:10
recurse-center--post-pairing-tictactoe-w-computer-random-choices
from hrplayers_ttt import hPlayer, rPlayer
class Tictactoe:
# The tictactoe game board spot IDs will be:
# 0 1 2
# 3 4 5
# 6 7 8
def __init__(self):
# list of all spots in the game, mapping 0 thru 8 to spots on the 3x3 board
self.spot_ids = [i for i in range(9)]
# record of board spot values reflecting which player occupies a spot, and with a space representing an empty/available spot
@msyvr
msyvr / game_ttt.py
Last active September 17, 2021 04:29
recurse-center--pairing--tictactoe
from player_ttt import hPlayer
class Tictactoe:
# The tictactoe game board spot IDs will be:
# 0 1 2
# 3 4 5
# 6 7 8
def __init__(self):
# list of all spots in the game, mapping 0 thru 8 to spots on the 3x3 board
self.spot_ids = [i for i in range(9)]
# record of board spot values reflecting which player occupies a spot, and with a space representing an empty/available spot
@msyvr
msyvr / player.py
Created September 15, 2021 05:11
tictactoo: oo tictactoe (python)
class Player:
def __init__(self, mark):
# mark can be 'x' or 'o'
self.mark = mark
# different types of players can inherit from here, so >> pass
def get_play(self, game):
pass
class HumanPlayer(Player):
@msyvr
msyvr / polymrphsm.py
Created September 15, 2021 00:22
python polymorphism: with inheritance objects, methods follow priority order behaviour as defined in: 1. object class, 2. parent class
class IrishPostalAddress(BasePostalAddress):
def __init__(self, recipient, postalCode):
super().__init__("IRELAND", recipient)
self.postalCode = postalCode
def display(self):
print(self.recipient)
print(self.postalCode)
print(self.country)
@msyvr
msyvr / cmpstn.py
Created September 14, 2021 23:08
python: composition (alternative to inheritance for reusing code)
class Point:
def __init__(self, initX, initY):
self.x = initX
self.y = initY
def distanceFromOrigin(self):
return ((self.x ** 2) + (self.y ** 2)) ** 0.5
def __str__(self):
@msyvr
msyvr / xtendng.py
Created September 14, 2021 23:06
python: extending via super()
class Point:
def __init__(self, initX, initY):
self.x = initX
self.y = initY
def distanceFromOrigin(self):
return ((self.x ** 2) + (self.y ** 2)) ** 0.5
def __str__(self):
@msyvr
msyvr / inheritnc.py
Last active September 14, 2021 23:02
python: basic inheritance
class Point:
def __init__(self, initX, initY):
self.x = initX
self.y = initY
def distanceFromOrigin(self):
return ((self.x ** 2) + (self.y ** 2)) ** 0.5
def __str__(self):
@msyvr
msyvr / gcden.py
Created September 14, 2021 22:41
python: function: greatest common denominator
def gcden(x, y):
while x % y != 0:
oldx = x
oldy = y
x = oldy
y = oldx % oldy
return y