Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,5 @@ assignment_ruby_warmup
Dice, dice, baby.

[A Ruby assignment from the Viking Codes School](http://www.vikingcodeschool.com)

Anne Richardson
47 changes: 47 additions & 0 deletions anagrams.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
def local_anagrams(input)
processed_input = input.downcase.chars.sort.join
matches = []

IO.foreach('enable.txt') do |word|
word.strip!
processed_word = word.downcase.chars.sort.join
matches << word if (processed_word == processed_input) && (word != input)
end

if matches.length > 0
puts "Anagrams for '#{input}' are:"
puts matches
else
puts "Sorry, there are no anagrams for '#{input}'."
end
end

local_anagrams('spear')

#=======================================

require 'open-uri'

def cloud_anagrams(input)
puts "Fetching your anagram data from the web..."

web_dictionary = open('http://s3.amazonaws.com/viking_education/web_development/web_app_eng/enable.txt') {|f| f = f.readlines}

processed_input = input.downcase.chars.sort.join
matches = []

web_dictionary.each do |word|
word.strip!
processed_word = word.downcase.chars.sort.join
matches << word if (processed_word == processed_input) && (word != input)
end

if matches.length > 0
puts "Anagrams for '#{input}' are:"
puts matches
else
puts "Sorry, there are no anagrams for '#{input}'."
end
end

cloud_anagrams('pears')
30 changes: 30 additions & 0 deletions dice_outcomes.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# Now write a method dice_outcomes which takes the number of dice to roll and the number of times to roll them, then outputs a visual chart of how many times each possible number comes up.

def roll_dice(qty_dice = 1)
roll_results = []

qty_dice.times do |result|
roll_results << rand(1..6)
end

roll_results.reduce(:+)
end

def dice_outcomes(qty_dice = 1, rolls = 1)
roll_outcomes = {}

rolls.times do
roll_total = roll_dice(qty_dice)
if roll_outcomes[roll_total]
roll_outcomes[roll_total] += '#'
else
roll_outcomes[roll_total] = '#'
end
end

roll_outcomes.sort.to_h.each do |key, value|
puts "#{key}: #{value} (#{value.length})"
end
end

dice_outcomes(3, 100)
Loading