분류 전체보기
-
[Ruby] hashescoding for food 2022. 8. 4. 00:55
Data structure - Hashes 'key => value' pairs my_hash = { "name" => "App Academy", "color" => "red", "age" => 5 } puts my_hash["name"] my_hash["color"] = "pink" puts my_hash my_hash["age"] += 1 puts my_hash -------------------------------- #=> App Academy {"name"=>"App Academy", "color"=>"pink", "age"=>5} {"name"=>"App Academy", "color"=>"pink", "age"=>6}
-
[Ruby] Pig Latin Word (복습할 것)coding for food 2022. 8. 3. 13:25
# Pig latin translation uses the following rules: # - for words that start with a vowel, add 'yay' to the end # - for words that start with a nonvowel, move all letters before the first vowel to the end of the word and add 'ay' def pig_latin_word(word) end puts pig_latin_word("apple") # => "appleyay" puts pig_latin_word("eat") # => "eatyay" puts pig_latin_word("banana") # => "ananabay" puts pig_..
-
[Ruby] Array Translate (복습할 것)coding for food 2022. 8. 3. 08:45
def array_translate(array) end print array_translate(["Cat", 2, "Dog", 3, "Mouse", 1]); # => "CatCatDogDogDogMouse" puts print array_translate(["red", 3, "blue", 1]); # => "redredredblue" puts solution def array_translate(array) str = "" i = 0 while i < array.length ele = array[i] num = array[i + 1] num.times { str += ele } # str += ele * num 으로도 가능 i += 2 end return str end print array_translat..
-
[Ruby] Two Dimensional Translatecoding for food 2022. 8. 3. 05:39
Write a method two_d_translate that takes in a 2 dimensional array and translates it into a 1 dimensional array. You can assume that the inner arrays always have 2 elements. See the examples. def two_d_translate(arr) end arr_1 = [ ['boot', 3], ['camp', 2], ['program', 0] ] print two_d_translate(arr_1) # => [ 'boot', 'boot', 'boot', 'camp', 'camp' ] puts arr_2 = [ ['red', 1], ['blue', 4] ] print ..
-
[Ruby] Two Dimensional Sumcoding for food 2022. 8. 3. 05:14
def two_d_sum(arr) end array_1 = [ [4, 5], [1, 3, 7, 1] ] puts two_d_sum(array_1) # => 21 array_2 = [ [3, 3], [2], [2, 5] ] puts two_d_sum(array_2) # => 15 my code == solution def two_d_sum(arr) total = 0 arr.each do |sub_array| sub_array.each do |num| total += num end end return total end array_1 = [ [4, 5], [1, 3, 7, 1] ] puts two_d_sum(array_1) # => 21 array_2 = [ [3, 3], [2], [2, 5] ] puts t..
-
[Ruby] Opposite Countcoding for food 2022. 8. 3. 04:34
def opposite_count(nums) end puts opposite_count([ 2, 5, 11, -5, -2, 7 ]) # => 2 puts opposite_count([ 21, -23, 24 -12, 23 ]) # => 1 my code def opposite_count(nums) sum = 0 nums.each_with_index do |ele1, idx1| nums.each_with_index do |ele2, idx2| if idx2 > idx1 if ele1 + ele2 == 0 sum += 1 end end end end return sum end puts opposite_count([ 2, 5, 11, -5, -2, 7 ]) # => 2 puts opposite_count([ 2..
-
[Ruby] Combinationscoding for food 2022. 8. 3. 02:00
Combinations Write a method combinations that takes in an array of unique elements, the method should return a 2D array representing all possible combinations of 2 elements of the array. def combinations(arr) end print combinations(["a", "b", "c"]); # => [ [ "a", "b" ], [ "a", "c" ], [ "b", "c" ] ] puts print combinations([0, 1, 2, 3]); # => [ [ 0, 1 ], [ 0, 2 ], [ 0, 3 ], [ 1, 2 ], [ 1, 3 ], [ ..
-
[Ruby] dimensional arrayscoding for food 2022. 8. 3. 01:04
# 2 - Dimensional Arrays arr = [ ["a", "b", "c"], ["d", "e"], ["f", "g", "h"], ] print arr[2][1] ----------------- #=> g # 2 - Dimensional Arrays arr = [ ["a", "b", "c"], ["d", "e"], ["f", "g", "h"], ] arr.each do |subArr| print subArr puts subArr.each do |ele| puts ele end end --------------------------- #=> ["a", "b", "c"] a b c ["d", "e"] d e ["f", "g", "h"] f g h