Skip to content

Instantly share code, notes, and snippets.

@r3
r3 / poker.py
Created April 25, 2012 00:12
Udacity code up to #16
def poker(hands):
"""Return the best hand: poker([hand,...]) => hand"""
# Recall that providing a key is essentially calling
# that function on 'hands' and sorting the objects by
# the results.
return max(hands, key=hand_rank)
def card_ranks(cards):
"""Return hand sorted by card rank"""
# Dictionary, maps between one object and another
@r3
r3 / gist:2590147
Created May 3, 2012 22:52
Project Euler #1 (prior to refactor)
# The name doesn't well reflect what the function does (as Ilya pointed
# out), but after the refactor, this will be fixed
def find_multiples(factors, end=1000, func=sum):
"""Creates a list of multiples less than a given end
value (1000 by default) from the given factors and
applies a function to them (sum by default).
def find_multiples(factor, end):
"""Creates a list of multiples less than a given end
value from the given factors.
"""
return range(factor, end, factor)
"""Project Euler: Problem 3"""
def largest_prime_factor(num):
"""Determines the largest prime factor of the given number"""
def divide_out_multiples(ix, num):
"""Removes swathes of multiples at a time. If two is a multiple,
we remove any even factors and return the result.
"""
while num % ix == 0:
num /= ix
@r3
r3 / euler5.py
Created May 16, 2012 04:13
Project Euler 5 solution and explanation.
"""Project Euler: Problem 5
Requires: Python 2
2520 is the smallest number that can be divided by each of the numbers from
1 to 10 without any remainder. What is the smallest positive number that is
evenly divisible by all of the numbers from 1 to 20?
This result takes a different approach to factoring than we used in class.
The code that we built is in another gist linked in the email that I sent
out. I didn't feel the need to generate prime numbers if I could let a prime
@r3
r3 / Euler7.py
Created May 18, 2012 05:21
Project Euler Challenge 7
"""You'll have to forgive me some of the ugly,
I wrote this when I was new to Python, but it looks passable.
-r3
"""
def is_prime(num):
"""Checks argument for primality"""
# Optimization to determine if even
if num % 2 == 0:
@r3
r3 / euler6.py
Created May 18, 2012 05:27
Project Euler 6
"""I took the liberty of changing two variable names that happen to shadow
builtin Python functions (sum and str), but the code is otherwise
unedited from what the crew produced in the meeting today.
"""
def squareOfSums(numbers):
#Bob
n = sum(numbers)
return n ** 2
numbers = ("73167176531330624919225119674426574742355349194934"
"96983520312774506326239578318016984801869478851843"
"85861560789112949495459501737958331952853208805511"
"12540698747158523863050715693290963295227443043557"
"66896648950445244523161731856403098711121722383113"
"62229893423380308135336276614282806444486645238749"
"30358907296290491560440772390713810515859307960866"
"70172427121883998797908792274921901699720888093776"
"65727333001053367881220235421809751254540594752243"
"52584907711670556013604839586446706324415722155397"
@r3
r3 / gist:3549083
Created August 31, 2012 04:30
Classes and Instances
class Foo():
# It has an attribute called "class_attribute"
# All instances of 'Foo' will share (have a reference to) this class_attribute
# We are not in a method, so we have no reference to any instances, only the class
class_attribute = None
# This is the special method used when instantiating 'Foo'
# Here, 'self' has a special meaning ONLY because of the way it is called.
# As the first parameter, it will be given the created instance of 'Foo'
# Realize that the word 'self' has no special meaning. It is simply a parameter.
@r3
r3 / gist:3668650
Created September 7, 2012 19:03
Classes
import random
from collections import namedtuple
COUNTRIES = ("Norway", "Sweden", "Germany", "Egypt", "South Africa", "Canada",
"China", "Brazil", "Mexico")
# Normally, I'd store key/value pairs as a dictionary for the next two globals,
# but in this case, it wouldn't help since we don't do any lookups.
CONDITIONS = (("Cancer", 30), ("AIDS", 40), ("Common Cold", 3),
("Healthy", 0))