Skip to content

Instantly share code, notes, and snippets.

View bru32's full-sized avatar

Bruce Wernick bru32

  • TechniSolve
  • South Africa
View GitHub Profile
@bru32
bru32 / diag.py
Created August 13, 2025 07:06
Generator that yields 2D list values.
"""
Diagonal array generator.
Start at top right corner going diagonally up
and end in the bottom left corner.
Bruce Wernick
13 August 2025
"""
def diag(arr):
nr = len(arr)
@bru32
bru32 / factors.py
Last active August 13, 2025 07:08
Best way to get all factors of a number.
"""
Get all the factors of a number.
Bruce Wernick
13 August 2025
"""
import math
def get_factors(n):
arr = set()
@bru32
bru32 / lagrange.py
Created August 13, 2025 04:31
Closure to create an interpolator function
"""
Closure to create an interpolator function.
Bruce Wernick
13 August 2025
"""
def lagrange(xp, yp):
n = len(xp)
def inner(x):
value = 0.0
@bru32
bru32 / roots.py
Last active August 13, 2025 03:15
Root finding using Python closures
"""
Root finder as a closure. Has benefits over the class
approach because you are not carrying around self.
This demo is not really for the root finder. It's mainly
to demonstrate the use of closures in solving this kind
of problem. So, I don't implement all the methods.
Also, the methods that are implemented don't have all
the safeties. Things like checking for zero slope or
max iteration limit reached.
Bruce Wernick
@bru32
bru32 / newt.py
Last active August 13, 2025 00:22
Interesting idea to write Newton's root finding method as a decorator
"""
Traditional Newton root finding method
and Alternative using decorators...
Author: Bruce Wernick
Date: 13 August 2025
"""
def newt(f, x, **kwargs):
maxits = kwargs.get("maxits", 48)
tol = kwargs.get("tol", 1e-6)
@bru32
bru32 / tk_template_02.py
Last active August 12, 2025 16:37
tkinter template with an App class.
import tkinter as tk
from tkinter import ttk
class App(tk.Tk):
def __init__(self):
super().__init__()
self.geometry("600x400+500+10")
self.title("App")
self.style = ttk.Style()
self.style.theme_use("clam")
@bru32
bru32 / tk_template_01.py
Last active August 12, 2025 05:23
Most basic tkinter skeleton
import tkinter as tk
from tkinter import ttk
root = tk.Tk()
root.title("App")
root.geometry("400x400+600+10")
root.mainloop()
# 2D array traversal
# These are not really for production use,
# they are more to demonstrate how to traverse 2-D arrays.
# Bruce Wernick
# 31 January 2022
from collections import deque
import itertools
from random import shuffle
@bru32
bru32 / files.txt
Created March 16, 2017 08:36
data_files
hybrid_150.json
hybrid_300.json