coding for food
-
[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 ----------------------------------..
-
[Ruby] .map // .selectcoding for food 2022. 8. 6. 03:40
# Array-Giving Enumerables # map # select arr = ["a", "b", "c", "d"] new_arr = arr.map { |ele| ele.upcase + "!" } print new_arr ----------------------------- #=> ["A!", "B!", "C!", "D!"] num = [1,2,3,4,5,6] new_num = num.select { |ele| ele % 2 == 0 } print new_num -------------------- #=> [2, 4, 6]
-
[Ruby] exercise _ Unique Elementscoding for food 2022. 8. 5. 14:21
Write a method unique_elements that takes in an array and returns a new array where all duplicate elements are removed. Solve this using a hash. # Hint: all keys of a hash are automatically unique def unique_elements(arr) end print unique_elements(['a', 'b', 'a', 'a', 'b', 'c']) #=> ["a", "b", "c"] puts my code # Hint: all keys of a hash are automatically unique def unique_elements(arr) new_arr ..
-
[Ruby] exercise _ Hash To Pairscoding for food 2022. 8. 5. 12:57
Write a method hash_to_pairs that takes in a hash and returns a 2D array representing each key-value pair of the hash. def hash_to_pairs(hash) end print hash_to_pairs({"name"=>"skateboard", "wheels"=>4, "weight"=>"7.5 lbs"}) #=> [["name", "skateboard"], ["wheels", 4], ["weight", "7.5 lbs"]] puts print hash_to_pairs({"kingdom"=>"animalia", "genus"=>"canis", "breed"=>"German Shepherd"}) #=> [["kin..
-
[Ruby] exercise _ Frequent Letters (복습할 것)coding for food 2022. 8. 5. 12:26
Write a method frequent_letters that takes in a string and returns an array containing the characters that appeared more than twice in the string. def frequent_letters(string) end print frequent_letters('mississippi') #=> ["i", "s"] puts print frequent_letters('bootcamp') #=> [] puts def frequent_letters(string) twice = [] count = Hash.new(0) string.each_char do |char| count[char] += 1 end count..
-
[Ruby] exercise _ Hand Scorecoding for food 2022. 8. 5. 10:59
Write a method hand_score that takes in a string representing a hand of cards and returns it's total score. You can assume the letters in the string are only A, K, Q, J. A is worth 4 points, K is 3 points, Q is 2 points, and J is 1 point. The letters of the input string not necessarily uppercase. def hand_score(hand) end puts hand_score("AQAJ") #=> 11 puts hand_score("jJka") #=> 9 my code def ha..
-
[Ruby] exercise _ Element Countcoding for food 2022. 8. 5. 10:00
Write a method element_count that takes in an array and returns a hash representing the count of each element in the array. def element_count(arr) end puts element_count(["a", "b", "a", "a", "b"]) #=> {"a"=>3, "b"=>2} puts element_count(["red", "red", "blue", "green"]) #=> {"red"=>2, "blue"=>1, "green"=>1} my code def element_count(arr) count = Hash.new(0) arr.each do |char| count[char] += 1 end..