{{ message }}

Instantly share code, notes, and snippets.

# Ben Hoytbenhoyt

Created May 12, 2016
Print most frequent N-grams in given file
View ngrams.py
 """Print most frequent N-grams in given file. Usage: python ngrams.py filename Problem description: Build a tool which receives a corpus of text, analyses it and reports the top 10 most frequent bigrams, trigrams, four-grams (i.e. most frequently occurring two, three and four word consecutive combinations). NOTES
Created Oct 7, 2020
Diff to override goldmark's code block output
View markdown.diff
 diff --git a/internal/markdown/markdown.go b/internal/markdown/markdown.go index a729b9f..94d29c1 100644 --- a/internal/markdown/markdown.go +++ b/internal/markdown/markdown.go @@ -13,8 +13,11 @@ import ( "bytes" "github.com/yuin/goldmark" + "github.com/yuin/goldmark/ast" "github.com/yuin/goldmark/parser"
Created Jul 7, 2020
Little Go tool to flatten JSON input
View flattenjson.go
 // Flatten JSON input // // Example: // // \$ echo '{"user":"Ben","ints":[true,false,null],"sub":{"x":1,"y":2}}' | go run flattenjson.go // _.ints = true // _.ints = false // _.ints = null // _.sub.x = 1 // _.sub.y = 2
Created Aug 5, 2016
"Birthday problem" calculator in Python
View birthday_probability.py
 """Calculate the probability of generating a duplicate random number after generating "n" random numbers in the range "d". Usage: python birthday_probability.py n [d=365] Each value can either be an integer directly, or in the format "2**x", where x is the number of bits in the value. For example, to calculate the probability that two people will have the same birthday in a room with 23 people:
Created Aug 3, 2016
An atomic, thread-safe incrementing counter for Python
View atomic_counter.py
 """An atomic, thread-safe incrementing counter.""" import threading class AtomicCounter: """An atomic, thread-safe incrementing counter. >>> counter = AtomicCounter() >>> counter.increment()
Created Oct 17, 2019
Overview of (parts of) the Python standard library
View python-stdlib.md

I'm going to demo a bunch of Python builtin and stdlib functions. There's a lot to get through, so I'll be going fast, but please stop me and ask questions as we go. The goal is to give you a taste of Python's power and expressivity if you're not a Python person, or maybe teach you a few new tricks if you are already.

## Built-in functions

```# enumerate: iterate with index *and* item
>>> strings = ['123', '0', 'x']
>>> for i, s in enumerate(strings):
...     print(f'{i} - {s}')  # f-strings!```
Created May 8, 2018
Calculate edit distance with simple (memoized) recursive algorithm
View edit_distance.py
 def distance(s, t, cache=None): """Return minimum edit distance between s and t, where an edit is a character substitution, deletion, or addition. """ if not s: return len(t) if not t: return len(s) if cache is None:
Created Nov 20, 2018
AWK program to compare time complexity of joining strings
View join.awk
 # AWK program to compare time complexity of joining strings using a # simple O(N^2) algorithm and a slightly more complex O(N log N) one. # Join array elements, separated by sep: O(N^2) version function join1(a, sep, i, s) { for (i = 1; i+1 in a; i++) { s = s a[i] sep } if (i in a) { s = s a[i]
Created Sep 21, 2018
Go program to print the Mandelbrot set on stdout
View mandelbrot.go
 // Print the Mandelbrot set on stdout package main import ( "fmt" "math/cmplx" ) const (
Created Nov 3, 2016
Test how many threads we can run at once