This file contains hidden or 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
-module(change2). | |
-export([find_variations/2]). | |
-export([count_change/1]). | |
find_variations(0, _) -> 1; % No amount given. | |
find_variations(_, []) -> 0; % No coins given. | |
find_variations(Amount, _) % Amount is impossible. | |
when Amount < 0 -> 0; | |
find_variations(Amount, [Coin|Rest]=Coins) -> |
This file contains hidden or 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
# One of several ways to do a ternary operation in Python | |
# For more discussion, see: http://drj11.wordpress.com/2007/05/25/iversons-convention-or-what-is-the-value-of-x-y/ | |
# "AB"[True] => A, likewise "AB"[False] => B | |
# In this way you can index any two item list with a test: | |
card_type = ["Low", "High"]][card_value < 10] | |
This file contains hidden or 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
#Play with dispatch ideas and guards in Python. | |
class Guard: | |
dispatch_table = [] | |
@classmethod | |
def register(cls, func, condition): | |
cls.dispatch_table += [(func, condition)] |
This file contains hidden or 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
sub get_operand { | |
my $arg = shift; | |
my $listref = @$arg; | |
if (UNIVERSAL::isa($listref, 'ARRAY')) { | |
my $x = @$listref; | |
print length($x).join(",",$x) . "\n" | |
# WTF? | |
} |
This file contains hidden or 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
#!/usr/bin/env perl | |
use strict; | |
use constant StartY => 123; | |
use constant BP => 0; | |
use constant PATTERN_BUFFER => 0x7F800; | |
sub register | |
{ | |
my $register = shift; | |
This file contains hidden or 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 group?(current, last) | |
current.created_at - last.created_at < 1.minute | |
end | |
recent_statuses.inject([]) { |results, status| | |
next results << [status] if results.empty? # Can't compare first item. | |
results << group?(status, results.last.last) ? stat : [stat] | |
} |
This file contains hidden or 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 logged_in? | |
!! DB.execute("SELECT id FROM sessions WHERE session_key=? LIMIT 1", params[:session_key]) | |
end |
This file contains hidden or 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
// Two tips from Brian Kernighan: | |
// 1. Say what you mean, simply and directly. | |
// 14. Use symbolic constants for magic numbers. (Applies to chars and strings too.) | |
// | |
namespace System | |
{ | |
public static class SentenceParser | |
{ | |
protected char BLANK = ' '; |
This file contains hidden or 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
words = [] | |
for line in sys.stdin.readlines(): | |
for word in line.split(): | |
words.append(word) | |
# VS. | |
words = [word for line in sys.stdin.readlines() for word in line.split()] |
This file contains hidden or 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
fields = re.findall("(\"[^\"\r]+\"|[^,\r]+)", row) |
OlderNewer