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 Guessing_game | |
VALID_Numbers = (1..100).to_a # Store valid answers in an array | |
def initialize answer; @answer = answer | |
@solved = false | |
# Validate input | |
raise "Answer must be between 1 and 100" unless VALID_Numbers.include? @answer | |
end | |
def guess ( number ) |
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
# TODO: Print the elements at indices 1, 3, 5, 7, etc. on separate lines. | |
# You should make use of Enumerable#each_with_index | |
def print_odd_indexed_integers(array) | |
array.each_with_index { |key,value| | |
if value.odd? | |
puts value.to_s | |
end | |
} | |
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
#Initialize the delimeter pattern | |
SSN_PATERN=/\d{3}-\d{2}-\d{4}/ | |
SSN_ANY_PATERN=/(?<first)\d{3})\W?(?<second>\d{2})\W?(?<third>\d{4})/ | |
# Determine whether a string contains a Social Security number. | |
def has_ssn?(string) | |
string.match(SSN_PATERN) ? true : false | |
end | |
puts "has_ssn? returns true if it has what looks like a SSN" |
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 destroy_message!(string) | |
#TODO: remove the message from string destructively! | |
new_string=string[/[\W|\w]*:/] # '*:' is the whole thing b4 the * + inclusive of * | |
if string.include? ':' | |
string.sub!(string,new_string) | |
else | |
string | |
end | |
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 benchmark | |
# Your benchmarking code goes here. | |
start_time=Time.now | |
yield # yield (long_string.reverse) ==> execute command in {} | |
end_time=Time.now | |
end_time-start_time #return back to running_time below |
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 dictionary_sort(arr) | |
# Your code here to sort the array | |
arr.sort! | |
end | |
# Added a method HERE // for checking if it is a integer | |
def isint(str) | |
return !!(str =~ /^[-+]?[1-9]([0-9]*)?$/) | |
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
# 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
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 |
OlderNewer