Last active
August 29, 2015 14:22
-
-
Save ddlion/3d8709ff7f3a9b5346db to your computer and use it in GitHub Desktop.
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
#1 | |
class Question1 | |
class << self | |
def case_for() | |
total = 0 | |
for i in 1..10 do | |
total += i | |
end | |
puts total | |
end | |
def case_while() | |
i = 0 | |
total = 0 | |
while i <= 10 do | |
total += i | |
i += 1 | |
end | |
puts total | |
end | |
def case_recursion(total = 0 , i = 1) | |
if i > 10 | |
puts total | |
return | |
end | |
total += i | |
i += 1 | |
case_recursion(total, i) | |
end | |
end | |
case_for | |
case_while | |
case_recursion | |
end | |
#2 | |
class Question2 | |
class << self | |
def join_two_array() | |
p %w(1 2 3).zip(%w(a b c)).flatten | |
end | |
end | |
join_two_array | |
end | |
#3 | |
class Quetion3 | |
class << self | |
def calc_fibonacci_total | |
p fibonacci(10).inject(&:+) | |
end | |
def fibonacci(n) | |
[].tap do |arr| | |
raise StandardError if n < 2 | |
arr.push 0, 1 | |
(2..n - 1).each do |i| | |
arr << arr[i - 2] + arr[i - 1] | |
end | |
end | |
end | |
end | |
calc_fibonacci_total | |
end | |
#4 | |
class Question4 | |
class << self | |
def max_pattern | |
puts [50, 2, 1, 9].permutation(4).map(&:join).max | |
end | |
end | |
max_pattern | |
end | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment