ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • [Ruby] exercise _ Word Lengths
    coding for food 2022. 8. 5. 04:50

    Write a method word_lengths that takes in a sentence string and returns a hash where every key is a word of the sentence, and its' corresponding value is the length of that word.

    def word_lengths(sentence)
    
    end
    
    puts word_lengths("this is fun") #=> {"this"=>4, "is"=>2, "fun"=>3}
    puts word_lengths("When in doubt, leave it out") #=> {"When"=>4, "in"=>2, "doubt,"=>6, "leave"=>5, "it"=>2, "out"=>3}
    def word_lengths(sentence)
      words = sentence.split(" ")
      lengths = {}
    
      words.each { |word| lengths[word] = word.length}
    
      return lengths
    end
    
    puts word_lengths("this is fun") #=> {"this"=>4, "is"=>2, "fun"=>3}
    puts word_lengths("When in doubt, leave it out") #=> {"When"=>4, "in"=>2, "doubt,"=>6, "leave"=>5, "it"=>2, "out"=>3}

    중요한 개념 잊지말자..

    hash = {}
    hash["this"] = 4
    p hash
    ------------------
    #=>
    {"this"=>4}


     

Designed by Tistory.