Blocks

Sort by

recency

|

96 Discussions

|

  • + 0 comments
    def factorial(n)
        total = n
        for i in 1...n do
            total *= i
        end
        
        yield total
    end
    
    n = gets.to_i
    factorial(n) do |num| 
        puts num
    end
    
  • + 0 comments
    def factorial(n)
        yield(n)
    end
    n = gets.to_i
    puts factorial(n) { (1..n).reduce(1, :*) }
    
  • + 0 comments
    def factorial(n, i)
        yield(n, i)
    end
    
    n = gets.to_i
    i = n - 1
    puts factorial(n, i) { 
        while i != 0
            n = n * i
            i -= 1
        end
        n
    }
    
  • + 0 comments

    Here is blocks problem solution - https://www.gyangav.com/2022/10/hackerrank-ruby-blocks-problem-solution.html

  • + 0 comments
    def factorial
        yield
    end
    
    n = gets.to_i
    factorial do 
        puts (1..n).reduce(:*)
    end