Skip to content

Instantly share code, notes, and snippets.

@tomtheisen
tomtheisen / tictactoe.py
Created November 27, 2012 18:54
HackerRank Tic Tac Toe
#!/bin/python
outcomes = lambda: expando
outcomes.inProgress = "0 in progress"
outcomes.lose = "1 lose"
outcomes.tie = "2 tie"
outcomes.win = "3 win"
getNextPlayer = {"X": "O", "O": "X"}.get
@tomtheisen
tomtheisen / Goldbach.cs
Last active December 20, 2015 20:58
Goldbach
void Main() {
for (BigInteger e = 4; IsSumOfTwoPrimes(e); e += 2);
}
bool IsPrime(BigInteger n) {
for (BigInteger f = 2; f * f <= n; f++) {
if (n % f == 0) return false;
}
return true;
}
@tomtheisen
tomtheisen / fizzbuzz.py
Created October 28, 2013 19:33
Fizzbuzz without division or modulus
def fizz0():
global fizz
fizz = fizz1
return "Fizz"
def fizz1():
global fizz
fizz = fizz2
return ""
@tomtheisen
tomtheisen / checkerboard.py
Created December 1, 2013 18:38
Two ways to print a checkerboard
def printboard1(chars, squares):
def boardchar(row, col):
return " X"[(row^col) & 1]
print "-" * (chars * squares + 2)
for row in range(squares):
contents = "".join(boardchar(row, col) * chars for col in range(squares))
print ("|" + contents + "|\n") * chars,
print "-" * (chars * squares + 2)
@tomtheisen
tomtheisen / tsp.md
Last active December 30, 2015 02:39
Travelling Salesman Scenario

Travelling salesman scenario

Start from (1). Visit every destination. It is not necessary to return to (1).

Visually:

   (3)        13           (2)
    ------------------------

|\ /|

@tomtheisen
tomtheisen / Maybe Comprehension
Last active August 29, 2015 14:01
using linq query syntax to create a maybe<t> structure that can help avoid null reference exceptions
void Main() {
Console.WriteLine("Printing the seconds of 1/2/3");
PrintSecondDifference("1/2/3");
Console.WriteLine("Printing the seconds of null");
PrintSecondDifference(null);
}
// print the rounded number of seconds between the passed date and now
// if, at any point, there is no intermediate result (null),
@tomtheisen
tomtheisen / expression.cs
Last active August 29, 2015 14:05
Expression Parser
void Main() {
EnsureFailure(
"",
"#",
"1 + 2 +",
"+",
"(+)1",
"()",
"1 ()",
"1 2"
print("hello world")
lines
#["This is some very long text.","25"]
extract-array
#"This is some very long text."
#"25"
read-num
#"This is some very long text."
#25
@tomtheisen
tomtheisen / unpack.stax
Created February 15, 2018 05:46
Stax program to unpack a packed stax program
convert packed stax program to ascii
build stax encoding table
248 9786 9787 9829 9830 9827 9824 8226 9688 9675 9689 9794 9792 9834 9835 9788 9658 9668 8597 8252 182 167 9644 8616 8593 8595 8594 8592 8735 8596 9650 9660
32l {i\m
8962 199 252 233 226 228 224 229 231 234 235 232 239 238 236 196 197 201 230 198 244 246 242 251 249 255 214 220 162 163 165 8359 402 225 237 243 250 241 209 170 186 191 8976 172 189 188 161 171 187 9617 9618 9619 9474 9508 9569 9570 9558 9557 9571 9553 9559 9565 9564 9563 9488 9492 9524 9516 9500 9472 9532 9566 9567 9562 9556 9577 9574 9568 9552 9580 9575 9576 9572 9573 9561 9560 9554 9555 9579 9578 9496 9484 9608 9604 9612 9616 9600 945 223 915 960 931 963 181 964 934 920 937 948 8734 966 949 8745 8801 177 8805 8804 8992 8993 247 8776 176 8729 183 8730 8319 178 9632 916
129l {i127+\m
+:f|t use tables to stax-encode character codepoints
If you already have stax-encoded bytes, start here.