Skip to content

Instantly share code, notes, and snippets.

@djGrill
Created September 7, 2017 17:32
Show Gist options
  • Save djGrill/928264147fff5cb71e2333c21e596d6f to your computer and use it in GitHub Desktop.
Save djGrill/928264147fff5cb71e2333c21e596d6f to your computer and use it in GitHub Desktop.
Challenges
# Where n is a positive integer, the function f(n) satisfies the following:
#
# f(0) = 0
# f(1) = 1
# f(n) = f(n - 1) + f(n - 2)
#
# Create a program to find f(n)
def super_sum(number)
results = [0, 1]
(2..number).each { |n| results[n] = results[n - 1] + results[n - 2] }
return results[number]
end
# Implement a program that lists the nodes of a random binary tree by nodes at the same depth
class Node
attr_accessor :value
attr_accessor :node_0
attr_accessor :node_1
def initialize(value)
@value = value
end
end
def list_nodes_by_depth(node, depth = 0)
@nodes ||= {}
return if node.nil?
@nodes[depth] ||= []
@nodes[depth] << node.value
list_nodes_by_depth(node.node_0, depth + 1)
list_nodes_by_depth(node.node_1, depth + 1)
return @nodes
end
# Imagine you are playing a board game. You roll a 6-faced dice and move forward the same
# number of spaces that you rolled. If the finishing point is "n" spaces away from the starting
# point, please implement a program that calculates how many possible ways there are to
# arrive exactly at the finishing point
def ultra_sum(number)
results = [1, 1, 2, 4, 8, 16]
return results[number] if number < 6
(6..number).each do |n|
results[n] = results[n - 1] +
results[n - 2] +
results[n - 3] +
results[n - 4] +
results[n - 5] +
results[n - 6]
end
return results[number]
end
puts ultra_sum(ARGV[0].to_i)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment