diff --git a/README.md b/README.md index 45aa284..7105108 100644 --- a/README.md +++ b/README.md @@ -4,3 +4,5 @@ assignment_ruby_warmup Dice, dice, baby. [A Ruby assignment from the Viking Codes School](http://www.vikingcodeschool.com) + +Sinister78 diff --git a/anagrams.rb b/anagrams.rb new file mode 100644 index 0000000..a65e9ac --- /dev/null +++ b/anagrams.rb @@ -0,0 +1,14 @@ +def anagrams(string) + string.downcase! + anagrams = [] + file_contents = File.read("enable.txt") + + permutations = string.chars.permutation.map &:join + permutations.each do |word| + file_contents.each_line do |line| + anagrams << word.upcase if (word.chomp != string) && (word.chomp == line.chomp) + end + end + + return anagrams.sort.uniq +end diff --git a/dice_outcomes.rb b/dice_outcomes.rb new file mode 100644 index 0000000..0829eb7 --- /dev/null +++ b/dice_outcomes.rb @@ -0,0 +1,23 @@ +require './roll_dice.rb' + +def dice_outcomes(number_of_dice = 1, rolls = 1) + results = {} + + rolls.times do + roll_total = roll_dice(number_of_dice) + results[roll_total] ||= 0 + results[roll_total] += 1 + end + + number_of_dice.upto(number_of_dice * 6) do |roll_total| + if !results[roll_total] + hash_string = "" + else + hash_string = '#' * results[roll_total] + end + puts "#{roll_total}:".ljust(4) + hash_string + end + + sorted_results = Hash[results.sort] + return sorted_results +end diff --git a/fibonacci.rb b/fibonacci.rb new file mode 100644 index 0000000..c2fe58b --- /dev/null +++ b/fibonacci.rb @@ -0,0 +1,19 @@ +def fibonacci(number_of_members) + sequence = [] + one_back = 0 + two_back = 0 + + 1.upto(number_of_members) do |member| + if member == 1 + sequence << 1 + one_back = 1 + else + new_sum = one_back + two_back + sequence << new_sum + two_back = one_back + one_back = new_sum + end + end + + return sequence +end diff --git a/roll_dice.rb b/roll_dice.rb new file mode 100644 index 0000000..a849856 --- /dev/null +++ b/roll_dice.rb @@ -0,0 +1,7 @@ +def roll_dice(rolls = 1) + roll_total = 0 + rolls.times do + roll_total += rand(1..6) + end + return roll_total +end diff --git a/stock_picker.rb b/stock_picker.rb new file mode 100644 index 0000000..de5ea15 --- /dev/null +++ b/stock_picker.rb @@ -0,0 +1,19 @@ +def stock_picker(stock_prices) + highest_profit = { + buy_day: 0, + sell_day: 0, + profit: 0 + } + + stock_prices.each_with_index do |price, index| + for subsequent_stock in ((index + 1)..(stock_prices.size - 1)) + if stock_prices[subsequent_stock] - price > highest_profit[:profit] + highest_profit[:buy_day] = index + highest_profit[:sell_day] = subsequent_stock + highest_profit[:profit] = stock_prices[subsequent_stock] - price + end + end + end + + return Array[highest_profit[:buy_day], highest_profit[:sell_day]] +end