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

Submitting Assignment #149

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

Sinister78
14 changes: 14 additions & 0 deletions anagrams.rb
Original file line number Diff line number Diff line change
@@ -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
23 changes: 23 additions & 0 deletions dice_outcomes.rb
Original file line number Diff line number Diff line change
@@ -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
19 changes: 19 additions & 0 deletions fibonacci.rb
Original file line number Diff line number Diff line change
@@ -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
7 changes: 7 additions & 0 deletions roll_dice.rb
Original file line number Diff line number Diff line change
@@ -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
19 changes: 19 additions & 0 deletions stock_picker.rb
Original file line number Diff line number Diff line change
@@ -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