분류 전체보기
-
[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..
-
[Ruby] exercise _ Ae Count카테고리 없음 2022. 8. 5. 09:59
Write a method ae_count that takes in a string and returns a hash containing the number of a's and e's in the string. Assume the string contains only lowercase characters. def ae_count(str) end puts ae_count("everyone can program") #=> {"a"=>2, "e"=>3} puts ae_count("keyboard") #=> {"a"=>1, "e"=>1} solution def ae_count(str) count = {"a"=>0, "e"=>0} str.each_char do |char| if (char == "a" || cha..
-
[Ruby] exercise _ Cat Buildercoding for food 2022. 8. 5. 08:38
Write a method cat_builder that takes in a name, color, and age. The method should return a hash representing a cat with those values. def cat_builder(name_str, color_str, age_num) end print cat_builder("Whiskers", "orange", 3) #=> {"name"=>"Whiskers", "color"=>"orange", "age"=>3} puts print cat_builder("Salem", "black", 100) #=> {"name"=>"Salem", "color"=>"black", "age"=>100} puts my code def c..