Skip to content

Instantly share code, notes, and snippets.

struct A {
name: String
}
struct B {
name: String
}
@zmarvel
zmarvel / chcommon.mk
Created June 23, 2019 04:35
Helper make rules for ChibiOS projects
OPENOCD ?= /usr/bin/openocd/
OPENOCD_SCRIPTS ?= /usr/share/openocd/scripts
openocd: $(ELF)
$(OPENOCD) -s $(OPENOCD_SCRIPTS) \
-f interface/stlink-v2-1.cfg \
-f target/stm32l4x.cfg \
-c "telnet_port 4444" \
-c "gdb_port 3333"
@zmarvel
zmarvel / systems_programming.md
Created April 18, 2019 05:58
Systems programming notes

Systems Programming Topics

  • Tasks
    • Threads share memory. You have to make sure that memory accesses are atomic when it matters, using synchronization primitives.
    • Processes by default have their own memory. In Unix, you have to use a "shared memory" API to communicate between processes. You can also use pipes and sockets.
  • Synchronization
    • Mutexes. Low-level primitive for making sure "critical sections" aren't interrupted by another scheduler giving control to another thread. Suppose two threads share some memory. This ensures thread A can finish its modification of the shared memory before thread B accesses the memory, and vice-versa. See the Xinu OS book.
  • Semaphores. Another low-level primitive for signaling that something has been done, and waiting on it in another thread. Similar to mutexes, effectively protects shared memory and indicates "critical sections." See the Xinu OS book, Wikipedi
# Implement a queue with 2 stacks . Your queue should have an enqueue and a
# dequeue function and it should be "first in first out" (FIFO).
#
# Optimize for the time cost of mmm function calls on your queue. These can be
# any mix of enqueue and dequeue calls.
#
# Assume you already have a stack implementation and it gives O(1)
# time push and pop.
class Stack(list):
@zmarvel
zmarvel / reverse_binary.py
Created October 16, 2016 23:54
Leetcode 190 solution.
# Given a 32-bit decimal number, convert it to binary, reverse it, then print
# the reversed version in decimal.
def solution(n):
result = 0
for i in range(32):
result |= ((n >> i) & 1) << (31 - i)
return result

Escape the trolls

This is a maze game, implemented in four phases:

  1. The player (U) can move around in the maze.
  2. The player can move the walls (#) of the maze.
  3. Trolls spawn (T) who seek the player and move a wall when the player does.
  4. Each game generates a maze.
@zmarvel
zmarvel / 2.py
Last active June 24, 2016 01:58
A Python solution to the same problem as my 2.clj gist.
#!/usr/bin/env python3
# Given a list of "passwords" for which e.g. "abc" and "cba" or "cat" and "tac"
# are not considered unique but "abc" and "bac" are considered unique, count the
# unique words in the list.
#
# Usage: python3 2.py < words.txt
import sys
from collections.abc import Set
@zmarvel
zmarvel / 2.clj
Created June 23, 2016 02:42
A Clojure solution to a Google programming challenge my roommate was telling me about.
; Given a list of "passwords" for which e.g. "abc" and "cba" or "cat" and "tac"
; are not considered unique but "abc" and "bac" are considered unique, count the
; unique words in the list.
;
; Run it like this: `clojure 2.clj < words.txt`
(require 'clojure.string)
options iwlwifi 11n_disable=1
options iwlwifi swcrypto=1
#!/bin/bash
#
# This is free and unencumbered software released into the public domain.
#
# Anyone is free to copy, modify, publish, use, compile, sell, or
# distribute this software, either in source code form or as a compiled
# binary, for any purpose, commercial or non-commercial, and by any
# means.
#
# In jurisdictions that recognize copyright laws, the author or authors