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.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 |
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.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 |
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
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 |
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
# 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 |
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
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 |
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
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 |
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
# 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"]] | |
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
#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 | |
} |
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
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 |
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
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! |