Skip to content

Instantly share code, notes, and snippets.

@temirov
Last active November 23, 2015 17:47
Show Gist options
  • Save temirov/b95fe59456dbf0c06bc8 to your computer and use it in GitHub Desktop.
Save temirov/b95fe59456dbf0c06bc8 to your computer and use it in GitHub Desktop.
Fibonacci voting
class Fibonacci
class << self
def number(n)
return cache[n] if cache[n]
cache[n] = number(n-1) + number(n-2)
end
private
def cache
@cache ||= [0, 1]
end
end
end
class Voting
def initialize(processor:)
raise(ArgumentError, 'Processor object must respond to :number method') unless processor.respond_to?(:number)
@processor = processor
end
def vote
@processor.number(ballot)
end
private
def ballot
(1..4).to_a.sample
end
end
Voting.new(processor: Fibonacci).vote
@temirov
Copy link
Author

temirov commented Nov 6, 2015

Here is a quick voting system which will always vote from 1 to 3, using Fibonacci sequence as an internal processor

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment