전체 글
-
[Ruby] exercise _ Greatest Factor Arraycoding for food 2022. 8. 7. 02:24
Write a method greatest_factor_array that takes in an array of numbers and returns a new array where every even number is replaced with it's greatest factor. A greatest factor is the largest number that divides another with no remainder. For example the greatest factor of 16 is 8. (For the purpose of this problem we won't say the greatest factor of 16 is 16, because that would be too easy, ha) d..
-
[Ruby] exercise _ Prime Factorscoding for food 2022. 8. 7. 00:54
Write a method prime_factors that takes in a number and returns an array containing all of the prime factors of the given number. def prime_factors(num) end print prime_factors(24) #=> [2, 3] puts print prime_factors(60) #=> [2, 3, 5] puts #my code def prime_factors(num) i = 1 new_arr = [] while i [2, 3, 5] puts def prime_factors(num) prime_facts = [] (1..num).each do |i| if num % i == 0 && prim..
-
[Ruby] exercise _ Pick Primescoding for food 2022. 8. 6. 23:34
Write a method pick_primes that takes in an array of numbers and returns a new array containing only the prime numbers. def pick_primes(numbers) end print pick_primes([2, 3, 4, 5, 6]) #=> [2, 3, 5] puts print pick_primes([101, 20, 103, 2017]) #=> [101, 103, 2017] puts # my code def pick_primes(numbers) arr = [] numbers.each do |num| if is_prime(num) arr [2, 3, 5] puts print pick_primes([101, 20,..
-
[Ruby] exercise _ Primecoding for food 2022. 8. 6. 13:10
Write a method prime? that takes in a number and returns a boolean, indicating whether the number is prime. A prime number is only divisible by 1 and itself. def prime?(num) end puts prime?(2) #=> true puts prime?(5) #=> true puts prime?(11) #=> true puts prime?(4) #=> false puts prime?(9) #=> false puts prime?(-5) #=> false def prime?(num) if num < 2 return false end (2...num).each do |factor| ..
-
[Ruby] Most Vowelscoding for food 2022. 8. 6. 11:45
Write a method most_vowels that takes in a sentence string and returns the word of the sentence that contains the most vowels. def most_vowels(sentence) end print most_vowels("what a wonderful life") #=> "wonderful" # solution def most_vowels(sentence) counts = {} sentence.split.each do |word| counts[word] = vowel_count(word) end sorted = counts.sort_by { |k,v| v} return sorted[-1][0] end def vo..
-
[Ruby] Last Indexcoding for food 2022. 8. 6. 10:25
Write a method last_index that takes in a string and a character. The method should return the last index where the character can be found in the string. def last_index(str, char) end puts last_index("abca", "a") #=> 3 puts last_index("mississipi", "i") #=> 9 puts last_index("octagon", "o") #=> 5 puts last_index("programming", "m")#=> 7 # my code def last_index(str, char) word = str.split("") up..
-
[Ruby] exercise _ Map By Namecoding for food 2022. 8. 6. 04:36
Write a method map_by_name that takes in an array of hashes and returns a new array containing the names of each hash. def map_by_name(arr) end pets = [ {"type"=>"dog", "name"=>"Rolo"}, {"type"=>"cat", "name"=>"Sunny"}, {"type"=>"rat", "name"=>"Saki"}, {"type"=>"dog", "name"=>"Finn"}, {"type"=>"cat", "name"=>"Buffy"} ] print map_by_name(pets) #=> ["Rolo", "Sunny", "Saki", "Finn", "Buffy"] puts c..
-
[Ruby] .map // .select 2coding for food 2022. 8. 6. 03:58
arr = ["apple", "bootCAMP", "caRrot", "DaNce"] new_arr = arr.map { |word| word[0].upcase + word[1..-1].downcase} print new_arr ------------------------------------- #=> ["Apple", "Bootcamp", "Carrot", "Dance"] arr = ["apple", "bootCAMP", "caRrot", "DaNce"] new_arr = arr.map do |ele| first = ele[0].upcase rest = ele[1..-1].downcase first + rest end print new_arr ----------------------------------..