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.
Related
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
I want to write a script on Bash that does:
I have a file, that consists of string like this:
2002-02-15 00:01:19 217.21.43.21 RES company_name
2002-02-15 00:01:19 217.21.43.21 RES company_name
2002-02-15 00:01:19 217.21.43.21 DEL company_name
2002-02-13 00:01:19 217.21.43.21 RES company_name
I need to calculate the number of requests with parameter RES for each day.
Output of script should be:
2002-02-15 2
2002-02-13 1
This should be enough:
awk '/RES/ { N[$1] += 1; }; END { for (day in N) { print day, N[day] } }' your_input
It creates an associative array N whose indices are the days of the first field, and whose values are incremented by one for every line matching RES.
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.
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 5 years ago.
Improve this question
I want to make this simple program that returns the weight quantity by choosing the units. When I run the program it runs with no problems and asks the two questions in if but doesn't return the value
Here is my code:
puts "What is your starting weight and ratio unit?
1:kg.m/h
2:gm.m/s
3:mm.m/s"
inputing_unit = gets.chomp
puts "What is your ending weight and ratio unit?
1:kg.m/h
2:gm.m/s
3:mm.m/s"
ending_unit = gets.chomp
if inputing_unit == 1 and ending_unit == 1 then
puts "What is your weight?"
input_weight = gets.chomp.to_i
puts "What is your ratio?"
input_ratio = gets.chomp.to_i
puts "Your moving value is #{input weight * input_ratio}"
end
As I can see from that screenshot in the beginning of the description on pre-last line where is your puts statement there is a typo #{input weight * input_ratio}. You should change ...#{input weight... to ...#{input_weight... (with a dash).
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]
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",)