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.rs
Created January 9, 2022 06:56
How to slice a string via code points in Rust
fn main() {
let text = "Super🐍Snake";
let text_slice = &text.chars().collect::<Vec<_>>()[5..=10];
for character in text_slice {
println!("{}", character);
}
}
@JosephTLyons
JosephTLyons / main.rs
Created December 5, 2021 20:25
Examples of some Rust Crates that mimic some common Python data structures
use counter::Counter;
use defaultmap::DefaultHashMap;
fn counter_example() {
let counter = "Hello, world!".chars().collect::<Counter<_>>();
for (item, count) in counter.most_common() {
println!("{}: {}", item, count);
}
}
@JosephTLyons
JosephTLyons / main.py
Created November 27, 2021 18:42
A script that categorizes words by a "shrink" ranking
from collections import defaultdict
import random
def get_shrink_rank(word):
if " " in word:
print(word)
raise Exception("Word must be a single word")
if not word.isalpha():
print(word)
@JosephTLyons
JosephTLyons / iter_int.py
Created November 22, 2021 20:16
An integer class whose digits can be iterated over
class IterInt(int):
def __init__(self, integer):
self.integer = integer
def __iter__(self):
return iter(int(character) for character in str(self.integer))
number = IterInt(4_999_135)
for digit in number:
@JosephTLyons
JosephTLyons / word_shuffle.py
Created November 15, 2021 03:00
A program that shuffles the characters of each word in a text file, while preserving the position of punctuation
import random
def shuffle_word(word):
shufflable_characters = [character for character in word if character.isalnum()]
random.shuffle(shufflable_characters)
word = [shufflable_characters.pop(0) if character.isalnum() else character for character in word]
word = "".join(word)
# Sanity check to ensure we've used the exact same number of letters as we initially collected
assert len(shufflable_characters) == 0
@JosephTLyons
JosephTLyons / main.rs
Created November 7, 2021 09:45
A few examples illustrating a few ways to iterate over `Results` in Rust
fn main() {
let items: [Result<i32, &str>; 4] = [Ok(1), Ok(2), Err("Missing Value"), Ok(3)];
// 1
let items_result: Result<Vec<_>, _> = items.into_iter().collect();
match items_result {
Ok(items) => {
for item in items {
println!("{}", item);
@JosephTLyons
JosephTLyons / visualize.py
Last active November 15, 2021 03:04
A function that uses a Python Counter to print a visual summary of the occurrences of integers in a list
from collections import Counter
from random import randint
def visualize(numbers):
counter = Counter(numbers)
max_number_length = len(str(max(numbers)))
for number, count in counter.most_common():
padded_number = str(number).rjust(max_number_length, "0")
count_visualization = "*" * count
@JosephTLyons
JosephTLyons / main.rs
Last active September 30, 2021 12:23
A quick Rust code jam to make Rock, Paper, Scissors!
use std::io::{self, Write};
#[derive(Debug, PartialEq)]
enum Weapon {
Rock,
Paper,
Scissors,
}
impl std::fmt::Display for Weapon {
@JosephTLyons
JosephTLyons / meta_print.py
Created September 18, 2021 02:34
Silly Twitter challenge of writing an application that prints its own source code
with open("./meta_print.py", "r") as file:
for line in file:
print(line, end="")
@JosephTLyons
JosephTLyons / coins.py
Last active February 22, 2024 21:12
Verifies that any change value under 1 dollar can be created with 3 quarters, 1 dime, 2 nickels, and 4 pennies
def coins_can_make_value(coin_dictionary: dict[int, int], value: float) -> bool:
if value == 0:
return True
for coin_value, coin_count in coin_dictionary.items():
for _ in range(coin_count):
if value >= coin_value:
value -= coin_value
if value == 0: