Created
August 1, 2014 04:59
-
-
Save lseppala/a909b2f679b07299922a to your computer and use it in GitHub Desktop.
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 hot_potato( queue, num ) | |
| def pass_potato potato_queue | |
| passer = potato_queue.pop | |
| potato_queue.insert( 0, passer ) | |
| potato_queue | |
| end | |
| def remove_looser( q, n ) | |
| list = q | |
| n.times do |i| | |
| list = pass_potato list | |
| end | |
| list.pop | |
| list | |
| end | |
| def find_winner ( q, n ) | |
| length = q.length - 1 | |
| length.times do |i| | |
| q.length > 1 ? q = remove_looser(q, n) : q | |
| end | |
| q.first | |
| end | |
| find_winner( queue, num ) | |
| end | |
| def ruby_hot_potato( queue, num ) | |
| def pass_potato potato_queue | |
| passer = potato_queue.pop | |
| potato_queue.insert( 0, passer ) | |
| end | |
| def remove_looser( q, n ) | |
| looser_leader_queue = ( 1..n ).inject( q ) { |acc, i| pass_potato( acc ) } | |
| looser_leader_queue.pop | |
| looser_leader_queue | |
| end | |
| def find_winner ( q, n ) | |
| l = q.length | |
| a = (0..l).to_a | |
| winner = a.inject( q ) { |acc, _| acc.length > 1 ? remove_looser( acc, n) : acc } | |
| end | |
| find_winner( queue, num ) | |
| end | |
| def functional_hot_potato( queue, num ) | |
| def pass_potato potato_queue | |
| passer = potato_queue.last | |
| [passer] + potato_queue[0...-1] | |
| end | |
| def remove_looser( q, n ) | |
| looser_leader_queue = ( 1..n ).inject( q ) { |acc, i| pass_potato( acc ) } | |
| looser_leader_queue[0...-1] | |
| end | |
| def find_winner ( q, n ) | |
| winner = q.inject( q ) { |acc, i| acc.length > 1 ? remove_looser( acc, n) : acc } | |
| end | |
| find_winner( queue, num ) | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment