Skip to content

Instantly share code, notes, and snippets.

@yclim95
yclim95 / stylish_code.rb
Last active January 12, 2016 16:05 — forked from nextacademy-private/stylish_code.rb
Stylish Code
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 )
# 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
#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"
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
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
@yclim95
yclim95 / dictionary_sort.rb
Last active January 12, 2016 17:24 — forked from nextacademy-private/dictionary_sort.rb
Dictionary Sort
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
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
@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"]]
@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