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 canonical(word)
# MAgic goes here
# Downcase, then group & replace it Then split it ... lastly sort it
word.downcase.gsub(/[^a-z]+/).sort
end
def is_anagram?(word1, word2)
canonical(word1)==canonical(word2)
end
# 1. - Read the word in the array
# 1.1 Check the length of words(array) are the same
# 1.2 Check capatilization
# 1.3 Break the words into individual array
# 1.4 Sort the alpabatical order (both, array&words)
# 1.5 Compare the list
# 2. - print out the same words that = array[word]
# include anagram_for method here
# Define function
#/ 1.Ask for input
# 2.Check the length of words
# 2.1 return false if length !=
# 2.2 string.length==string2.length
# 3. Check for capitalization
# string.downcase
# 4. Break the word into individual Array
# string.split!
def linear_search(num, array_num)
counter=0
until array_num[counter]==num || array_num[counter] == nil
counter+=1
end
puts array_num[counter]==num ? counter : "nil"
end
def checking(num1, num2)
return true if num1==num2