This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"""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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"""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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"""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: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"""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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
numbers = ("73167176531330624919225119674426574742355349194934" | |
"96983520312774506326239578318016984801869478851843" | |
"85861560789112949495459501737958331952853208805511" | |
"12540698747158523863050715693290963295227443043557" | |
"66896648950445244523161731856403098711121722383113" | |
"62229893423380308135336276614282806444486645238749" | |
"30358907296290491560440772390713810515859307960866" | |
"70172427121883998797908792274921901699720888093776" | |
"65727333001053367881220235421809751254540594752243" | |
"52584907711670556013604839586446706324415722155397" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) |
OlderNewer