Skip to content

Instantly share code, notes, and snippets.

View sloanlance's full-sized avatar
Certified GitHub Pro

Mr. Lance E Sloan sloanlance

Certified GitHub Pro
View GitHub Profile
@sloanlance
sloanlance / nested-for-map-equiv.py
Last active July 17, 2025 15:18
Python: map equivalent of nested for loops
def nestedFor(m, n):
"""add each element of m to each element of n"""
result = []
for i in m:
for j in n:
result.append(i+j)
return result
@sloanlance
sloanlance / validate-a-roman-number.py
Last active July 10, 2025 18:54
Python: Validating Roman Numerals via regex
# Note: Regex developed using regex101.com, which generated this code, then was edited further.
# See: https://regex101.com/r/Vj3CHI/5
# The regex below is part of the solution for HackerRank challenge:
# https://www.hackerrank.com/challenges/validate-a-roman-number
# The regex is probably incomplete, but it's good enough to pass HackerRank tests.
import re
regex = r'^(M{,3}(?=([^M]))){,1}([C]{,1}M{,1}){,1}([C]{,1}D{,3}){,1}([X]{,1}C{,3}){,1}(X{,1}L{,1}){,1}(I{,1}X{,3}){,1}(I{1,3}[VX]{,1}|VI{,3}){,1}$'
@sloanlance
sloanlance / numbar.py
Last active July 10, 2025 13:46
Python: A small function to make a string of digits for numbering columns in the output.
def numbar(n: int) -> str:
"""
Make a string of digits for numbering columns in the output. Column
numbering begins with zero. If the number of columns require more
place values, newlines are used to make multiple rows in the output.
E.g., if `n <= 10`, there will be one row of output, but if
`10 < n <= 100`, there will be two rows of output, etc.
"""
rows = len(str(n - 1))
@sloanlance
sloanlance / _Python Pillow image roll - README.md
Last active May 8, 2025 13:06
Using Pillow to roll images horizontally and vertically.

Use the Pillow module for Python to "roll" images horizontally or vertically. When an image is rolled, it is shifted in some direction. The part of the image that would be lost as a result of that shift is added onto the opposite side of the image.

@sloanlance
sloanlance / clone-private-github-repo-in-google-colab.ipynb
Last active May 15, 2025 14:47
clone-private-github-repo-in-google-colab.ipynb
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@sloanlance
sloanlance / unicode-variables.py
Last active December 20, 2023 02:29
Python's handling of Unicode-character variables is surprising!
𝐀 = {'😀': 5}
for ℹ︎ in range(20):
# print(i, ℹ︎, i == ℹ︎) # NameError: name 'i' is not defined.
for _,__ in 𝐀.items():
if __ == ℹ︎:
print(_, ℹ︎)
print(𝐀) # Prints dictionary in 𝐀
print(A) # Also prints dictionary in 𝐀!
print(𝐀 is A) # True
@sloanlance
sloanlance / indexformatter1.py
Last active November 1, 2023 18:45
A Python class before and after optimization, as inspired by GitHub Copilot.
class IndexFormatter:
def __init__(self, volumeId: int):
self.itemPages = (
ItemPage.objects.filter(volume__id=volumeId)
.order_by('item__topic__name', F('page') * 1, 'item__name'))
def format(self) -> str:
"""
Format the index for printing.
"""
@sloanlance
sloanlance / pandas-read-csv-with-other-encodings.ipynb
Last active October 24, 2023 18:50
pandas-read-csv-with-other-encodings.ipynb
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@sloanlance
sloanlance / Say Goodbye to Loops in Python, and Welcome Vectorization.md
Last active October 3, 2023 16:03
Say Goodbye to Loops in Python, and Welcome Vectorization!

Say Goodbye to Loops in Python, and Welcome Vectorization!

Use Vectorization — a super fast alternative to loops in Python

By Anmol Tomar on CodeX

Nov 29, 2022

Introduction

@sloanlance
sloanlance / jq Notebook.md
Created September 29, 2023 15:18
Useful notes about using jq

jq Notebook

Useful notes about using jq.

  1. Print all paths to values
    • jq -c paths Prints each path as an array and doesn't show values.
    • jq --stream 'select(.[1]|scalars!=null) | "\(.[0]|join(".")): \(.[1]|tojson)"' example.json
      Nearly perfect. If each path shown began with . and array indices were enclosed in square brackets, then each one would be a valid jq query string.