Last active
August 29, 2015 14:26
-
-
Save blake41/f7e0235f89093052a02a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Count words in a sentence | |
require 'pry-nav' | |
require "pry" | |
require 'ap' | |
paragraph = <<-something | |
Steven likes the movies. Blake likes to ride his bike but hates movies. | |
Blake is taller than steven. Steven is a great teacher. | |
something | |
def parse_paragraph(paragraph) | |
paragraph.downcase.gsub!(".","").split(" ") | |
end | |
def letter_cost | |
alpha_num = {} | |
"a".upto("z").each.with_index(1) do |letter, index| | |
alpha_num[letter] = index | |
end | |
alpha_num | |
end | |
def word_cost(word,alpha_num) | |
word.split("").reduce(0) do |acc, letter| | |
acc += alpha_num[letter] | |
end | |
end | |
def word_cost_in_paragraph(paragraph) | |
cost = Hash.new(0) | |
word_list = parse_paragraph(paragraph) | |
word_list.each do |word| | |
cost[word] += word_cost(word, letter_cost) | |
end | |
cost | |
end | |
word_costs = word_cost_in_paragraph(paragraph) | |
binding.pry |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment