Matching response to multiple ranges of figures [closed] - ruby

Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 10 years ago.
Improve this question
I have a method that will return a number between 1 - 100
depending on whether the response (x) is between
100 - 90 i would like one response, 89 - 85 another 84 - 72 another ... etc
I use
if x > 90
response a
elsif x > 85
response b
elsif etc...
but this seems a little messy, is there a better way of refactoring this?
Many thanks.

Take advantage of Range#=== and use a case statement:
case x
when 72..84
# Do something
when 85..89
# Do something
when 90..100
# Do something
else
# Do something when no matches
end

You could try a table (a Hash) where the keys are ranges and the values ar the numbers you want to return :
T = {
(90..100) => 1,
(85..89) => 2,
# and so on
}
(r,v) = T.find {|r,v| r.member? x}
if v then
return v
else
# x wasn't in any of the defined ranges
end

Related

Ruby grammar problem . I don't know how to solve it [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
Please output the number obtained by adding a and b.
At the end of the line break, do not include extra characters, blank lines.
expmple1
1 1
result1
2
expmple2
0 99
result2
99
My code is:
input_lines = gets.chop
a = input_lines[0]
b = input_lines[1]
puts a + b
But it's not working, please help.
Add split to the chopped gets would work:
input_lines = gets.chop.split
a = input_lines[0].to_i
b = input_lines[1].to_i
puts a + b
Try it.
Check String doc.
More DRY way to do it:
input_lines = gets.chop.split.map(&:to_i)
a,b = input_lines
puts a + b
In this case the numbers inside input_lines already changed to Integers.

Ruby binary search [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I'm trying to do a binary search on a list of 1 million employee IDs, already sorted out.
def exist?(id)
lower = -1
upper = $employee_list.count
while true
mid = (lower + upper) / 2
if upper == lower + 1
return nil
if id == $employee_list[mid]
return mid
if id < $employee_list[mid]
upper = mid
else
lower = mid
end
end
end
end
end
I get the error:
NoMethodError:undefined method 'call' for nil:NilClass
Also, I'm trying to make it such that if exist?(54) is in the list, it will return true.
Just use Array#bsearch:
$employee_list.bsearch { |employee| id <=> employee }
As for the error - it comes from somewhere else.

If input is "X", multiply by "Y" [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I'm trying to do a simple math operation, but I'm stuck.
Ask user to introduce an amount of money:
puts "Capital to deposit"
capital = gets.chomp
Ask user to introduce a fixed number of days:
puts "Indicate days of deposit"
deposit_time = gets.chomp
I made the following array with the only possible answers for step #2:
deposit_time = [30, 45, 60]
I need a multiplication operation that depends on what the user chooses in step #2. For instance, let's suppose the user chooses 30. I would need the code to read:
capital = 10
deposit_time = 30
to multiply 10 * 1.0219 and print the resulting number: 10.219. Any ideas?
Use a hash, not an array.
deposit_rates = { 30 => 1.0219, 45 => 1.0336, 60 => 1.0467 }
deposit_time = gets.to_i
deposit_rate = deposit_rates[deposit_time]
# will be `nil` if not one of the defined ones

Random numbers in Ruby [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
I am trying to make n random numbers from -0.5 from to 0.5
and I made a function like this
def create_noise(n)
end
I found an implementation of this but i don't think this works
randoms = Set.new
loop
randoms << rand(max)
return randoms.to_a if randoms.size >= n
You would just do
def create_noise(n)
n.times.collect { rand(-0.5..0.5) }
end
that will spit back an array like this:
[-0.034680737617880486, 0.34802029078157803, 0.1346483808607455, 0.12155616615186282, -0.41043213731234474]

Sorting arbitrary numeric data and leave whitespaces for the missing values [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I need to sort the numeric data in a row/column while leaving space of missing values at scale of 1-5.
Like:
A B
13245 1_2_3_4_5
152.... 1_2_ _ _5
Try this UDF:
Function ModSort(Str As String) As String
Res = ""
For Iter = 1 To 5
If InStr(1, Str, CStr(Iter)) Then
Hold = CStr(Iter) & "_"
Else
Hold = " _"
End If
Res = Res & Hold
Next Iter
Res = Left(Res, 9)
ModSort = Res
End Function
Screenshot:
Let us know if this helps.
Not meant as a serious answer (UDF seems good enough) but appears possible with a formula:
=IF(LEN(SUBSTITUTE(A2,1,""))<LEN(A2),"1_"," _")
&IF(LEN(SUBSTITUTE(A2,2,""))<LEN(A2),"2_"," _")
&IF(LEN(SUBSTITUTE(A2,3,""))<LEN(A2),"3_"," _")
&IF(LEN(SUBSTITUTE(A2,4,""))<LEN(A2),"4_"," _")
&IF(LEN(SUBSTITUTE(A2,5,""))<LEN(A2),"5",)

Resources