Skip to content

Instantly share code, notes, and snippets.

@samueljoli
Forked from blake41/frequency.rb
Last active August 29, 2015 14:26
Show Gist options
  • Save samueljoli/a7da5d63a3adb35633c5 to your computer and use it in GitHub Desktop.
Save samueljoli/a7da5d63a3adb35633c5 to your computer and use it in GitHub Desktop.
# 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 = {}
num = 0
"a".upto("z").each do |letter|
alpha_num[letter] = num += 1
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