Skip to content

Instantly share code, notes, and snippets.

@dbc-challenges
Last active October 22, 2021 00:28
Show Gist options
  • Save dbc-challenges/e27de88b056d3025941b to your computer and use it in GitHub Desktop.
Save dbc-challenges/e27de88b056d3025941b to your computer and use it in GitHub Desktop.
# Compare Fibonacci!
# At this point you should be good at refactoring your code to make it better,
# but do you feel comfortable determining "good" vs. "bad" code? In this exercise
# you will evaluate four different solutions to the same challenge and evaluate
# their clarity, effectiveness, and overall "good"ness.
# By the end of this challenge, you should be able to:
# 1) Confidently read other people's code and figure out how it works
# 2) Recognize good and not so good practices
# 3) Consider ways to improve solutions
# For each solution below:
# 1. Read each line of code. Write a comment above each method that explains what
# it's doing and how.
# 2. Is this an iterative or recursive solution? How can you tell?
# 3. Consider the variable and method names. What do you think about them?
# Could they be more clear?
# 4. What do you think of this code overall? Do you think it's an elegant solution?
# Why or why not?
# 5. How would you improve this solution?
# 6. Write your comments together and submit it here: https://socrates.devbootcamp.com/challenges/445
# 7. Submit feedback for your session on feedbackinator!
# Your Names:
# 1)
# 2)
############################################################
############################################################
# Solution 1
# Iterative or recursive?
# Method Description
#
#
def is_fibonacci?(num)
generate_fibonacci(num).include?(num)
end
# Method Description
#
#
def generate_fibonacci(num, base_num=1)
fibonacci_sequence = [0,1]
while base_num <= num
fibonacci_sequence << base_num
base_num += fibonacci_sequence[-2]
end
return fibonacci_sequence
end
# Naming
#
#
# Overall solution
#
#
# Suggested improvements?
#
#
#
############################################################
############################################################
# Solution 2
# Iterative or recursive?
# Method Description
#
#
def is_fibonacci?(i)
fib_array = [0, 1]
counter = 0
500.times do
fib_num = fib_array[counter] + fib_array[counter+1]
fib_array << fib_num
counter += 1
end
fib_array.include?(i)
end
# Naming
#
#
# Overall solution
#
#
# Suggested improvements?
#
#
#
############################################################
############################################################
# Solution 3
# Iterative or recursive?
# Method Description
#
#
def is_fibonacci?(i, current = 1, before = 0)
return true if current == i || i == 0
return false if current > i
is_fibonacci?(i, current + before, current)
end
# Naming
#
#
# Overall solution
#
#
# Suggested improvements?
#
#
#
############################################################
############################################################
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment