Skip to content

Instantly share code, notes, and snippets.

View jgat's full-sized avatar

Jackson Gatenby jgat

  • Sydney, Australia
View GitHub Profile
@jgat
jgat / getdigits.md
Created October 11, 2014 12:04
Recursion: Extracting Digits

Task:

Given a positive integer `n`, write a function `getdigits` which returns a list of digits in `n`, in base 10.

Here's the trick with recursion: we need to state the solution of the problem in terms of a different input to the same problem.

In this case, the problem is "given a number N, find all the digits of N", and we need to write this in a function called getdigits. One solution to this problem is: "To get the digits of N, first remove the last digit from N, then get all the digits of that number, then add on the last digit of N." Before you read on, convince yourself that this is a correct solution (though maybe we don't quite know how to write Python code to solve it).

Now, how do we do this? Here's the bits we need to do:

@jgat
jgat / ex1.c
Created August 31, 2014 00:12
Fun with Functions
#include <stdio.h>
/*
* 1. Write a function `ifelse`, which takes three parameters:
* - an integer (or a boolean in C99)
* - two functions (void* to void*)
* If the integer is nonzero, then return the first function,
* otherwise return the second function.
*
* 2. Write two functions `increment` and `decrement` which take a pointer to
@jgat
jgat / uqauth.py
Last active August 29, 2015 14:05
uqcloud authentication in Python CGI scripts.
# uqauth.py
"""
Usage:
try:
id = uqauth.get_user()
except uqauth.Redirected:
# you have been redirected. exit the script
else:
# continue as normal.
@jgat
jgat / 2012-2-Q17.md
Last active August 29, 2015 14:02
CSSE1001 2012 Sem 2 Q17

Question: What is the value of w after the following is evaluated?

def f(x, y):
  x.append(x.pop(0))
  x.append(y[0])
  return x

a = [4,5]
b = [1,2,3]
@jgat
jgat / webcrawler.py
Created May 25, 2014 06:06
Webcrawler
from HTMLParser import HTMLParser
import urllib2
import urlparse
import pprint
class LinkParser(HTMLParser):
def __init__(self):
HTMLParser.__init__(self)
self._urls = []
@jgat
jgat / sample_tests.py
Created May 14, 2014 00:20
Assignment 2 sample tests
#!/usr/bin/env python
"""
CSSE1001 Semester 1 2014 - Assignment 2 Sample Tests
Version 2 (14/05/2014)
Save this file in the same folder as the `assign2.py` file which contains your
solution, and the sample `todo.txt` file provided.
This program will run a sample of automated test cases on your code, and
check if it runs correctly.
@jgat
jgat / COMP4500 tests
Created September 29, 2013 07:12
Some test cases for COMP4500
# Note: Times for large tests will vary; note also this was run on moss.
xxx@moss ~/comp4500/repo/ass2 master $ ls
bin Makefile src tests
xxx@moss ~/comp4500/repo/ass2 master $ ls tests
generate.py Makefile minimal.in no-path.in one-waypoint.in simple-1.in simple-2.in simple-3.in
xxx@moss ~/comp4500/repo/ass2 master $ java waypoints.MoreTests tests/*.in
tests/minimal.in
@jgat
jgat / expected_output
Last active December 23, 2015 04:39
Some test cases for a thing
$ for f in $(ls shortestpath-tests/t*.txt); do echo $f; java shortestpaths.ShortestPathTest $f; echo; done;
shortestpath-tests/t1.txt
Path from 0 to 4 costs 15 via 0-1-2-3-4
Path from 0 to 4 costs 14 via waypointA (1): 0-1-2-3-4
Path from 0 to 4 costs 12 via waypoints A and B (1, 2): 0-1-2-3-4
shortestpath-tests/t2.txt
Path from 0 to 10 costs 12 via 0-3-6-9-10
Path from 0 to 10 costs 14 via waypointA (2): 0-1-2-6-9-10
Path from 0 to 10 costs 15 via waypoints A and B (2, 4): 0-1-2-4-5-9-10
@jgat
jgat / example_t10.py
Last active December 17, 2015 09:39
Tutorial 10
def sum(xs):
if not xs:
return 0
else:
return xs[0] + sum(xs[1:])
def sum2(xs):
if not xs:
return 0
elif len(xs) == 1:
@jgat
jgat / Tute_4_tests.py
Created November 3, 2012 04:44
Tutorial 4 Test Cases
"""Tutorial 4 Test cases
Your program should output this list:
[(13, 'identity', ('x',)),
(17, 'print_', ('things1', things2')),
(22, 'f1', ('a', 'b', 'c')),
(32, 'f2', ()),
(42, 'applicator', ('fs',)),
(44, '_helper', ('*args', '**kwargs'))]