fifthcard 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,4 @@
1
+ *.gem
2
+ .bundle
3
+ Gemfile.lock
4
+ pkg/*
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in fifthcard.gemspec
4
+ gemspec
@@ -0,0 +1,6 @@
1
+ require 'bundler/gem_tasks'
2
+ require 'rake/testtask'
3
+
4
+ Rake::TestTask.new do |t|
5
+ t.libs << 'test'
6
+ end
@@ -0,0 +1,22 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "fifthcard/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "fifthcard"
7
+ s.version = Fifthcard::VERSION
8
+ s.authors = ["Kristján Pétursson"]
9
+ s.email = ["kristjan@gmail.com"]
10
+ s.homepage = ""
11
+ s.summary = %q{An algorithmic magic trick}
12
+ s.description = %q{Based on the PayPal card puzzle described at http://www.quora.com/What-s-the-hardest-puzzle-question-asked-at-PayPal}
13
+
14
+ s.rubyforge_project = "fifthcard"
15
+
16
+ s.files = `git ls-files`.split("\n")
17
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
18
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
19
+ s.require_paths = ["lib"]
20
+
21
+ s.add_dependency('deck', '>=1.1.2')
22
+ end
@@ -0,0 +1,30 @@
1
+ require_relative "fifthcard/version"
2
+ require 'deck'
3
+
4
+ module Fifthcard
5
+ def self.encode(hand)
6
+ by_suit = hand.group_by(&:suit)
7
+ big_suit, suited_cards = by_suit.find{|suit, cards| cards.size > 1}
8
+ base_card, target_card = suited_cards.sort.each_cons(2).find do |low, high|
9
+ high.to_i - low.to_i <= 6
10
+ end
11
+ counting_cards = sort_cards(hand.cards - [target_card, base_card])
12
+ diff = target_card.to_i - base_card.to_i
13
+ Hand.new.tap do |encoding|
14
+ [base_card, *counting_cards.permutation.to_a[diff - 1]].each do |card|
15
+ encoding << card
16
+ end
17
+ end
18
+ end
19
+
20
+ def self.decode(hand)
21
+ base_card = hand.cards.first
22
+ counting_cards = hand.cards[1..-1]
23
+ diff = sort_cards(counting_cards).permutation.to_a.index(counting_cards) + 1
24
+ base_card + diff
25
+ end
26
+
27
+ def self.sort_cards(cards)
28
+ cards.sort_by {|card| [card.to_i, card.suit] }
29
+ end
30
+ end
@@ -0,0 +1,3 @@
1
+ module Fifthcard
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,65 @@
1
+ require 'test/unit'
2
+ require './lib/fifthcard'
3
+
4
+ class FifthcardTest < Test::Unit::TestCase
5
+ def test_encode
6
+ hand = Hand.new
7
+ [
8
+ [10, 'Diamonds'],
9
+ [6, 'Diamonds'],
10
+ ['King', 'Diamonds'],
11
+ [6, 'Hearts'],
12
+ [9, 'Spades'],
13
+ ].each {|card| hand << Card.new(*card)}
14
+ four = Fifthcard.encode(hand)
15
+ expected = [
16
+ [6, 'Diamonds'],
17
+ [9, 'Spades'],
18
+ ['King', 'Diamonds'],
19
+ [6, 'Hearts'],
20
+ ].map {|card| Card.new(*card)}
21
+ assert_equal expected, four.cards
22
+ end
23
+
24
+ def test_encode_with_suit_tiebreaker
25
+ hand = Hand.new
26
+ [
27
+ [10, 'Diamonds'],
28
+ [6, 'Diamonds'],
29
+ ['King', 'Diamonds'],
30
+ ['King', 'Hearts'],
31
+ [9, 'Spades'],
32
+ ].each {|card| hand << Card.new(*card)}
33
+ four = Fifthcard.encode(hand)
34
+ expected = [
35
+ [6, 'Diamonds'],
36
+ ['King', 'Diamonds'],
37
+ ['King', 'Hearts'],
38
+ [9, 'Spades'],
39
+ ].map{|card| Card.new(*card)}
40
+ assert_equal expected, four.cards
41
+ end
42
+
43
+ def test_decode
44
+ hand = Hand.new
45
+ [
46
+ [6, 'Diamonds'],
47
+ [9, 'Spades'],
48
+ ['King', 'Diamonds'],
49
+ [6, 'Hearts'],
50
+ ].each {|card| hand << Card.new(*card)}
51
+ assert_equal Fifthcard.decode(hand), Card.new(10, 'Diamonds')
52
+ end
53
+
54
+ def test_sorting_cards_sorts_by_value
55
+ one = Card.new(1, 'Spades')
56
+ two = Card.new(2, 'Spades')
57
+ assert_equal [one, two], Fifthcard.sort_cards([two, one])
58
+ end
59
+
60
+ def test_sorting_cards_breaks_ties_by_suit
61
+ heart = Card.new(1, "Hearts")
62
+ spade = Card.new(1, "Spades")
63
+ assert_equal [heart, spade], Fifthcard.sort_cards([spade, heart])
64
+ end
65
+ end
metadata ADDED
@@ -0,0 +1,66 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: fifthcard
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Kristján Pétursson
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-01-11 00:00:00.000000000 -08:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: deck
17
+ requirement: &2160121560 !ruby/object:Gem::Requirement
18
+ none: false
19
+ requirements:
20
+ - - ! '>='
21
+ - !ruby/object:Gem::Version
22
+ version: 1.1.2
23
+ type: :runtime
24
+ prerelease: false
25
+ version_requirements: *2160121560
26
+ description: Based on the PayPal card puzzle described at http://www.quora.com/What-s-the-hardest-puzzle-question-asked-at-PayPal
27
+ email:
28
+ - kristjan@gmail.com
29
+ executables: []
30
+ extensions: []
31
+ extra_rdoc_files: []
32
+ files:
33
+ - .gitignore
34
+ - Gemfile
35
+ - Rakefile
36
+ - fifthcard.gemspec
37
+ - lib/fifthcard.rb
38
+ - lib/fifthcard/version.rb
39
+ - test/fifthcard_test.rb
40
+ has_rdoc: true
41
+ homepage: ''
42
+ licenses: []
43
+ post_install_message:
44
+ rdoc_options: []
45
+ require_paths:
46
+ - lib
47
+ required_ruby_version: !ruby/object:Gem::Requirement
48
+ none: false
49
+ requirements:
50
+ - - ! '>='
51
+ - !ruby/object:Gem::Version
52
+ version: '0'
53
+ required_rubygems_version: !ruby/object:Gem::Requirement
54
+ none: false
55
+ requirements:
56
+ - - ! '>='
57
+ - !ruby/object:Gem::Version
58
+ version: '0'
59
+ requirements: []
60
+ rubyforge_project: fifthcard
61
+ rubygems_version: 1.6.2
62
+ signing_key:
63
+ specification_version: 3
64
+ summary: An algorithmic magic trick
65
+ test_files:
66
+ - test/fifthcard_test.rb