/
rumplestilt.rb
47 lines (31 loc) · 1.06 KB
/
rumplestilt.rb
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
puts "Please enter numbers separated by commas:"
numbers = gets.chomp
numbers = numbers.split(',').map { |n| n.to_i }
puts "Please enter your target number under 10,000:"
target = gets.chomp.to_i
def countup(num_array, target)
array_of_operations = ["+","-","/","*"]
left_value = num_array.sample
total_iterations = 0
while true
output = ""
total_iterations += 1
current_operation = array_of_operations.sample
right_value = num_array.sample
output = "#{left_value} #{current_operation} #{right_value} ="
# get number close to zero
if current_operation == "/" && left_value % right_value != 0
output = "#{left_value} % #{right_value} ="
left_value = left_value % right_value
# avoid dividing by 0
elsif right_value != 0
left_value = left_value.send(current_operation, right_value)
# and just add if divide by 0 was gonna happen
else
left_value += right_value
end
puts "#{output} #{left_value}"
return total_iterations if left_value == target
end
end
puts countup(numbers, target)