Last active
November 28, 2015 21:47
-
-
Save chukitow/7ac05e20930c10320128 to your computer and use it in GitHub Desktop.
fibonacci closest numbers
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
require 'net/http' | |
require 'json' | |
KUESKI_TOKEN = 'fbxfDZRWCxmdhxHfM-Q' | |
fibonacci_sequence = [] | |
def fibonacci(x, cache = {}) | |
return x if x <= 1 | |
cache[x] ||= fibonacci(x-1, cache) + fibonacci(x-2, cache) | |
end | |
1.upto(100) do |x| | |
fibonacci_sequence << fibonacci(x) | |
end | |
uri_get_input = URI('https://kueski.com/candidates/get_input') | |
res_get_input = Net::HTTP.post_form(uri_get_input, token: KUESKI_TOKEN) | |
api_input = JSON.parse(res_get_input.body)["value"] | |
puts "You received #{api_input}" | |
first_range = fibonacci_sequence.select{|item| item < api_input.to_i}.max | |
last_range = fibonacci_sequence[fibonacci_sequence.index(first_range)+1] | |
solution = api_input == last_range ? last_range : "#{first_range}-#{last_range}" | |
puts "The solution is #{solution}" | |
uri_send_solution = URI('https://kueski.com/candidates/send_solution') | |
res_send_solution = Net::HTTP.post_form(uri_send_solution, { token: KUESKI_TOKEN, solution: solution}) | |
puts res_send_solution.body |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment