전체 글
-
[Ruby] exercise _ All Else Equalcoding for food 2022. 8. 7. 14:08
Write a method all_else_equal that takes in an array of numbers. The method should return the element of the array that is equal to half of the sum of all elements of the array. If there is no such element, the method should return nil. def all_else_equal(arr) end p all_else_equal([2, 4, 3, 10, 1]) #=> 10, because the sum of all elements is 20 p all_else_equal([6, 3, 5, -9, 1]) #=> 3, because th..
-
[Ruby] exercise _ Adjacent Sumcoding for food 2022. 8. 7. 11:42
# doh code def adjacent_sum(arr) new_arr = [] arr.each.with_index do |num, idx| if idx !=0 new_num = arr[idx-1] + arr[idx] new_arr [10, 9, 13], because [ 3+7, 7+2, 2+11 ] puts print adjacent_sum([2, 5, 1, 9, 2, 4]) #=> [7, 6, 10, 11, 6], because [2+5, 5+1, 1+9, 9+2, 2+4] puts # solution def adjacent_sum(arr) new_arr = [] arr.each_with_index do |ele, i| if i != arr.length - 1 # I want to stop one..
-
[Ruby] exercise _ Double Letter Countcoding for food 2022. 8. 7. 10:29
Write a method that takes in a string and returns the number of times that the same letter repeats twice in a row. def double_letter_count(string) end puts double_letter_count("the jeep rolled down the hill") #=> 3 puts double_letter_count("bootcamp") #=> 1 # solution def double_letter_count(string) count = 0 string.each_char.with_index do |char, i| if string[i] == string[i + 1] count += 1 end e..
-
[Ruby] exercise _ Vowel Ciphercoding for food 2022. 8. 7. 09:57
Write a method vowel_cipher that takes in a string and returns a new string where every vowel becomes the next vowel in the alphabet. # my code def vowel_cipher(string) vowel = "aeiou" new_str = "" string.each_char do |char| if vowel.include?(char) new_idx = vowel.index(char) + 1 new_str += vowel[new_idx % 5] else new_str += char end end return new_str end puts vowel_cipher("bootcamp") #=> buutc..
-
[Ruby] exercise _ Caesar Ciphercoding for food 2022. 8. 7. 07:21
Write a method caesar_cipher that takes in a string and a number. The method should return a new string where every character of the original is shifted num characters in the alphabet. # Feel free to use this variable: # alphabet = "abcdefghijklmnopqrstuvwxyz" def caesar_cipher(str, num) end puts caesar_cipher("apple", 1) #=> "bqqmf" puts caesar_cipher("bootcamp", 2) #=> "dqqvecor" puts caesar_c..
-
[Ruby] exercise _ Triple Sequencecoding for food 2022. 8. 7. 04:21
Write a method triple_sequence that takes in two numbers, start and length. The method should return an array representing a sequence that begins with start and is length elements long. In the sequence, every element should be 3 times the previous element. Assume that the length is at least 1. def triple_sequence(start, length) end print triple_sequence(2, 4) # => [2, 6, 18, 54] puts print tripl..
-
[Ruby] exercise _ Perfect Squarecoding for food 2022. 8. 7. 03:42
Write a method perfect_square? that takes in a number and returns a boolean indicating whether it is a perfect square. A perfect square is a number that results from multiplying a number by itself. For example, 9 is a perfect square because 3 3 = 9, 25 is a perfect square because 5 5 = 25. def perfect_square?(num) end puts perfect_square?(5) #=> false puts perfect_square?(12) #=> false puts perf..