forked from vikingeducation/assignment_ruby_warmup
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmethods.rb
More file actions
163 lines (101 loc) · 3.7 KB
/
methods.rb
File metadata and controls
163 lines (101 loc) · 3.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
# ____________________________________________________
# ******************Rolling Dice***********************
# ----------------------------------------------------
# Define method with a parameter for number of rolls. Set default value to 1.
def roll_dice(number_of_rolls = 1)
# Create a variable to hold the total to be returned.
sum_of_rolls = 0
number_of_rolls.times do
#Generate a random number between 1 & 6.
outcome = rand(6) + 1
#Add the outcome to the total sum variable.
sum_of_rolls += outcome
end
# Return the final sum of all the rolls.
return sum_of_rolls
end
# puts roll_dice(2)
# ____________________________________________________
# ******************Dice Outcomes**********************
# ----------------------------------------------------
def dice_outcomes(number_of_dice, times_to_roll)
max_sum = 6*number_of_dice
# Create a hash to track the number of times each value is rolled.
outcome_tracker = {}
# Roll all the dice times_to_roll times
times_to_roll.times do
# Find a random number between number of dice (min) and max_sum
outcome = (number_of_dice - 1) + (rand((max_sum+1) - number_of_dice) + 1)
# determine if the outcome is already a key in the outcome_tracker
if outcome_tracker.include?(outcome)
#if it is, add 1 to its value
outcome_tracker[outcome] += 1
#if its not, create a key = to outcome and set its value to 1
else
outcome_tracker[outcome] = 1
end
end
# Print the chart from leaset to greatest outcome
number_of_dice.upto(max_sum) do |possible_outcome|
# check to see if the outcome has been rolled
if outcome_tracker.include?(possible_outcome)
puts "#{possible_outcome}: " + ("#" * outcome_tracker[possible_outcome])
end
end
end
# puts dice_outcomes(4, 5)
# ____________________________________________________
# ******************Fibonacci**********************
# ----------------------------------------------------
def fibonacci (numbers_in_sequence)
sequence_array =[1, 1]
numbers_in_sequence.times do
next_number = sequence_array[-1] + sequence_array[-2]
sequence_array << next_number
end
return sequence_array
end
# print fibonacci(9)
# ____________________________________________________
# ******************Stock Picker**********************
# ----------------------------------------------------
def stock_picker(stock_prices)
max_profit = 0
best_days = []
# Run through the possible buy days
(stock_prices.length - 1).times do |buy_date|
# Subtract the buy date price from each possible sell date
(buy_date..(stock_prices.length - 1)).each do |sell_date|
profit = stock_prices[sell_date] - stock_prices[buy_date]
# Compare the difference to the max profit value to see if it is greater
if profit > max_profit
max_profit = profit
best_days[0] = buy_date
best_days[1] = sell_date
end
end
end
print best_days
end
# stock_picker([44, 30, 24, 32, 35, 30, 40, 38, 15])
# ____________________________________________________
# ******************Anagrams**********************
# ----------------------------------------------------
def anagrams(original_word)
# Make sure the given input is formatted correctly
formatted_word= original_word.downcase.strip
# Sort the letters so it can be compared accurately
test_word = formatted_word.split("").sort.join
# Create an array to hold anagrams as they appear
anagram_array=[]
File.open("enable.txt").readlines.each do |imported_word|
imported_word = imported_word.strip
if imported_word != formatted_word
if imported_word.split("").sort.join == test_word
anagram_array << imported_word
end
end
end
print anagram_array
end
anagrams("Pears ")