Skip to content

Instantly share code, notes, and snippets.

@smiley1983
smiley1983 / read_and_sort.py
Last active March 16, 2024 09:43
Read and sort input
# Define a function to insert a word into a list
def insert_word(word, l):
if len(l) > 0:
# If there's something in the list already,
# insert the new word
# First split the list into head and tail
head = l[0]
tail = l[1:]
# We can use the < operator in Python
diff --git a/home/smiley/Downloads/StateMachine04.ino b/StateMachine04_jh_modifi
ed.ino
index e2c3992..ff24608 100644
--- a/home/smiley/Downloads/StateMachine04.ino
+++ b/StateMachine04_jh_modified.ino
@@ -1,3 +1,5 @@
+#include <functional>
+
// state machine experiments, try #4
textToGame = function(text, seed) {
var game = JSON.parse(text)
if (game.version != 11) {
alert("Invalid version number: " + json_game.version);
}
//Adds determinism (when used with https://github.com/davidbau/seedrandom) to color scramble.
console.log(seed);
Math.seedrandom(seed);
def cardinal_distance_to_edge(loc, direction, limit):
if limit > 0:
next_step = gameMap.getLocation(loc, direction)
site = gameMap.getSite(next_step)
if next_step.owner == myID:
return 1 + cardinal_distance_to_edge (next_step, direction, limit - 1)
else: return 1
else:
return 1
@smiley1983
smiley1983 / bfs.py
Last active November 20, 2016 05:17
Breadth First Search example
DIRS = [
((-1, 0), 1),
((0, 1), 2),
((1, 0), 3),
((0, -1), 4)
]
def wrap (row, col):
if row < 0: row = row + gameMap.height
if col < 0: col = col + gameMap.width
/* This short program was written to clarify in my own head something which
* had confused me. If I pass a struct (not a pointer) as an argument to a
* function, that function receives a copy of the original struct. Changes
* made to that copy will have no effect on the struct in the calling function.
* This holds true for arrays inside the struct: the arrays are complete
* copies of the originals, not copies of pointers leading to the same place
* (as I had for some reason expected).
*
* The function called bad_manipulate1 shows a simple case demonstrating the
* error I made in another program.