Last active
September 26, 2017 08:49
-
-
Save kanglicheng/8a194d988a5954d2a0b2e0ac5d728c71 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
#METHODS | |
# EASY | |
# Write a method that returns its argument converted to a string. | |
def my_to_s(arg) | |
return arg.to_s | |
end | |
# Write a method that returns its argument rounded to the nearest integer. | |
def my_round(num) | |
num.round | |
end | |
# Write a method that returns the remainder of its two arguments. | |
# You may use the modulo operator. | |
def my_modulo(dividend, divisor) | |
return dividend % divisor | |
end | |
# Write a method that returns the least common multiple of its two arguments. | |
# You may use the lcm method. | |
def my_lcm(int_one, int_two) | |
int_two.lcm(int_one) | |
end | |
# Write a method that returns its argument converted to a float then | |
# converted to a string. | |
def to_stringified_float(int) | |
a = int.to_f | |
return a.to_s | |
end | |
# Write a method that returns the sum of the absolute values of its arguments. | |
def absolute_sum(num_one, num_two) | |
n1 = num_one.abs | |
n2= num_two.abs | |
return (n1+n2) | |
end | |
# Write a method that returns the negative value of its argument. | |
# If the argument is negative, the method simply returns the argument. | |
# (negative(-1) => -1, negative(1) => -1, negative(0) => 0) | |
# HINT: use the abs method | |
def negative(num) | |
#-num if num >0 else num | |
num > 0 ? -num : num | |
end | |
# MEDIUM | |
# Write a method that returns the last digit of its argument. | |
# Assume the argument is an integer. | |
# HINT: What is the return value of 142 % 10? How about 2 % 10? | |
def last_digit(int) | |
return int%10 | |
end | |
# Write a method that returns a boolean indicating whether | |
# the last digit of the method's argument is odd. | |
# Assume the argument is an integer. | |
# Bonus points if you use last_digit as a helper method. | |
def last_digit_odd?(int) | |
n = last_digit(int) | |
return !(n%2 == 0) | |
end | |
# Write a method that returns the greatest common divisor of the last | |
# digit of each of its arguments. Assume the arguments are integers. | |
# (gcd_of_last_digits(93, 9) = 3.gcd(9) => 3) | |
# Bonus points if you use last_digit as a helper method. | |
def gcd_of_last_digits(int_one, int_two) | |
first = last_digit(int_one) | |
second = last_digit(int_two) | |
return first.gcd(second) | |
end | |
# Write a method that returns the last n digits of its first argument, | |
# where n is the second argument. | |
# Assume both arguments are non-zero integers. | |
# (last_n_digits(1234, 2) => 34) | |
# HINT: What is the return value of 1234 % 100? How about 4 % 100? | |
def last_n_digits(num, n) | |
divisor = 10**n | |
num % divisor | |
end | |
#HARD | |
# Write a method that returns the decimal remainder of dividing two floats. | |
# The decimal remainder is the right side of the decimal point | |
# (the "fractional part") of the quotient. | |
# (dec_remainder_of_two_floats(8.0, 5.0) => 0.6 because 8.0 / 5.0 => 1.6) | |
def dec_remainder_of_two_floats(f_dividend, f_divisor) | |
ans = f_dividend/f_divisor | |
ans -= ans.floor | |
end | |
# Write a method that returns the decimal remainder of dividing two integers. | |
# HINT: Use dec_remainder_of_two_floats as a helper method, | |
# but don't forget to pass the proper type of argument | |
def dec_remainder_of_two_integers(i_dividend, i_divisor) | |
ans = i_dividend.to_f/i_divisor.to_f | |
ans -= ans.floor | |
end | |
# EXPERT | |
# Write a method that returns the integer remainder of its two arguments. | |
# (i.e., what using the modulo operator would return). | |
# You may not use the modulo operator. | |
# Assume the arguments are integers. | |
# HINT: Use dec_remainder_of_two_integers as a helper method | |
def int_remainder_without_modulo(i_dividend, i_divisor) | |
end | |
require_relative "test.rb" | |
#begin test file for "METHODS" | |
$success_count = 0 | |
$failure_count = 0 | |
def deep_dup(arr) | |
arr.inject([]) { |acc, el| el.is_a?(Array) ? acc << deep_dup(el) : acc << el } | |
end | |
def note_success(returned, invocation, expectation) | |
puts "success: #{invocation} => #{expectation}" | |
$success_count += 1 | |
end | |
def note_failure(returned, invocation, expectation) | |
puts "failure: #{invocation}: expected #{expectation}, returned #{returned}" | |
$failure_count += 1 | |
end | |
def format_args(args) | |
o_args = deep_dup(args) | |
o_args.map! do |arg| | |
prettify(arg) | |
end | |
o_args.join(', ') | |
end | |
def prettify(statement) | |
case statement | |
when Float | |
statement.round(5) | |
when String | |
"\"#{statement}\"" | |
when NilClass | |
"nil" | |
else | |
statement | |
end | |
end | |
def equality_test(returned, invocation, expectation) | |
if returned == expectation && returned.class == expectation.class | |
note_success(returned, invocation, expectation) | |
else | |
note_failure(returned, invocation, expectation) | |
end | |
end | |
def identity_test(returned, invocation, expectation, args) | |
if returned.__id__ == args[0].__id__ | |
equality_test(returned, invocation, expectation) | |
else | |
puts "failure: #{invocation}: You did not mutate the original array!" | |
$failure_count += 1 | |
end | |
end | |
def method_missing(method_name, *args) | |
method_name = method_name.to_s | |
expectation = args[-1] | |
args = args[0...-1] | |
if method_name.start_with?("test_") | |
tested_method = method_name[5..-1] | |
print_test(tested_method, args, expectation) | |
else | |
method_name = method_name.to_sym | |
super | |
end | |
end | |
def print_test(method_name, args, expectation) | |
returned = self.send(method_name, *args) | |
returned = prettify(returned) | |
expectation = prettify(expectation) | |
args_statement = format_args(args) | |
invocation = "#{method_name}(#{args_statement})" | |
method_name.include?("!") ? identity_test(returned, invocation, expectation, args) : equality_test(returned, invocation, expectation) | |
rescue Exception => e | |
puts "failure: #{invocation} threw #{e}" | |
puts e.backtrace.select {|t| !t.include?("method_missing") && !t.include?("print_test")} | |
$failure_count += 1 | |
end | |
puts "\nmy_to_s:\n" + "*" * 15 + "\n" | |
test_my_to_s(3, "3") | |
test_my_to_s(nil, "") | |
puts "\nmy_round:\n" + "*" * 15 + "\n" | |
test_my_round(3.4, 3) | |
test_my_round(3.5, 4) | |
puts "\nmy_modulo:\n" + "*" * 15 + "\n" | |
test_my_modulo(9, 2, 1) | |
test_my_modulo(10, 4, 2) | |
puts "\nmy_lcm:\n" + "*" * 15 + "\n" | |
test_my_lcm(9, 3, 9) | |
test_my_lcm(4, 6, 12) | |
puts "\nto_stringified_float:\n" + "*" * 15 + "\n" | |
test_to_stringified_float(2, "2.0") | |
test_to_stringified_float(0, "0.0") | |
puts "\nabsolute_sum:\n" + "*" * 15 + "\n" | |
test_absolute_sum(2, -2, 4) | |
test_absolute_sum(2, 2, 4) | |
puts "\nnegative:\n" + "*" * 15 + "\n" | |
test_negative(-1, -1) | |
test_negative(0, 0) | |
puts "\nlast_digit:\n" + "*" * 15 + "\n" | |
test_last_digit(1234, 4) | |
test_last_digit(0, 0) | |
puts "\nlast_digit_odd?:\n" + "*" * 15 + "\n" | |
test_last_digit_odd?(1234, false) | |
test_last_digit_odd?(1233, true) | |
puts "\ngcd_of_last_digits:\n" + "*" * 15 + "\n" | |
test_gcd_of_last_digits(93, 9, 3) | |
test_gcd_of_last_digits(42, 44, 2) | |
puts "\nlast_n_digits:\n" + "*" * 15 + "\n" | |
test_last_n_digits(1234, 2, 34) | |
test_last_n_digits(1234, 1, 4) | |
puts "\ndec_remainder_of_two_floats:\n" + "*" * 15 + "\n" | |
test_dec_remainder_of_two_floats(8.0, 5.0, 0.6) | |
test_dec_remainder_of_two_floats(10.0, 4.0, 0.5) | |
puts "\ndec_remainder_of_two_integers:\n" + "*" * 15 + "\n" | |
test_dec_remainder_of_two_integers(8, 5, 0.6) | |
test_dec_remainder_of_two_integers(10, 4, 0.5) | |
puts "\nint_remainder_without_modulo:\n" + "*" * 15 + "\n" | |
test_int_remainder_without_modulo(8, 5, 3) | |
test_int_remainder_without_modulo(10, 4, 2) | |
puts | |
puts "TOTAL CORRECT: #{$success_count} / #{$success_count + $failure_count}" | |
puts "TOTAL FAILURES: #{$failure_count}" | |
$success_count = 0 | |
$failure_count = 0 | |
#end test file for "METHODS" | |
# begin solutions file for "METHODS" | |
#For solutions not found here, see the "walkthrough" section of the curriculum. | |
def num_vowels(str) | |
i = 0 | |
count = 0 | |
while i < str.length | |
if is_vowel?(str[i]) | |
count += 1 | |
end | |
i += 1 | |
end | |
count | |
end | |
def devowel(str) | |
new_str = "" | |
i = 0 | |
while i < str.length | |
new_str += str[i] unless is_vowel?(str[i]) | |
i += 1 | |
end | |
new_str | |
end | |
def is_vowel?(letter) | |
vowels = ["a", "e", "i", "o", "u"] | |
vowels.include?(letter) | |
end | |
def repeating_letters?(str) | |
new_str = str.downcase | |
i = 0 | |
while i < str.length - 1 | |
if new_str[i] == new_str[i + 1] | |
return true | |
end | |
i += 1 | |
end | |
false | |
end | |
def my_rotate(arr, offset=1) | |
split_idx = offset % arr.length | |
arr.drop(split_idx) + arr.take(split_idx) | |
end | |
#end solution file for "METHODS" | |
# DATA STRUCTURES | |
# EASY | |
# Write a method that returns a boolean indicating whether an array is in sorted order. Use the equality operator (==), which returns a boolean indicating whether its operands are equal, e.g., 2 == 2 => true, ["cat", "dog"] == ["dog", "cat"] => false | |
def in_order?(arr) | |
ans = true | |
for i in 0 ... arr.length-1 | |
if arr[i] > arr[i+1] | |
ans = false | |
end | |
end | |
ans | |
end | |
# MEDIUM | |
# Write a method that returns the range of its argument (an array of integers). | |
def range(arr) | |
arr.max - arr.min | |
end | |
# Write a method that returns the returns an array of the digits of a non-negative integer in descending order and as strings, e.g., descending_digits(4291) #=> ["9", "4", "2", "1"] | |
def descending_digits(int) | |
end | |
#NB: Writing the following 3 methods will require the use of techniques found in the "Logic and Control Flow" section of the curriculum. Feel free to come back to these problems after reading that section. | |
# Write a method that returns the number of vowels in its argument | |
def num_vowels(str) | |
vowels = "AEIOUaeiou" | |
count =0 | |
for i in 0 ... str.length | |
if vowels.index(str[i]) != nil | |
count +=1 | |
end | |
end | |
count | |
end | |
# Write a method that returns its argument with all its vowels removed | |
def devowel(str) | |
ans = "" | |
vowels = "AEIOUaeiou" | |
for i in 0 ... str.length | |
if vowels.index(str[i]) == nil | |
ans += str[i] | |
end | |
end | |
ans | |
end | |
# Write a method that returns a boolean indicating whether a string has repeating letters. Capital letters count as repeats of lowercase ones, e.g., repeating_letters?("Aa") => true | |
def repeating_letters?(str) | |
holder = [] | |
str1= str.downcase | |
for i in 0 ... str.length | |
if holder.index(str1[i]) == nil | |
holder.push(str1[i]) | |
else | |
return true | |
end | |
end | |
return false | |
end | |
# HARD | |
# Write a method that converts an array of ten integers into a phone number in the format "(123) 456-7890". | |
def to_phone_number(arr) | |
part1= arr[0 .. 2] | |
part2=arr[3 .. 5] | |
part3= arr[6 .. 9] | |
"("+part1.join+") "+part2.join+"-"+part3.join | |
end | |
# Write a method that returns the range of a string of comma-separated integers, e.g., str_range("4,1,8") #=> 7 | |
def str_range(str) | |
arr = str.split(",") | |
arr.map! { |elt| elt.to_i } | |
#arr.map! do |el| | |
# el.to_i | |
#end | |
arr.max - arr.min | |
end | |
#EXPERT | |
# Write a method that is functionally equivalent to the rotate(offset) method of arrays. offset=1 ensures that the value of offset is 1 if no argument is provided. | |
# HINT: use the take(num) and drop(num) methods. You won't need much code, but the solution is tricky! | |
def my_rotate(arr, offset=1) | |
# your code goes here | |
end | |
require_relative "test.rb" | |
#begin test file for "DATA STRUCTURES" | |
$success_count = 0 | |
$failure_count = 0 | |
def deep_dup(arr) | |
arr.inject([]) { |acc, el| el.is_a?(Array) ? acc << deep_dup(el) : acc << el } | |
end | |
def note_success(returned, invocation, expectation) | |
puts "success: #{invocation} => #{expectation}" | |
$success_count += 1 | |
end | |
def note_failure(returned, invocation, expectation) | |
puts "failure: #{invocation}: expected #{expectation}, returned #{returned}" | |
$failure_count += 1 | |
end | |
def format_args(args) | |
o_args = deep_dup(args) | |
o_args.map! do |arg| | |
arg = prettify(arg) | |
arg.class == Array ? arg.to_s : arg | |
end | |
o_args.join(', ') | |
end | |
def prettify(statement) | |
case statement | |
when Float | |
statement.round(5) | |
when String | |
"\"#{statement}\"" | |
when NilClass | |
"nil" | |
else | |
statement | |
end | |
end | |
def equality_test(returned, invocation, expectation) | |
if returned == expectation && returned.class == expectation.class | |
note_success(returned, invocation, expectation) | |
else | |
note_failure(returned, invocation, expectation) | |
end | |
end | |
def identity_test(returned, invocation, expectation, args) | |
if returned.__id__ == args[0].__id__ | |
equality_test(returned, invocation, expectation) | |
else | |
puts "failure: #{invocation}: You did not mutate the original array!" | |
$failure_count += 1 | |
end | |
end | |
def method_missing(method_name, *args) | |
method_name = method_name.to_s | |
expectation = args[-1] | |
args = args[0...-1] | |
if method_name.start_with?("test_") | |
tested_method = method_name[5..-1] | |
print_test(tested_method, args, expectation) | |
else | |
method_name = method_name.to_sym | |
super | |
end | |
end | |
def print_test(method_name, args, expectation) | |
returned = self.send(method_name, *args) | |
returned = prettify(returned) | |
expectation = prettify(expectation) | |
args_statement = format_args(args) | |
invocation = "#{method_name}(#{args_statement})" | |
method_name.include?("!") ? identity_test(returned, invocation, expectation, args) : equality_test(returned, invocation, expectation) | |
rescue Exception => e | |
puts "failure: #{invocation} threw #{e}" | |
puts e.backtrace.select {|t| !t.include?("method_missing") && !t.include?("print_test")} | |
$failure_count += 1 | |
end | |
puts "\nin_order?:\n" + "*" * 15 + "\n" | |
test_in_order?(["a", "z", "c"], false) | |
test_in_order?([0, 1, 2], true) | |
puts "\nrange:\n" + "*" * 15 + "\n" | |
test_range([-1, 5, 0], 6) | |
test_range([0, 0], 0) | |
puts "\ndescending_digits:\n" + "*" * 15 + "\n" | |
test_descending_digits(4291, ["9", "4", "2", "1"]) | |
test_descending_digits(0, ["0"]) | |
puts "\nnum_vowels:\n" + "*" * 15 + "\n" | |
test_num_vowels("AEiouz", 5) | |
test_num_vowels("ghf", 0) | |
puts "\ndevowel:\n" + "*" * 15 + "\n" | |
test_devowel("I have vowels", " hv vwls") | |
test_devowel("ghf", "ghf") | |
puts "\nrepeating_letters?:\n" + "*" * 15 + "\n" | |
test_repeating_letters?("aA", true) | |
test_repeating_letters?("abcd", false) | |
puts "\nto_phone_number:\n" + "*" * 15 + "\n" | |
test_to_phone_number([1,2,3,4,5,6,7,8,9,0], "(123) 456-7890") | |
puts "\nstr_range:\n" + "*" * 15 + "\n" | |
test_str_range("4,1,8", 7) | |
test_str_range("0,0", 0) | |
puts "\nmy_rotate:\n" + "*" * 15 + "\n" | |
test_my_rotate(["a", "b", "c", "d"], ["b", "c", "d", "a"]) | |
test_my_rotate(["a", "b", "c", "d"], 2, ["c", "d", "a", "b"]) | |
test_my_rotate(["a", "b", "c", "d"], -3, ["b", "c", "d", "a"]) | |
test_my_rotate(["a", "b", "c", "d"], 15, ["d", "a", "b", "c"]) | |
puts "\n" | |
puts "TOTAL CORRECT: #{$success_count} / #{$success_count + $failure_count}" | |
puts "TOTAL FAILURES: #{$failure_count}" | |
$success_count = 0 | |
$failure_count = 0 | |
#end test file for "DATA STRUCTURES" | |
#begin solutions file for "DATA STRUCTURES" | |
#For solutions not found here, see the "walkthrough" section of the curriculum. | |
def num_vowels(str) | |
i = 0 | |
count = 0 | |
while i < str.length | |
if is_vowel?(str[i]) | |
count += 1 | |
end | |
i += 1 | |
end | |
count | |
end | |
def devowel(str) | |
new_str = "" | |
i = 0 | |
while i < str.length | |
new_str += str[i] unless is_vowel?(str[i]) | |
i += 1 | |
end | |
new_str | |
end | |
def is_vowel?(letter) | |
vowels = ["a", "e", "i", "o", "u"] | |
vowels.include?(letter) | |
end | |
def repeating_letters?(str) | |
new_str = str.downcase | |
i = 0 | |
while i < str.length - 1 | |
if new_str[i] == new_str[i + 1] | |
return true | |
end | |
i += 1 | |
end | |
false | |
end | |
def my_rotate(arr, offset=1) | |
split_idx = offset % arr.length | |
arr.drop(split_idx) + arr.take(split_idx) | |
end | |
#end solutions file for "DATA STRUCTURES" | |
#CONTROL FLOW | |
# EASY | |
# Return the argument with all its uppercase characters removed. | |
def destructive_uppercase(str) | |
ans = "" | |
for i in 0 ... str.length | |
if str[i] != str[i].upcase | |
ans += str[i] | |
end | |
end | |
ans | |
end | |
# Return the middle character of a string. Return the middle two characters if the word is of even length, e.g. middle_substring("middle") => "dd", middle_substring("mid") => "i" | |
def middle_substring(str) | |
mid = str.length/2 | |
if str.length % 2 !=0 | |
return str[mid] | |
else | |
return str[mid-1]+ str[mid] | |
end | |
end | |
# Return the number of vowels in a string. | |
def num_vowels(str) | |
vowels = "AEIOUaeiou" | |
count = 0 | |
for i in 0 ... str.length | |
if vowels.include?(str[i]) | |
count +=1 | |
end | |
end | |
count | |
end | |
# Return the factoral of the argument (num!). A number's factorial is the product of all whole numbers between 1 and the number itself. Assume the argument will be > 0. | |
def factorial(num) | |
prod = num | |
while num >1 | |
num -= 1 | |
prod *= num | |
end | |
prod | |
end | |
# MEDIUM | |
# Write your own version of the join method. separator="" ensures that the default argument is "". | |
def my_join(arr, separator="") | |
output= "" | |
for i in 0 ... arr.length-1 | |
output = output+ arr[i]+separator | |
end | |
output+arr[-1] | |
end | |
# Write a method that converts its argument to weirdcase, where every odd character is lowercase and every even is uppercase, e.g. weirdcase("weirdcase") => "wEiRdCaSe" | |
def weirdcase(str) | |
ans = "" | |
for i in 0 ... str.length | |
if i % 2 ==0 | |
ans += str[i].downcase | |
else | |
ans += str[i].upcase | |
end | |
end | |
ans | |
end | |
# Reverse all words of five more more letters in a string. Return the resulting string, e.g., reverse_five("Looks like my luck has reversed") => "skooL like my luck has desrever"). | |
def reverse_five(str) | |
arr = str.split(" ") | |
ans = "" | |
for i in 0 ... arr.length | |
if arr[i].length >=5 | |
arr[i].reverse! | |
end | |
end | |
arr.join(" ") | |
end | |
# Return an array of integers from 1 to 30 (inclusive), except for each multiple of 3 replace the integer with "fizz", for each multiple of 5 replace the integer with "buzz", and for each multiple of both 3 and 5, replace the integer with "fizzbuzz". | |
def fizzbuzz | |
output = [] | |
for i in 1 .. 30 | |
if i%3 ==0 && i%5 ==0 | |
output.push("fizzbuzz") | |
elsif i%3 ==0 | |
output.push("fizz") | |
elsif i%5 == 0 | |
output.push("buzz") | |
else | |
output.push(i) | |
end | |
end | |
output | |
end | |
# HARD | |
# Write a method that returns a new array containing all the elements of the original array in reverse order. | |
def my_reverse(arr) | |
newArr= [] | |
i = arr.length-1 | |
while i >=0 | |
newArr.push(arr[i]) | |
i -= 1 | |
end | |
newArr | |
end | |
# Write a method that returns a boolean indicating whether the argument is prime. | |
def prime?(num) | |
factors = [] | |
for i in 1 .. num | |
if num%i == 0 | |
factors.push(i) | |
end | |
end | |
factors.length == 2 | |
end | |
# Write a method that returns a sorted array of the factors of its argument. | |
def factors(num) | |
factors = [] | |
for i in 1 .. num | |
if num%i == 0 | |
factors.push(i) | |
end | |
end | |
factors | |
end | |
# Write a method that returns a sorted array of the prime factors of its argument. | |
def prime_factors(num) | |
factors = [] | |
for i in 1 .. num | |
if num%i == 0 && prime?(i) | |
factors.push(i) | |
end | |
end | |
factors | |
end | |
# Write a method that returns the number of prime factors of its argument. | |
def num_prime_factors(num) | |
factors = [] | |
for i in 1 .. num | |
if num%i == 0 && prime?(i) | |
factors.push(i) | |
end | |
end | |
factors.length | |
end | |
# EXPERT | |
# Return the one integer in an array that is even or odd while the rest are of opposite parity, e.g. oddball([1,2,3]) => 2, oddball([2,4,5,6] => 5) | |
def oddball(arr) | |
# your code goes here | |
end | |
require_relative "test.rb" | |
#END CONTROL FLOW | |
#Begin test.rb for CONTROL FLOW | |
$success_count = 0 | |
$failure_count = 0 | |
def deep_dup(arr) | |
arr.inject([]) { |acc, el| el.is_a?(Array) ? acc << deep_dup(el) : acc << el } | |
end | |
def note_success(returned, invocation, expectation) | |
puts "success: #{invocation} => #{expectation}" | |
$success_count += 1 | |
end | |
def note_failure(returned, invocation, expectation) | |
puts "failure: #{invocation}: expected #{expectation}, returned #{returned}" | |
$failure_count += 1 | |
end | |
def format_args(args) | |
o_args = deep_dup(args) | |
o_args.map! do |arg| | |
arg = prettify(arg) | |
arg.class == Array ? arg.to_s : arg | |
end | |
o_args.join(', ') | |
end | |
def prettify(statement) | |
case statement | |
when Float | |
statement.round(5) | |
when String | |
"\"#{statement}\"" | |
when NilClass | |
"nil" | |
else | |
statement | |
end | |
end | |
def equality_test(returned, invocation, expectation) | |
if returned == expectation && returned.class == expectation.class | |
note_success(returned, invocation, expectation) | |
else | |
note_failure(returned, invocation, expectation) | |
end | |
end | |
def identity_test(returned, invocation, expectation, args) | |
if returned.__id__ == args[0].__id__ | |
equality_test(returned, invocation, expectation) | |
else | |
puts "failure: #{invocation}: You did not mutate the original array!" | |
$failure_count += 1 | |
end | |
end | |
def method_missing(method_name, *args) | |
method_name = method_name.to_s | |
expectation = args[-1] | |
args = args[0...-1] | |
if method_name.start_with?("test_") | |
tested_method = method_name[5..-1] | |
print_test(tested_method, args, expectation) | |
else | |
method_name = method_name.to_sym | |
super | |
end | |
end | |
def print_test(method_name, args, expectation) | |
returned = self.send(method_name, *args) | |
returned = prettify(returned) | |
expectation = prettify(expectation) | |
args_statement = format_args(args) | |
invocation = "#{method_name}(#{args_statement})" | |
method_name.include?("!") ? identity_test(returned, invocation, expectation, args) : equality_test(returned, invocation, expectation) | |
rescue Exception => e | |
puts "failure: #{invocation} threw #{e}" | |
puts e.backtrace.select {|t| !t.include?("method_missing") && !t.include?("print_test")} | |
$failure_count += 1 | |
end | |
fizzbuzzed = [1, 2, "fizz", 4, "buzz", "fizz", 7, 8, "fizz", "buzz", 11, "fizz", | |
13, 14, "fizzbuzz", 16, 17, "fizz", 19, "buzz", "fizz", 22, 23, "fizz", "buzz", | |
26, "fizz", 28, 29, "fizzbuzz"] | |
puts "\ndestructive_uppercase:\n" + "*" * 15 + "\n" | |
test_destructive_uppercase("doGgIE", "dog") | |
test_destructive_uppercase("HI", "") | |
puts "\nmiddle_substring:\n" + "*" * 15 + "\n" | |
test_middle_substring("middle", "dd") | |
test_middle_substring("mid", "i") | |
puts "\nnum_vowels:\n" + "*" * 15 + "\n" | |
test_num_vowels("Aruba", 3) | |
test_num_vowels("bcd", 0) | |
puts "\nfactorial:\n" + "*" * 15 + "\n" | |
test_factorial(4, 24) | |
test_factorial(1, 1) | |
puts "\nmy_join:\n" + "*" * 15 + "\n" | |
test_my_join(["k", "i", "t", "t", "y"], "kitty") | |
test_my_join(["dolla", "dolla"], "$", "dolla$dolla") | |
puts "\nweirdcase:\n" + "*" * 15 + "\n" | |
test_weirdcase("chamillionaire", "cHaMiLlIoNaIrE") | |
puts "\nreverse_five:\n" + "*" * 15 + "\n" | |
test_reverse_five("Looks like my luck has reversed", "skooL like my luck has desrever") | |
puts "\nfizzbuzz:\n" + "*" * 15 + "\n" | |
test_fizzbuzz(fizzbuzzed) | |
puts "\nmy_reverse:\n" + "*" * 15 + "\n" | |
test_my_reverse(["a", "b", "c"], ["c", "b", "a"]) | |
test_my_reverse(["a"], ["a"]) | |
puts "\nprime?:\n" + "*" * 15 + "\n" | |
test_prime?(1, false) | |
test_prime?(2, true) | |
test_prime?(53, true) | |
puts "\nfactors:\n" + "*" * 15 + "\n" | |
test_factors(1, [1]) | |
test_factors(18, [1, 2, 3, 6, 9, 18]) | |
puts "\nprime_factors:\n" + "*" * 15 + "\n" | |
test_prime_factors(2, [2]) | |
test_prime_factors(54, [2, 3]) | |
puts "\nnum_prime_factors:\n" + "*" * 15 + "\n" | |
test_num_prime_factors(2, 1) | |
test_num_prime_factors(54, 2) | |
test_num_prime_factors(1, 0) | |
puts "\noddball:\n" + "*" * 15 + "\n" | |
test_oddball([1,2,3], 2) | |
test_oddball([2, 4, 5, 6], 5) | |
puts | |
puts "TOTAL CORRECT: #{$success_count} / #{$success_count + $failure_count}" | |
puts "TOTAL FAILURES: #{$failure_count}" | |
$success_count = 0 | |
$failure_count = 0 | |
#end test.rb control flow | |
#BEGIN PRACTICE ASSESMENT 1 | |
# A "slippery number" has 3 as a factor or has 5 as a factor, but not both. 6 is a slippery, but 30 is not. Define a method that returns a boolean indicating whether its argument is slippery. | |
def is_slippery?(n) | |
if n%3==0 && n%5 !=0 | |
return true | |
elsif n%5==0 && n % 3 != 0 | |
return true | |
else | |
return false | |
end | |
end | |
# Write a method that, given an integer n, returns an array of the first n slippery numbers. | |
# slippery_numbers(7) => [3, 5, 6, 9, 10, 12, 18] | |
def slippery_numbers(n) | |
results =[] | |
i = 1 | |
while results.length <n | |
if is_slippery?(i) | |
results.push(i) | |
end | |
i+=1 | |
end | |
results | |
end | |
# Define a method, #e_words(str), that accepts a string as an argument. Your method return the number of words in the string that end with the letter "e". | |
# e_words("tree") => 1 | |
# e_words("Let be be finale of seem.") => 3 | |
def e_words(str) | |
arr = str.split(" ") | |
p arr | |
count =0 | |
for word in arr | |
if word[-1] == "e" | |
count +=1 | |
end | |
end | |
count | |
end | |
# The Fibonacci Sequence follows a simple rule: the next number in the sequence is the sum of the previous two. The sequence begins with [0, 1]. One computes the third number by summing the first and second (0 + 1 == 1), yielding [0, 1, 1], one computes the fourth number by summing the second and the third, yielding [0, 1, 1, 2], and so on. | |
# Define a method, #fibs, that accepts an integer as an argument. The method should return an array of the first n Fibonacci numbers. | |
# fibs(1) => [0] | |
# fibs(6) => [0, 1, 1, 2, 3, 5] | |
def fibs(n) | |
results=[0,1] | |
if n == 1 | |
return [0] | |
elsif n == 2 | |
return results | |
end | |
for i in 3 .. n | |
results.push(results[i-3]+results[i-2]) | |
end | |
results | |
end | |
require_relative "test.rb" | |
#end practice assesment 1 | |
#begin test.rb assesment 1 | |
$success_count = 0 | |
$failure_count = 0 | |
def deep_dup(arr) | |
arr.inject([]) { |acc, el| el.is_a?(Array) ? acc << deep_dup(el) : acc << el } | |
end | |
def note_success(returned, invocation, expectation) | |
puts "success: #{invocation} => #{expectation}" | |
$success_count += 1 | |
end | |
def note_failure(returned, invocation, expectation) | |
puts "failure: #{invocation}: expected #{expectation}, returned #{returned}" | |
$failure_count += 1 | |
end | |
def format_args(args) | |
o_args = deep_dup(args) | |
o_args.map! do |arg| | |
arg = prettify(arg) | |
arg.class == Array ? arg.to_s : arg | |
end | |
o_args.join(', ') | |
end | |
def prettify(statement) | |
case statement | |
when Float | |
statement.round(5) | |
when String | |
"\"#{statement}\"" | |
when NilClass | |
"nil" | |
else | |
statement | |
end | |
end | |
def equality_test(returned, invocation, expectation) | |
if returned == expectation && returned.class == expectation.class | |
note_success(returned, invocation, expectation) | |
else | |
note_failure(returned, invocation, expectation) | |
end | |
end | |
def identity_test(returned, invocation, expectation, args) | |
if returned.__id__ == args[0].__id__ | |
equality_test(returned, invocation, expectation) | |
else | |
puts "failure: #{invocation}: You did not mutate the original array!" | |
$failure_count += 1 | |
end | |
end | |
def method_missing(method_name, *args) | |
method_name = method_name.to_s | |
expectation = args[-1] | |
args = args[0...-1] | |
if method_name.start_with?("test_") | |
tested_method = method_name[5..-1] | |
print_test(tested_method, args, expectation) | |
else | |
method_name = method_name.to_sym | |
super | |
end | |
end | |
def print_test(method_name, args, expectation) | |
returned = self.send(method_name, *args) | |
returned = prettify(returned) | |
expectation = prettify(expectation) | |
args_statement = format_args(args) | |
invocation = "#{method_name}(#{args_statement})" | |
method_name.include?("!") ? identity_test(returned, invocation, expectation, args) : equality_test(returned, invocation, expectation) | |
rescue Exception => e | |
puts "failure: #{invocation} threw #{e}" | |
puts e.backtrace.select {|t| !t.include?("method_missing") && !t.include?("print_test")} | |
$failure_count += 1 | |
end | |
puts "\nis_slippery?:\n" + "*" * 15 + "\n" | |
test_is_slippery?(6, true) | |
test_is_slippery?(30, false) | |
puts "\nslipper_numbers:\n" + "*" * 15 + "\n" | |
test_slippery_numbers(2, [3, 5]) | |
test_slippery_numbers(0, []) | |
puts "\ne_words:\n" + "*" * 15 + "\n" | |
test_e_words("loom", 0) | |
test_e_words("To be or not to be", 2) | |
puts "\nfibs:\n" + "*" * 15 + "\n" | |
test_fibs(2, [0,1]) | |
test_fibs(6, [0,1,1,2,3,5]) | |
puts | |
puts "TOTAL CORRECT: #{$success_count} / #{$success_count + $failure_count}" | |
puts "TOTAL FAILURES: #{$failure_count}" | |
$success_count = 0 | |
$failure_count = 0 | |
# end test.rb practice assesment 1 | |
# begin practice assesment 2 | |
# Define a method that reverses the digits of its argument and returns the resulting number. | |
# reverse_digits(1738) => 8371 | |
def reverse_digits(int) | |
int.to_s.reverse.to_i | |
end | |
# Define a method, #pair_product?, that accepts two arguments: an array of integers and a target_product (an integer). The method returns a boolean indicating whether any pair of elements in the array multiplied together equals that product. You cannot multiply an element by itself. An element on its own is not a product. | |
# pair_product?([3, 1, 5], 15) => true | |
def pair_product?(arr, target_product) | |
for i in 0 ... arr.length | |
for j in 0 ... arr.length | |
if i!=j && arr[i]*arr[j] == target_product | |
return true | |
end | |
end | |
end | |
false | |
end | |
# Define a method, #slice_between_vowels(word), that accepts a string as an argument. Your method should return the slice of the word between the first and last vowels of that word. Return an empty string if the word has less than 2 vowels. | |
# slice_between_vowels("serendipity") => "rendip" | |
# slice_between_vowels("train") => "" | |
# slice_between_vowels("dog") => "" | |
def slice_between_vowels(word) | |
i = 0 | |
vowels = "aeiouAEIOU" | |
pos = [] | |
while i < word.length | |
if vowels.include?(word[i]) | |
pos.push(i) | |
end | |
i += 1 | |
end | |
if pos.length <2 | |
return "" | |
end | |
else | |
f = pos[0]+1 | |
e = pos[-1] - f | |
return word.slice(f, e) | |
end | |
require_relative "test.rb" | |
#begin test.rb for practice assesment 2 | |
$success_count = 0 | |
$failure_count = 0 | |
def deep_dup(arr) | |
arr.inject([]) { |acc, el| el.is_a?(Array) ? acc << deep_dup(el) : acc << el } | |
end | |
def note_success(returned, invocation, expectation) | |
puts "success: #{invocation} => #{expectation}" | |
$success_count += 1 | |
end | |
def note_failure(returned, invocation, expectation) | |
puts "failure: #{invocation}: expected #{expectation}, returned #{returned}" | |
$failure_count += 1 | |
end | |
def format_args(args) | |
o_args = deep_dup(args) | |
o_args.map! do |arg| | |
arg = prettify(arg) | |
arg.class == Array ? arg.to_s : arg | |
end | |
o_args.join(', ') | |
end | |
def prettify(statement) | |
case statement | |
when Float | |
statement.round(5) | |
when String | |
"\"#{statement}\"" | |
when NilClass | |
"nil" | |
else | |
statement | |
end | |
end | |
def equality_test(returned, invocation, expectation) | |
if returned == expectation && returned.class == expectation.class | |
note_success(returned, invocation, expectation) | |
else | |
note_failure(returned, invocation, expectation) | |
end | |
end | |
def identity_test(returned, invocation, expectation, args) | |
if returned.__id__ == args[0].__id__ | |
equality_test(returned, invocation, expectation) | |
else | |
puts "failure: #{invocation}: You did not mutate the original array!" | |
$failure_count += 1 | |
end | |
end | |
def method_missing(method_name, *args) | |
method_name = method_name.to_s | |
expectation = args[-1] | |
args = args[0...-1] | |
if method_name.start_with?("test_") | |
tested_method = method_name[5..-1] | |
print_test(tested_method, args, expectation) | |
else | |
method_name = method_name.to_sym | |
super | |
end | |
end | |
def print_test(method_name, args, expectation) | |
returned = self.send(method_name, *args) | |
returned = prettify(returned) | |
expectation = prettify(expectation) | |
args_statement = format_args(args) | |
invocation = "#{method_name}(#{args_statement})" | |
method_name.include?("!") ? identity_test(returned, invocation, expectation, args) : equality_test(returned, invocation, expectation) | |
rescue Exception => e | |
puts "failure: #{invocation} threw #{e}" | |
puts e.backtrace.select {|t| !t.include?("method_missing") && !t.include?("print_test")} | |
$failure_count += 1 | |
end | |
puts "\nreverse_digits:\n" + "*" * 15 + "\n" | |
test_reverse_digits(1738, 8371) | |
test_reverse_digits(0, 0) | |
puts "\npair_product?:\n" + "*" * 15 + "\n" | |
test_pair_product?([3,1,5], 15, true) | |
test_pair_product?([1,5,100], 25, false) | |
puts "\nslice_between_vowels:\n" + "*" * 15 + "\n" | |
test_slice_between_vowels("serendipity", "rendip") | |
test_slice_between_vowels("le3t", "") | |
puts | |
puts "TOTAL CORRECT: #{$success_count} / #{$success_count + $failure_count}" | |
puts "TOTAL FAILURES: #{$failure_count}" | |
$success_count = 0 | |
$failure_count = 0 | |
#end test.rb for practice assesment 2 | |
#begin practice assesment 3 | |
# Define a method that reverses the digits of its argument and returns the resulting number. | |
# reverse_digits(1738) => 8371 | |
def reverse_digits(int) | |
int.to_s.reverse.to_i | |
end | |
# Define a method, #pair_product?, that accepts two arguments: an array of integers and a target_product (an integer). The method returns a boolean indicating whether any pair of elements in the array multiplied together equals that product. You cannot multiply an element by itself. An element on its own is not a product. | |
# pair_product?([3, 1, 5], 15) => true | |
def pair_product?(arr, target_product) | |
for i in 0 ... arr.length | |
for j in 0 ... arr.length | |
if i!=j && arr[i]*arr[j] == target_product | |
return true | |
end | |
end | |
end | |
false | |
end | |
# Define a method, #slice_between_vowels(word), that accepts a string as an argument. Your method should return the slice of the word between the first and last vowels of that word. Return an empty string if the word has less than 2 vowels. | |
# slice_between_vowels("serendipity") => "rendip" | |
# slice_between_vowels("train") => "" | |
# slice_between_vowels("dog") => "" | |
def slice_between_vowels(word) | |
i = 0 | |
vowels = "aeiouAEIOU" | |
pos = [] | |
while i < word.length | |
if vowels.include?(word[i]) | |
pos.push(i) | |
end | |
i += 1 | |
end | |
if pos.length <2 | |
return "" | |
end | |
else | |
f = pos[0]+1 | |
e = pos[-1] - f | |
return word.slice(f, e) | |
end | |
require_relative "test.rb" | |
#end practice assesment 3 | |
#begin test.rb practice assesment 3 | |
$success_count = 0 | |
$failure_count = 0 | |
def deep_dup(arr) | |
arr.inject([]) { |acc, el| el.is_a?(Array) ? acc << deep_dup(el) : acc << el } | |
end | |
def note_success(returned, invocation, expectation) | |
puts "success: #{invocation} => #{expectation}" | |
$success_count += 1 | |
end | |
def note_failure(returned, invocation, expectation) | |
puts "failure: #{invocation}: expected #{expectation}, returned #{returned}" | |
$failure_count += 1 | |
end | |
def format_args(args) | |
o_args = deep_dup(args) | |
o_args.map! do |arg| | |
arg = prettify(arg) | |
arg.class == Array ? arg.to_s : arg | |
end | |
o_args.join(', ') | |
end | |
def prettify(statement) | |
case statement | |
when Float | |
statement.round(5) | |
when String | |
"\"#{statement}\"" | |
when NilClass | |
"nil" | |
else | |
statement | |
end | |
end | |
def equality_test(returned, invocation, expectation) | |
if returned == expectation && returned.class == expectation.class | |
note_success(returned, invocation, expectation) | |
else | |
note_failure(returned, invocation, expectation) | |
end | |
end | |
def identity_test(returned, invocation, expectation, args) | |
if returned.__id__ == args[0].__id__ | |
equality_test(returned, invocation, expectation) | |
else | |
puts "failure: #{invocation}: You did not mutate the original array!" | |
$failure_count += 1 | |
end | |
end | |
def method_missing(method_name, *args) | |
method_name = method_name.to_s | |
expectation = args[-1] | |
args = args[0...-1] | |
if method_name.start_with?("test_") | |
tested_method = method_name[5..-1] | |
print_test(tested_method, args, expectation) | |
else | |
method_name = method_name.to_sym | |
super | |
end | |
end | |
def print_test(method_name, args, expectation) | |
returned = self.send(method_name, *args) | |
returned = prettify(returned) | |
expectation = prettify(expectation) | |
args_statement = format_args(args) | |
invocation = "#{method_name}(#{args_statement})" | |
method_name.include?("!") ? identity_test(returned, invocation, expectation, args) : equality_test(returned, invocation, expectation) | |
rescue Exception => e | |
puts "failure: #{invocation} threw #{e}" | |
puts e.backtrace.select {|t| !t.include?("method_missing") && !t.include?("print_test")} | |
$failure_count += 1 | |
end | |
puts "\nreverse_digits:\n" + "*" * 15 + "\n" | |
test_reverse_digits(1738, 8371) | |
test_reverse_digits(0, 0) | |
puts "\npair_product?:\n" + "*" * 15 + "\n" | |
test_pair_product?([3,1,5], 15, true) | |
test_pair_product?([1,5,100], 25, false) | |
puts "\nslice_between_vowels:\n" + "*" * 15 + "\n" | |
test_slice_between_vowels("serendipity", "rendip") | |
test_slice_between_vowels("le3t", "") | |
puts | |
puts "TOTAL CORRECT: #{$success_count} / #{$success_count + $failure_count}" | |
puts "TOTAL FAILURES: #{$failure_count}" | |
$success_count = 0 | |
$failure_count = 0 | |
#end test.rb practice assesment 3 | |
#begin practice assesment 4 | |
# Define a method that, given an array of numbers, returns another array with each of the argument's numbers multiplied by two. Do not modify the original array. | |
def array_times_two(arr) | |
end | |
# Define a method that, given an array of numbers, mulitplies each of its elemnets by two. This SHOULD mutate the original array! | |
def array_times_two!(arr) | |
end | |
# Define a method that substitutes all five-letter words in its argument with "#####" and returns the result. Do not consider punctuation. | |
# redact_five_letter_words("long longer longest longy") => "long longer longest #####" | |
def redact_five_letter_words(str) | |
end | |
# Define a method that takes an array of pairs (two-element arrays) and returns the pair with the greatest sum. | |
# largest_pair([[-4,0],[-2,-1],[-3,2]]) => [-3,2] | |
# largest_pair([[1,0]]) => [1,0] | |
def largest_pair(pairs_array) | |
end | |
require_relative "test.rb" | |
#end practice assesment 4 | |
#begin test.rb practice assesment 4 | |
$success_count = 0 | |
$failure_count = 0 | |
def deep_dup(arr) | |
arr.inject([]) { |acc, el| el.is_a?(Array) ? acc << deep_dup(el) : acc << el } | |
end | |
def note_success(returned, invocation, expectation) | |
puts "success: #{invocation} => #{expectation}" | |
$success_count += 1 | |
end | |
def note_failure(returned, invocation, expectation) | |
puts "failure: #{invocation}: expected #{expectation}, returned #{returned}" | |
$failure_count += 1 | |
end | |
def format_args(args) | |
o_args = deep_dup(args) | |
o_args.map! do |arg| | |
arg = prettify(arg) | |
arg.class == Array ? arg.to_s : arg | |
end | |
o_args.join(', ') | |
end | |
def prettify(statement) | |
case statement | |
when Float | |
statement.round(5) | |
when String | |
"\"#{statement}\"" | |
when NilClass | |
"nil" | |
else | |
statement | |
end | |
end | |
def equality_test(returned, invocation, expectation) | |
if returned == expectation && returned.class == expectation.class | |
note_success(returned, invocation, expectation) | |
else | |
note_failure(returned, invocation, expectation) | |
end | |
end | |
def identity_test(returned, invocation, expectation, args) | |
if returned.__id__ == args[0].__id__ | |
equality_test(returned, invocation, expectation) | |
else | |
puts "failure: #{invocation}: You did not mutate the original array!" | |
$failure_count += 1 | |
end | |
end | |
def method_missing(method_name, *args) | |
method_name = method_name.to_s | |
expectation = args[-1] | |
args = args[0...-1] | |
if method_name.start_with?("test_") | |
tested_method = method_name[5..-1] | |
print_test(tested_method, args, expectation) | |
else | |
method_name = method_name.to_sym | |
super | |
end | |
end | |
def print_test(method_name, args, expectation) | |
returned = self.send(method_name, *args) | |
returned = prettify(returned) | |
expectation = prettify(expectation) | |
args_statement = format_args(args) | |
invocation = "#{method_name}(#{args_statement})" | |
method_name.include?("!") ? identity_test(returned, invocation, expectation, args) : equality_test(returned, invocation, expectation) | |
rescue Exception => e | |
puts "failure: #{invocation} threw #{e}" | |
puts e.backtrace.select {|t| !t.include?("method_missing") && !t.include?("print_test")} | |
$failure_count += 1 | |
end | |
puts "\narray_times_two:\n" + "*" * 15 + "\n" | |
test_array_times_two([1,2,3].freeze, [2,4,6]) | |
test_array_times_two([].freeze, []) | |
puts "\narray_times_two!:\n" + "*" * 15 + "\n" | |
test_array_times_two!([1,2,3], [2,4,6]) | |
test_array_times_two!([], []) | |
puts "\nredact_five_letter_words:\n" + "*" * 15 + "\n" | |
test_redact_five_letter_words("long longer longest longy", "long longer longest #####") | |
test_redact_five_letter_words("Full fathom five thy father lies;/ Of his bones are coral made", "Full fathom five thy father lies;/ Of his ##### are ##### made") | |
puts "\nlargest_pair:\n" + "*" * 15 + "\n" | |
test_largest_pair([[1,0]], [1,0]) | |
test_largest_pair([[-4,0],[-2,-1],[-3,2]], [-3,2]) | |
puts | |
puts "TOTAL CORRECT: #{$success_count} / #{$success_count + $failure_count}" | |
puts "TOTAL FAILURES: #{$failure_count}" | |
$success_count = 0 | |
$failure_count = 0 | |
#end test.rb practice assesment 4 | |
#begin practice assesment 5 | |
# Define a method that returns a boolean indicating whether its argument is a palindrome. | |
def palindrome?(str) | |
end | |
# Define a method, boolean_to_binary(arr), that accepts an array of booleans as an argument. Your method should convert the array into a string of 1's (for true values) and 0's (for false values) and return the result. | |
# boolean_to_binary([true]) => "1" | |
# boolean_to_binary([true, false, true]) => "101" | |
def boolean_to_binary(arr) | |
end | |
# Define a method that returns the third-largest element in an array. Assume the argument has at least three elements. | |
# third_greatest([5, 9, 3, 7, 7, 2, 10]) == 7 | |
def third_largest(arr) | |
end | |
# Define a method that takes a number of minutes as its argument and returns a string formatted HH:MM. Assume the number of hours is always less than 99. | |
# time_conversion(90) => "01:30" | |
# time_conversion(0) => "00:00" | |
def time_conversion(mins) | |
end | |
require_relative "test.rb" | |
#end practice assesment 5 | |
#begin test.rb practice assesment 5 | |
$success_count = 0 | |
$failure_count = 0 | |
def deep_dup(arr) | |
arr.inject([]) { |acc, el| el.is_a?(Array) ? acc << deep_dup(el) : acc << el } | |
end | |
def note_success(returned, invocation, expectation) | |
puts "success: #{invocation} => #{expectation}" | |
$success_count += 1 | |
end | |
def note_failure(returned, invocation, expectation) | |
puts "failure: #{invocation}: expected #{expectation}, returned #{returned}" | |
$failure_count += 1 | |
end | |
def format_args(args) | |
o_args = deep_dup(args) | |
o_args.map! do |arg| | |
arg = prettify(arg) | |
arg.class == Array ? arg.to_s : arg | |
end | |
o_args.join(', ') | |
end | |
def prettify(statement) | |
case statement | |
when Float | |
statement.round(5) | |
when String | |
"\"#{statement}\"" | |
when NilClass | |
"nil" | |
else | |
statement | |
end | |
end | |
def equality_test(returned, invocation, expectation) | |
if returned == expectation && returned.class == expectation.class | |
note_success(returned, invocation, expectation) | |
else | |
note_failure(returned, invocation, expectation) | |
end | |
end | |
def identity_test(returned, invocation, expectation, args) | |
if returned.__id__ == args[0].__id__ | |
equality_test(returned, invocation, expectation) | |
else | |
puts "failure: #{invocation}: You did not mutate the original array!" | |
$failure_count += 1 | |
end | |
end | |
def method_missing(method_name, *args) | |
method_name = method_name.to_s | |
expectation = args[-1] | |
args = args[0...-1] | |
if method_name.start_with?("test_") | |
tested_method = method_name[5..-1] | |
print_test(tested_method, args, expectation) | |
else | |
method_name = method_name.to_sym | |
super | |
end | |
end | |
def print_test(method_name, args, expectation) | |
returned = self.send(method_name, *args) | |
returned = prettify(returned) | |
expectation = prettify(expectation) | |
args_statement = format_args(args) | |
invocation = "#{method_name}(#{args_statement})" | |
method_name.include?("!") ? identity_test(returned, invocation, expectation, args) : equality_test(returned, invocation, expectation) | |
rescue Exception => e | |
puts "failure: #{invocation} threw #{e}" | |
puts e.backtrace.select {|t| !t.include?("method_missing") && !t.include?("print_test")} | |
$failure_count += 1 | |
end | |
puts "\npalindrome?:\n" + "*" * 15 + "\n" | |
test_palindrome?("evitative", true) | |
test_palindrome?("revolver", false) | |
puts "\nboolean_to_binary:\n" + "*" * 15 + "\n" | |
test_boolean_to_binary([false], "0") | |
test_boolean_to_binary([false, true, false], "010") | |
puts "\nthird_largest:\n" + "*" * 15 + "\n" | |
test_third_largest([5, 9, 3, 7, 7, 2, 10], 7) | |
test_third_largest([0,0,0], 0) | |
puts "\ntime_conversion:\n" + "*" * 15 + "\n" | |
test_time_conversion(90, "01:30") | |
test_time_conversion(0, "00:00") | |
puts | |
puts "TOTAL CORRECT: #{$success_count} / #{$success_count + $failure_count}" | |
puts "TOTAL FAILURES: #{$failure_count}" | |
$success_count = 0 | |
$failure_count = 0 | |
#end test.rb practice assesment 5 | |
#begin practice assesment 6 | |
# Define a method that capitalizes each word of its argument and returns the resulting string. | |
def capitalize_each_word(str) | |
arr = str.split(" ") | |
for word in arr | |
word[0]= word[0].upcase | |
end | |
return arr.join(" ") | |
end | |
# Define a method that takes an array of integers and returns an array of the square of each. | |
# compute_squares([1, 2, 3, 4]) => [1, 4, 9, 16] | |
# compute_squares([]) => [] | |
def compute_squares(arr) | |
res = [] | |
arr.each{ |x| res.push(x**2)} | |
res | |
end | |
# Define a method that returns a boolean indicating whether any two elements in the argument array sum to 0. | |
# two_sum_to_zero?([4, 2, 6]) => false | |
# two_sum_to_zero?([-2, 5, 12, -3, 2]) => true | |
def two_sum_to_zero?(arr) | |
end | |
# Define a method that returns the longest word in its argument. | |
def longest_word(str) | |
arr = str.split(" ") | |
longest ="" | |
arr.each do |w| | |
if w.length> longest.length | |
longest = w | |
end | |
end | |
longest | |
end | |
require_relative "test.rb" | |
#end practice assesment 6 | |
#begin test.rb practice assesment 6 | |
$success_count = 0 | |
$failure_count = 0 | |
def deep_dup(arr) | |
arr.inject([]) { |acc, el| el.is_a?(Array) ? acc << deep_dup(el) : acc << el } | |
end | |
def note_success(returned, invocation, expectation) | |
puts "success: #{invocation} => #{expectation}" | |
$success_count += 1 | |
end | |
def note_failure(returned, invocation, expectation) | |
puts "failure: #{invocation}: expected #{expectation}, returned #{returned}" | |
$failure_count += 1 | |
end | |
def format_args(args) | |
o_args = deep_dup(args) | |
o_args.map! do |arg| | |
arg = prettify(arg) | |
arg.class == Array ? arg.to_s : arg | |
end | |
o_args.join(', ') | |
end | |
def prettify(statement) | |
case statement | |
when Float | |
statement.round(5) | |
when String | |
"\"#{statement}\"" | |
when NilClass | |
"nil" | |
else | |
statement | |
end | |
end | |
def equality_test(returned, invocation, expectation) | |
if returned == expectation && returned.class == expectation.class | |
note_success(returned, invocation, expectation) | |
else | |
note_failure(returned, invocation, expectation) | |
end | |
end | |
def identity_test(returned, invocation, expectation, args) | |
if returned.__id__ == args[0].__id__ | |
equality_test(returned, invocation, expectation) | |
else | |
puts "failure: #{invocation}: You did not mutate the original array!" | |
$failure_count += 1 | |
end | |
end | |
def method_missing(method_name, *args) | |
method_name = method_name.to_s | |
expectation = args[-1] | |
args = args[0...-1] | |
if method_name.start_with?("test_") | |
tested_method = method_name[5..-1] | |
print_test(tested_method, args, expectation) | |
else | |
method_name = method_name.to_sym | |
super | |
end | |
end | |
def print_test(method_name, args, expectation) | |
returned = self.send(method_name, *args) | |
returned = prettify(returned) | |
expectation = prettify(expectation) | |
args_statement = format_args(args) | |
invocation = "#{method_name}(#{args_statement})" | |
method_name.include?("!") ? identity_test(returned, invocation, expectation, args) : equality_test(returned, invocation, expectation) | |
rescue Exception => e | |
puts "failure: #{invocation} threw #{e}" | |
puts e.backtrace.select {|t| !t.include?("method_missing") && !t.include?("print_test")} | |
$failure_count += 1 | |
end | |
puts "\ncapitalize_each_word:\n" + "*" * 15 + "\n" | |
test_capitalize_each_word("capitalistic capital is a capital offense!", "Capitalistic Capital Is A Capital Offense!") | |
test_capitalize_each_word("", "") | |
puts "\ncompute_squares:\n" + "*" * 15 + "\n" | |
test_compute_squares([1,2,3,4], [1,4,9,16]) | |
test_compute_squares([], []) | |
puts "\ntwo_sum_to_zero?:\n" + "*" * 15 + "\n" | |
test_two_sum_to_zero?([4,2,6,0], false) | |
test_two_sum_to_zero?([-2,5,12,-3,2], true) | |
puts "\nlongest_word:\n" + "*" * 15 + "\n" | |
test_longest_word("Capital is reckless of the health or length of life of the laborer, unless under compulsion from society.", "compulsion") | |
test_longest_word("Enlightened statesmen will not always be at the helm.", "Enlightened") | |
puts | |
puts "TOTAL CORRECT: #{$success_count} / #{$success_count + $failure_count}" | |
puts "TOTAL FAILURES: #{$failure_count}" | |
$success_count = 0 | |
$failure_count = 0 | |
This file contains hidden or 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
#For solutions not found here, see the "walkthrough" section of the curriculum. | |
def num_vowels(str) | |
i = 0 | |
count = 0 | |
while i < str.length | |
if is_vowel?(str[i]) | |
count += 1 | |
end | |
i += 1 | |
end | |
count | |
end | |
def devowel(str) | |
new_str = "" | |
i = 0 | |
while i < str.length | |
new_str += str[i] unless is_vowel?(str[i]) | |
i += 1 | |
end | |
new_str | |
end | |
def is_vowel?(letter) | |
vowels = ["a", "e", "i", "o", "u"] | |
vowels.include?(letter) | |
end | |
def repeating_letters?(str) | |
new_str = str.downcase | |
i = 0 | |
while i < str.length - 1 | |
if new_str[i] == new_str[i + 1] | |
return true | |
end | |
i += 1 | |
end | |
false | |
end | |
def my_rotate(arr, offset=1) | |
split_idx = offset % arr.length | |
arr.drop(split_idx) + arr.take(split_idx) | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment