Skip to content

Instantly share code, notes, and snippets.

View JosephTLyons's full-sized avatar
🛏️

Joseph T. Lyons JosephTLyons

🛏️
View GitHub Profile
@JosephTLyons
JosephTLyons / main.py
Last active December 7, 2020 07:36
A Python example of dynamic programming for the recursive Fibonacci sequence algorithm
#!/usr/bin/env python3
from enum import auto, Enum, unique
def fib_recursive(n, calculated_results=None):
if calculated_results is not None and n in calculated_results:
result = calculated_results[n]
else:
@JosephTLyons
JosephTLyons / database_schema.py
Last active May 2, 2021 20:54
database_schema
from django.contrib.auth.models import AbstractUser
from django.db import models
from django.db.models.fields.related import ForeignKey
from django.utils.translation import ugettext
class ArchitectureSupport(models.Model):
THIRTY_TWO_BIT_VALUE = 32
SIXTY_FOUR_BIT_VALUE = 64
@JosephTLyons
JosephTLyons / threading_example.py
Last active July 2, 2021 04:48
Python Threading Example
import threading
import time
lock = threading.Lock()
def make_cookies(cake_number):
"""Instructions for the robot on how to create cookies"""
with lock:
@JosephTLyons
JosephTLyons / birthday_paradox.py
Last active July 4, 2021 02:55
Playing around with the Birthday Paradox
import random
from datetime import datetime, timedelta
def convert_integer_to_date(day_number_offset):
beginning_of_year = datetime(year=2021, month=1, day=1)
date = beginning_of_year + timedelta(days=day_number_offset)
date_string = date.strftime("%B %d")
return date_string
@JosephTLyons
JosephTLyons / kaprekar.py
Last active September 5, 2021 05:09
Exploring Kaprekar's constant
import random
from typing import Optional
def main() -> None:
run_number: int = 0
run_amount: int = 10_000
count_total: int = 0
while run_number < run_amount:
@JosephTLyons
JosephTLyons / card_shuffle_script.py
Last active September 12, 2021 01:34
A quick script to display a shuffled deck of cards
import random
cards = []
for suit in ["♠️", "♦️", "♣️", "❤️"]:
for rank in ["A", *list(range(2, 11)), "J", "Q", "K"]:
cards.append((rank, suit))
random.shuffle(cards)
@JosephTLyons
JosephTLyons / pairs.py
Created September 2, 2021 15:19
Simulates the trick of picking 2 random cards and then seeing if those two cards end up next to each other in a shuffled deck
import random
def main():
runs = 100_000
successes = 0
cards = list(range(13)) * 4
for _ in range(runs):
random.shuffle(cards)
@JosephTLyons
JosephTLyons / all_are_equal_timing_tests.py
Last active October 4, 2021 13:50
Time-testing various ways to check if all items in a list are the same value
from timeit import timeit
all_are_equal_functions = [
lambda numbers: all(a == b for a, b in zip(numbers, numbers[1:])),
lambda numbers: all(number == numbers[0] for number in numbers),
lambda numbers: len([number for number in numbers if number != numbers[0]]) == 0,
lambda numbers: len(set(numbers)) <= 1,
lambda numbers: not numbers or numbers.sort() or numbers[0] == numbers[-1],
lambda numbers: not numbers or numbers == ([numbers[0]] * len(numbers)),
lambda numbers: not numbers or numbers.count(numbers[0]) == len(numbers),
@JosephTLyons
JosephTLyons / signal_chain_processing.py
Last active September 7, 2021 06:59
A little program to emulate signal chain processing
import math
import random
import time
SIGNAL_CHAIN = [
lambda sample: sample + 1,
lambda sample: sample / 2,
lambda sample: math.tan(sample),
lambda sample: abs(sample),
@JosephTLyons
JosephTLyons / card_shuffle_OOP.py
Last active September 12, 2021 01:34
A class-based approach to displaying a shuffled deck of cards
import random
class Card:
def __init__(self, card_value):
self.card_value = card_value
def __str__(self):
rank_string = self.get_rank_string()
rank_string_justified = rank_string.rjust(2, " ")