coding for food
-
[Ruby] Ruby class // initialized methodcoding for food 2022. 8. 26. 02:14
class Book #created my own data type and a template for what a book is attr_accessor :title, :author, :pages end book1 = Book.new() #creating book object book1.title = "Harry Potter" book1.author = "JK Rowling" book1.pages = 400 puts book1.title 여러번 쓰는 것을 initialized method를 이용해서 간결하게 쓸 수 있다. class Book attr_accessor :title, :author, :pages def initialize(title, author, pages) @title = title @au..
-
[Ruby] switch 역할을 할 때,,coding for food 2022. 8. 25. 12:40
secret_word = "kitty" guess = "" guess_count = 0 guess_limit = 3 out_of_guesses = false while guess != secret_word && !out_of_guesses if guess_count < guess_limit puts "enter guess: " guess = gets.chomp() guess_count += 1 else out_of_guesses = true end end if out_of_guesses puts "you lose" else puts "you won!" end
-
[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..