Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Completed all code challenges #147

Open
wants to merge 11 commits into
base: master
Choose a base branch
from
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