Skip to content

Instantly share code, notes, and snippets.

View akaptur's full-sized avatar

Allison Kaptur akaptur

View GitHub Profile
@akaptur
akaptur / forces indents
Created June 19, 2012 21:04
use of else
def min_val(self, player, depth, max_depth):
#print "min_val depth of", depth
if self.game_won()[0]:
return self.utility()
if depth >= max_depth: #do I need =>?
return 0
else:
depth = depth + 1
min_init = 10
player = self.next_turn(player)
@akaptur
akaptur / gist:2990135
Created June 25, 2012 17:47
getattr (snip of Dive into Python)
>>> li = ["Larry", "Curly"]
>>> li.pop (1)
<built−in method pop of list object at 010DF884>
>>> getattr(li, "pop") (2)
<built−in method pop of list object at 010DF884>
>>> getattr(li, "append")("Moe") (3)
>>> li
["Larry", "Curly", "Moe"]
1. This gets a reference to the pop method of the list. Note that this is not
@akaptur
akaptur / gist:2996960
Created June 26, 2012 16:39
python is vs. ==
>>> a = [1,2]
>>> b = [1,2]
>>> c = a
>>> a is b
False
>>> a is c
True
>>> a == b
True
@akaptur
akaptur / gist:3157304
Created July 21, 2012 21:45
functions are objects
def getTalk(volume='shout'):
def whisper(word='hello'):
return word.lower()+'...'
def shout(word='hello'):
return word.capitalize()+"!"
if volume == 'shout' : # reference function without calling it
return shout
else:
return whisper
import random
import pdb
import sys
import pygame
from pygame.locals import QUIT, MOUSEBUTTONDOWN
class Game:
BLACK = (0,0,0)
WHITE = (255,255,255)
GREY = (140,140,140)
@akaptur
akaptur / gist:3251981
Created August 3, 2012 22:05
fun with pointers
# include <stdlib.h>
# include <stdio.h>
int main()
{
int myarray[10] = {100,4,'c','d','e','f','g','h','i','j'};
int *a_goddamn_pointer;
int x, y, z, w;
a_goddamn_pointer = &myarray;
@akaptur
akaptur / gist:3277921
Created August 6, 2012 19:47
simple C loop
for (int i=0; i<15; i++)
{
int c = rand();
printf("%d\n", c);
}
@akaptur
akaptur / gist:3279183
Created August 6, 2012 22:46
quicksort in C
#include <stdio.h>
#include <stdlib.h>
void quicksort(int *array, int length);
void swap(int *a, int *b);
void print(int *array, int length);
int main()
@akaptur
akaptur / gist:3287028
Created August 7, 2012 16:28
Quicksort with heisenbug
#include <stdio.h>
#include <stdlib.h>
void quicksort(int *array, int length);
void swap(int *a, int *b);
void print(int *array, int length);
int main()
@akaptur
akaptur / gist:3316675
Created August 10, 2012 18:46
String formatting with % in python
>>>a = '%s'
>>>a %= 1 # equivalently, a = a % 1
>>>a == '1'
True
# A more clear version
>>> f = '%s' % 1
>>> f
'1'