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.

Revisions

  1. dbc-challenges revised this gist Jan 24, 2014. 1 changed file with 0 additions and 49 deletions.
    49 changes: 0 additions & 49 deletions GPS_1_2_compare_fibonacci.rb
    Original file line number Diff line number Diff line change
    @@ -123,55 +123,6 @@ def is_fibonacci?(i, current = 1, before = 0)
    ############################################################
    ############################################################

    # BONUS: Solution 4
    # Iterative or recursive?

    # Method Description
    #
    #

    def is_fibonacci?(i)
    n1 = 5 * i**2 + 4
    n2 = n1 - 8
    is_square?(n1) or is_square?(n2)
    end

    # find floor(sqrt(i)) by nesting intervals
    def sqrt(i)
    a, b = 0, i
    while a + 1 < b
    m = (a + b) / 2
    if m**2 > i
    b = m
    else
    a = m
    end
    end
    a
    end

    # Method Description
    #
    #
    def is_square?(i)
    s = sqrt(i)
    s ** 2 == i
    end


    # Naming
    #
    #
    # Overall solution
    #
    #
    # Suggested improvements?
    #
    #
    #
    ############################################################
    ############################################################




  2. dbc-challenges revised this gist Jan 24, 2014. 1 changed file with 54 additions and 32 deletions.
    86 changes: 54 additions & 32 deletions GPS_1_2_compare_fibonacci.rb
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,14 @@
    # Your Names:
    # 1)
    # 2)
    # 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
    @@ -11,13 +19,19 @@
    # 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:
    # 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?
    # Iterative or recursive?

    # Method Description
    #
    @@ -48,11 +62,11 @@ def generate_fibonacci(num, base_num=1)
    #
    #
    #


    ############################################################
    ############################################################

    # Solution 2
    # Iterative or Recursive?
    # Iterative or recursive?

    # Method Description
    #
    @@ -79,9 +93,38 @@ def is_fibonacci?(i)
    #
    #
    #
    ############################################################
    ############################################################

    # Solution 3
    # Iterative or Recursive?

    # 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?
    #
    #
    #
    ############################################################
    ############################################################

    # BONUS: Solution 4
    # Iterative or recursive?

    # Method Description
    #
    @@ -126,30 +169,9 @@ def is_square?(i)
    #
    #
    #
    ############################################################
    ############################################################




    # Solution 4
    # 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?
    #
    #
    #
  3. dbc-challenges created this gist Nov 14, 2013.
    155 changes: 155 additions & 0 deletions GPS_1_2_compare_fibonacci.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,155 @@
    # Your Names:
    # 1)
    # 2)

    # 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:



    # 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)
    n1 = 5 * i**2 + 4
    n2 = n1 - 8
    is_square?(n1) or is_square?(n2)
    end

    # find floor(sqrt(i)) by nesting intervals
    def sqrt(i)
    a, b = 0, i
    while a + 1 < b
    m = (a + b) / 2
    if m**2 > i
    b = m
    else
    a = m
    end
    end
    a
    end

    # Method Description
    #
    #
    def is_square?(i)
    s = sqrt(i)
    s ** 2 == i
    end


    # Naming
    #
    #
    # Overall solution
    #
    #
    # Suggested improvements?
    #
    #
    #




    # Solution 4
    # 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?
    #
    #
    #