Skip to content

Instantly share code, notes, and snippets.

#1.0 takes in the number and a array of number
# 1.1 sort the array of number
# 1.2 check if is lower || higher (array.size)
# 1.3 keep dividing it into half each time until -1
# 1.4 Once stop get the index of the place u stop
def sort_array(array_num)
array_num.sort!
end
#1.0 takes in the number and a array of number
# 1.1 sort the array of number
# 1.2 check if is lower || higher (array.size)
# 1.3 keep dividing it into half each time until -1
# 1.4 Once stop get the index of the place u stop
def sort_array(array_num)
array_num.sort!
end
def fibonacci_iterative(n)
return 0 if n==0
return 1 if n==1
second_previous_num=0
first_previous_num=1
until n == 1
sum_of_two_num= first_previous_num + second_previous_num #if n==2, sum_of_two_num=1
second_previous_num=first_previous_num #if n==2, second_previous_num=1
# Implement an iterative version of the factorial function
def factorial_iterative(n)
total = n
until n == 1
total *= n-1 #if n=2, total=2*n(1)! elseif n=3, total=3*n(2)!
n -= 1
end
total
require 'benchmark'
def fibonacci_iterative(n)
return 0 if n==0
return 1 if n==1
second_previous_num=0
first_previous_num=1
until n == 1
@yclim95
yclim95 / next_academy_solution_Recursion_vs._Iteration,_Benchmarking_It.rb
Last active January 13, 2016 18:48
Edited to reduce the time taken to execute recursive for fibonacci
require 'benchmark'
def fibonacci_iterative(n)
return 0 if n==0
return 1 if n==1
second_previous_num=0
first_previous_num=1
until n == 1
sum_of_two_num= first_previous_num + second_previous_num #if n==2, sum_of_two_num=1
@yclim95
yclim95 / nested_arrays.rb
Last active January 14, 2016 02:11 — forked from nextacademy-private/nested_arrays.rb
Nested Arrays
# Objective 1 Chess
chessboard=[["B Rook","B Knight","B Bishop","B Queen","B King","B Bishop","B Knight","B Rook"],
["B Pawn","B Pawn","B Pawn","B Pawn","B Pawn","B Pawn","B Pawn","B Pawn"],
[],
[],
[],
[],
["W Pawn","W Pawn","W Pawn","W Pawn","W Pawn","W Pawn","W Pawn","W Pawn"],
["W Rook","W Knight","W Bishop","W Queen","W King","W Bishop","W Knight","W Rook"]]
#Tic_Tac_Toe revisited
def create_tick_tack_toe_table
tick_tack_toe_table=Array.new(3) {Array.new(3) }
choice=['X','0']
(0..2).each { |x|
(0..2).each {|y|
tick_tack_toe_table[x][y]=choice.sample
}
@yclim95
yclim95 / racer_utils.rb
Last active April 26, 2017 10:53 — forked from nextacademy-private/racer_utils.rb
R-r-r-r-r-ruby Racer!
class Dice
def initialize(sides = 6)
@sides = sides
end
# Remember: rand(N) randomly returns one of N consecutive integers, starting at 0
# So rand(N) returns a random integer in (0..N-1)
# And 1 + rand(N) returns a random integer in (1..N)
# See: http://www.ruby-doc.org/core-1.9.3/Kernel.html#method-i-rand
def roll
class BoggleBoard
LETTERS = ('A'...'Z').to_a
def initialize(size=4, length=16)
@size=size
@length=length
@board=Array.new(@size) { Array.new(@size) }
end
def shake!