diff --git a/README.md b/README.md index 45aa284..61d19e3 100644 --- a/README.md +++ b/README.md @@ -4,3 +4,6 @@ assignment_ruby_warmup Dice, dice, baby. [A Ruby assignment from the Viking Codes School](http://www.vikingcodeschool.com) + +
+Mariah Schneeberger diff --git a/anagrams.rb b/anagrams.rb new file mode 100644 index 0000000..b190ece --- /dev/null +++ b/anagrams.rb @@ -0,0 +1,13 @@ +def anagrams(word,anagrams_array) + answer_array = [] + new_word = word.upcase.split("") + print new_word + anagrams_array.each do |w| + if w.upcase.split("") & new_word == w.split("") + answer_array << w + end + end + print answer_array +end + +anagrams("pears",["APERS","APRES","ASWIR","ASPER","PARES","PARSE","PRASE","PRESA","RAPES","REAPS","SPARE","SPEAR"]) diff --git a/dice_outcomes.rb b/dice_outcomes.rb new file mode 100644 index 0000000..1d5a5e7 --- /dev/null +++ b/dice_outcomes.rb @@ -0,0 +1,22 @@ +def dice_outcomes(number_of_dice, roll_times) + dice = [1,2,3,4,5,6] + results_of_roll = [] + products = {} + roll_times.times do + results_of_roll << dice.sample(number_of_dice) + end + while results_of_roll.length > 0 + sum = 0 + results_of_roll[0].each {|num| sum += num} + if products.has_key?(sum) != true + products[sum] = 1 + else + products[sum] += 1 + end + results_of_roll.delete_at(0) + end + products.each {|key,value| print "#{key}: #{value}\n"} +end + + +dice_outcomes(3, 100) diff --git a/fibonacci.rb b/fibonacci.rb new file mode 100644 index 0000000..973414f --- /dev/null +++ b/fibonacci.rb @@ -0,0 +1,21 @@ + +#fibonacci +#takes n +#returns n numbers in the fib sequence +#fib sequence each product is the product of the prior two products +def fibonacci(n) + fib_array = [] + fib_index = 1 + a, b = 1, 1 + while fib_index <= n + c = a + a = b + b = c + b + fib_array << c + fib_index += 1 + end + return fib_array +end + +print fibonacci(10) +print fibonacci(14) diff --git a/roll_dice.rb b/roll_dice.rb new file mode 100644 index 0000000..2dad5f7 --- /dev/null +++ b/roll_dice.rb @@ -0,0 +1,13 @@ +def roll_dice(n=1) + dice = [1,2,3,4,5,6] + results_of_roll = [] + n.times do results_of_roll << dice.sample + end + sum = 0 + results_of_roll.each {|num| sum += num} + print results_of_roll + print sum +end + +roll_dice(4) +roll_dice(12) diff --git a/stock_picker.rb b/stock_picker.rb new file mode 100644 index 0000000..ab8e6cd --- /dev/null +++ b/stock_picker.rb @@ -0,0 +1,29 @@ +#stock_picker method +#takes an array of stock prices +#finds most profitable two days to buy stocks +#then finds two most profitable days to sell those stocks +def stock_picker(prices) + answer_array = [] + day_to_buy = 0 + day_to_sell = 0 + profit = prices[1] - prices[0] + prices.each_with_index do |buy_price, buy_day| + prices.each_with_index do |sell_price, sell_day| + if sell_day > buy_day + if sell_price - buy_price > profit + profit = sell_price - buy_price + day_to_buy = buy_day + day_to_sell = sell_day + end + end + end + end + answer_array << day_to_buy + answer_array << day_to_sell + print answer_array +end + + +stock_picker([44, 30, 24, 32, 35, 30, 40, 38, 15]) +stock_picker([65, 55, 65, 43, 32, 44, 56, 68, 23, 5]) +stock_picker([33,54,57,23,12,40,68,32,46,60,35,79,34,5])