Skip to content

Instantly share code, notes, and snippets.

@SteveDaulton
SteveDaulton / palindrome.py
Last active April 10, 2024 13:54
Efficient Integer Palindrome Checker
def is_palindrome(number: int) -> bool:
"""Return True if number is palindrome integer.
Designed to be efficient with integers of arbitrary length.
"""
if number < 0:
return False
if number < 10:
return True
reversed = 0
while number > reversed:
@SteveDaulton
SteveDaulton / tic_tac_toe.py
Last active April 2, 2024 15:35
Tic-Tac-Toe
"""Tic-Tac-Toe
A simple implementation in (mostly) procedural style.
"""
import sys
from random import choice
from typing import TypeAlias
Board: TypeAlias = list[list[str]]
@SteveDaulton
SteveDaulton / roulette_wheel.py
Created November 16, 2023 11:52
Roulette Wheel simulation
"""Roulette wheel simulation."""
from random import randrange
def wheel_spin() -> int:
"""Return result of roulette wheel spin.
Returns
-------
@SteveDaulton
SteveDaulton / birthday_paradox.py
Last active September 27, 2023 15:32
Birthday Paradox - a Monte Carlo experiment
"""Explore the surprising probabilities of the "Birthday Paradox".
Idea inspired by The Big Book of Small Python Projects by Al Sweigart.
https://inventwithpython.com/bigbookpython/project2.html
This version runs much faster than the version in the book, and so
can handle much larger numbers of people.
The main optimisations are:
1. `get_birthdays_till_match` exits early when a match is found.
@SteveDaulton
SteveDaulton / record_parts.py
Last active March 9, 2023 21:30
Sequential recording with Audacity with Python.
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Record Parts:
=============
A demonstration Python application controlling Audacity to make a
sequence of recordings. There will inevitably be short breaks between
each recording due to the time required to Export, clean up and start
@SteveDaulton
SteveDaulton / pipeclient.py
Last active May 8, 2024 15:07
pipeclient.py is a Python module for sending commands to Audacity from Python. This version is more recent than Audacity's version.
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""Automate Audacity via mod-script-pipe.
Pipe Client may be used as a command-line script to send commands to
Audacity via the mod-script-pipe interface, or loaded as a module.
Requires Python 3.
(Python 2.7 is now obsolete, so no longer supported)