Skip to content

Instantly share code, notes, and snippets.

View gulan's full-sized avatar

Glen Wilder gulan

  • Santa Clara, CA
View GitHub Profile
module CombinatorialHierarchy where
{- https://en.wikipedia.org/wiki/Combinatorial_hierarchy
3, 2^3-1, 2^7-1, 2^127-1
seq:
3, 7, 127, 170141183460469231731687303715884105727
cumulative sum:
#!/usr/bin/env python
# The way I usually write it.
"""
rep customers:
seq onecust:
invest
alt movement:
payin
withdraw
#!/usr/bin/env python
data = """a 9
b 2
c 2
d 4
e 12
f 2
g 3
h 2
/* Exercise 1-13. Write a program to print a histogram of the lengths
of words in its input. */
/*
input = (special | word)*
special = blank | tab | newline
word = non-special+
*/
#include<stdio.h>
@gulan
gulan / wc.c
Created November 17, 2016 17:39
/* Count words, lines and characters.*/
/*
The number of lines is just the number of newline chars. Every
character, including newline is counter as a character. EOF is not a
character.
I use a structure design rather than an IN/OUT state flag.
A sequence of chars not including blank, tab or newline is a
@gulan
gulan / htoi.c
Created November 17, 2016 17:34
#include<assert.h>
#include<stdio.h>
/* htoi(): convert hext digit string to native int. */
/*
left-to-right recursive:
htoir("2") == 2
htoir("A") == 10
htoir("23") == 16*htoir("2") + 3
/*
Display the length of the the longest line from text input.
*/
/*
[Skip the write-up as I did not follow it when writing the code.]
A simple text file is an iteration of lines, followed by EOF.
simple = body + EOF
@gulan
gulan / squeeze.c
Last active November 17, 2016 16:29
#include<assert.h>
#include<stdio.h>
#include<string.h>
/* The function squeeze removes all instances of specified characters
from the input string array. The characters to be excluded are given in
a second argument.*/
struct testrec {
char inpu[30];
@gulan
gulan / tree.py
Last active September 30, 2016 15:56
#!usr/bin/env python
from __future__ import print_function
"""
A tree is a restricted graph. Both trees and graphs may represented as
a set of (parent, child) pairs.
"""
t = set([(1,2), (1,3), (1,7), (2,4), (2,5), (4,6), (7,8), (7,9)])
import types
# - - - - - countdown - - - - -
def countdown(n):
# Tail recursive call will overflow Python's stack for large n.
return n if n == 0 else countdown(n-1)
assert countdown(5) == 0