Skip to content

Instantly share code, notes, and snippets.

@hosaka
hosaka / delay_time_units_check.py
Created March 12, 2015 19:03
Using this method with argparse parameter type=delay_check. Returns a value in seconds according to units specified. For example 51m or 120s or 2d, or defaults to seconds.
def delay_check(self, delay):
# get the units and the delay value
unit = delay.lstrip("0123456789")
# TODO: this raises exception, still caught by the parser, but incorrect
# for example input node 1 timer on aksldjhf will crash here
if unit not in "":
value = int(delay[:-len(unit)])
else:
return int(delay)
@hosaka
hosaka / dupe_arrays.py
Created March 16, 2015 00:17
remove duplicates from two arrays with matching index
__author__ = 'march'
ips = ["192.312.3", "192.312.3", "192.312.3", "10.10.3.4", "99.23.1.3", "99.23.1.3"]
ts = ["1", "2", "3", "4", "5", "6"]
new_ips = []
new_ts = []
for ip in ips:
new_ips.append(ip)
@hosaka
hosaka / json.lua
Created March 17, 2015 02:04
very crude but minimal JSON encode/decode in LUA
-- json parsing
function json_decode(msg)
local js = {}
for k,v in string.gmatch(msg,'"(%w+)":"(%w+)",') do
js[k] = v;
end
for k,v in string.gmatch(msg,'"(%w+)":"(%w+)"}') do
js[k] = v;
end
return js
@hosaka
hosaka / chrono_timer.cpp
Created March 18, 2015 23:31
Using chrono datetime utilities from C++11
#include <chrono>
#include <iostream>
#include <thread>
int main()
{
// time now
auto start = std::chrono::high_resolution_clock::now();
std::this_thread::sleep_for(std::chrono::seconds(5));
@hosaka
hosaka / README.md
Last active February 3, 2022 08:12
Abstract Data Type using opaque pointers in C

Compile and run

gcc adt.c array.c -o adt
./adt
@hosaka
hosaka / vtable.c
Created August 10, 2015 16:27
Virtual Method Tabble, implementing dynamic binding (polymorphism) in C
#include <stdio.h>
#include <stdlib.h>
// vtable is a virtual method table that implements dynamic binding
// The goal is to write code that operates abstractly on Shapes rather than on
// specific Circles or Rects as a form of // polymorphism //
// abstract and concrete objects
typedef struct Shape Shape;
typedef struct Circle Circle;
@hosaka
hosaka / inheritance.c
Created August 10, 2015 16:28
Basic inheritance using structs in C
#include <stdio.h>
#include <stdlib.h>
// super "class"
typedef struct
{
int age;
double weight;
} Animal;
@hosaka
hosaka / func_point.c
Created August 10, 2015 16:30
Usages of function pointers with structs in C
#include <stdio.h>
#include <stdlib.h>
// function pointers in C
// a function is just another address in memory which contains
// executable code, similar to getting address of a variable by using
// pointers, we can get an addr of function using function pointers
void func()
{
@hosaka
hosaka / bin_tree.c
Created August 10, 2015 16:33
Most simple binary tree implementation in C
#include <stdio.h>
#include <stdlib.h>
// node structure
typedef struct node_s
{
int value;
struct node_s *lnode;
struct node_s *rnode;
} node_t;
@hosaka
hosaka / linked_list.c
Created August 10, 2015 16:34
Most simple linked list implementation in C
#include <stdio.h>
#include <stdlib.h>
// list node
typedef struct node_s
{
int data;
struct node_s *next;
struct node_s *prev;
} node_t;