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 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 |
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. - 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 |
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
# 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! |
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 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 |
OlderNewer