분류 전체보기
-
[Ruby] Nested loops _ Stringcoding for food 2022. 8. 2. 13:08
# Nested Loops arr = ["a", "b", "c", "d"] # below we iterate through all possible w/duplicates arr.each do |ele1| arr.each do |ele2| puts ele1 + ele2 end end ----------------------------- #=> aa ab ac ad ba bb bc bd ca cb cc cd da db dc dd # below we iterate through only unique pairs arr.each_with_index do |ele1, idx1| arr.each_with_index do |ele2, idx2| if idx2 > idx1 puts ele1 + ele2 puts idx1..
-
[Ruby] Nested loop _ outer / inner numbercoding for food 2022. 8. 2. 11:41
(1..3).each do |num| puts num (1..5).each do |num2| puts " " + num2.to_s # outer num과 구분하기 위해 string " " 을 삽입, string 과 number value의 결합을 위해 .to_s를 같이 써줬다. stands for to string. end end ----------------------------- #=> 1 1 2 3 4 5 2 1 2 3 4 5 3 1 2 3 4 5 (1..3).each do |num| (1..5).each do |num2| puts num.to_s + " " + num2.to_s end end -------------------------------------- #=> 1 1 1 2 1 3 1 4 ..
-
[Ruby] Rotate Array카테고리 없음 2022. 8. 2. 09:35
Write a method rotate_array that takes in an array and a number. The method should return the array after rotating the elements the specified number of times. A single rotation takes the last element of the array and moves it to the front. def rotate_array(arr, num) end print rotate_array([ "Matt", "Danny", "Mashu", "Matthias" ], 1) # => [ "Matthias", "Matt", "Danny", "Mashu" ] puts print rotate..
-
[Ruby] exercise _ Reverse Wordscoding for food 2022. 8. 2. 07:36
exercise def reverse_words(sent) end puts reverse_words('keep coding') # => 'peek gnidoc' puts reverse_words('simplicity is prerequisite for reliability') # => 'yticilpmis si etisiuqererp rof ytilibailer' my code def reverse_words(sent) newsent = [] word = sent.split(" ") word.each do |char| newword = char.reverse newsent 'peek gnidoc' puts reverse_words('simplicity is prerequisite for reliabili..
-
[Ruby] exercise _ Is Valid Emailcoding for food 2022. 8. 2. 07:07
# For simplicity, we'll consider an email valid when it satisfies all of the following: # - contains exactly one @ symbol # - contains only lowercase alphabetic letters before the @ # - contains exactly one . after the @ def is_valid_email(str) end puts is_valid_email("abc@xy.z") # => true puts is_valid_email("jdoe@gmail.com") # => true puts is_valid_email("jdoe@g@mail.com") # => false puts is_v..
-
[Ruby] exercises _ Is Valid Namecoding for food 2022. 7. 31. 01:41
# A name is valid is if satisfies all of the following: # - contains at least a first name and last name, separated by spaces # - each part of the name should be capitalized # # Hint: use str.upcase or str.downcase # "a".upcase # => "A" def is_valid_name(str) end puts is_valid_name("Kush Patel") # => true puts is_valid_name("Daniel") # => false puts is_valid_name("Robert Downey Jr") # => true pu..
-
[Ruby] exercise _ Format Namecoding for food 2022. 7. 29. 12:54
exercise # Hint: use str.upcase and str.downcase # "abc".upcase # => "ABC" def format_name(str) end puts format_name("chase WILSON") # => "Chase Wilson" puts format_name("brian CrAwFoRd scoTT") # => "Brian Crawford Scott" my code def format_name(str) names = str.split(" ") new_names = [] names.each do |name| new_names "Chase Wilson" puts format_name("brian CrAwFoRd scoTT") # => "Brian Crawford S..
-
[Ruby] EXERCISES _ Abbreviate Sentencecoding for food 2022. 7. 29. 11:13
EXERCISES Write a method abbreviate_sentence that takes in a sentence string and returns a new sentence where every word longer than 4 characters has all of it's vowels removed. def abbreviate_sentence(sent) end puts abbreviate_sentence("follow the yellow brick road") # => "fllw the yllw brck road" puts abbreviate_sentence("what a wonderful life") # => "what a wndrfl life" def abbreviate_sentenc..