This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
How to sum array members in Ruby?
Lets say I have this array
#test = [1, 2, 3, 4]
Then I want to do:
#test[0] + #test[1] + #test[2] + #test[3]
Is there not a smarter, faster way of doing this?
You can do this:
#test.inject(:+)
sum = 0
#test.each { |el| sum+=el }
Related
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
Folks,
I'm trying to generate a random number between (0..10) less, say, 5.
new_index = rand(0..(old_index - 1)) || new_index = rand((old_index + 1)..10)
Can anyone shed any light?
new_sample_space = (0..10).to_a - [5] #=> [0, 1, 2, 3, 4, 6, 7, 8, 9, 10]
new_index = new_sample_space.sample #=> random integer from 0-10, except 5
Of course, doing this with a large range is probably not a good idea because of memory concerns. In such "huge" cases, you could possibly just get another random number after you get 5.
loop do
new_index = rand(1..10)
break if new_index != 5
end
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 7 years ago.
Improve this question
For example,
[0.99].to_a
# => [0, 1, 2, 3...]
I'm wondering how many different one-line solutions there are. This is how many I got (6):
(0..99).to_a
[*0..99]
0.upto(99).to_a
[*0.upto( 99 )]
(0..99).map {|a| a}
(0..100).step(1).to_a
1. (0..99).to_a
2. (0..99).map{|e| e}
3. 1.upto(99).inject([]) { |sum, e| sum << e }
4. 1.upto(99).map { |e| e }
5. 99.times.inject([]) { |sum, e| sum << e.next}
You can do these:
1. (0..99).map { |a| a }
2. (0..99).step(1).to_a
3. (0..99).to_a
4. (1..99).each { |a| a }.to_a
The obvious one...
Array.new(100) {|i| i}
# the same as:
Array.new(100, &:to_i)
And if we want to be stupid about it:
Module.const_get([].class.name).new(100, &:to_i)
eval("[1,2,3,*(4..99)]")
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Improve this question
I am trying to write the Array.sort method in my own way using recursive logic. My code is as follows:
def sorting_array(unsorted,sorted)
temp=unsorted
if unsorted.length==0
return sorted
elsif unsorted.length==1
return sorted.push(unsorted[0])
else
end
if unsorted[0]<=unsorted[1] # check the first position and add
sorted.push(unsorted.shift)
sorting_array(unsorted,sorted)
else # add the 0th element to the end to handle later
unsorted.push(unsorted.shift)
sorting_array(unsorted,sorted)
end
end
array=["pat","aog","cig","Zig","forse","erdvark", "aag"]
p sorting_array(array,[])
I appreciate any insight or input on where I am messing this up.
if unsorted[0]<=unsorted[1] #idea is to check the first position and add
sorted.push(unsorted.shift)
sorting_array(unsorted,sorted)
The problem comes here: you push unsorted[0] into sorted when unsorted[0] <= unsorted[1], no matter if unsorted[0] is the smallest in unsorted.
Try this: unsorted = [100, 101, 1, 2].
Your underlying algorithm is broken and will not produce a sorted array in all cases. Here's a simple counter example. These are arrays of strings of numbers. I'm just lazy and omitted the single/double quotes in the array definitions:
[2, 5, 0 ,4]
"2" <= "5" => true
[2] , [5, 0, 4]
"5" <= "0" => false
[2] , [0, 4, 5]
"0" <= "4" => true
[2, 0] , [4, 5]
"4" <= "5" => true
[2, 0, 4] , [5] <=== At this point your "sorted" array is clearly not sorted
Part of the problem is your code here:
if unsorted[0]<=unsorted[1]
sorted.push(unsorted.shift)
sorting_array(unsorted,sorted)
The if statement doesn't provide the kind of guarantee you need to push something into your sorted array.
This question already has answers here:
Check to see if an array is already sorted?
(8 answers)
Closed 9 years ago.
I am just wondering if there is a way to check if the array is increasing ?
Here is my solution, but I am searching for more beautiful way:
n = - 1
#arr.flatten.each { |e|
return false if e < n
n = e
}
You can do the following:
> arr = [1, 4, 5, 6]
> arr.each_cons(2).all? { |a, b| (a <=> b) <= 0 }
=> true
You can add it to Array class
class Array
def is_sorted?
each_cons(2).all? { |a, b| (a <=> b) <= 0 }
end
end
Try this,
if #arr.sort.uniq == #arr
# array is increasing
else
# array not increasing
end
This will sort the array and drop duplicate values, then compare it to the original array.
If your original array is always increasing, it should match the sorted, de-duplicated array.
EDIT:
While this solution gives the desired result, this is not the best solution (see comment below). I would suggest going with toch's solution instead.
This question already has answers here:
Ranges A to B where A > B in F#
(5 answers)
Closed 9 years ago.
Is there a way one can get a range in descending order?
Ex
[1..4]
evaluates to
> val it : int list = [1; 2; 3; 4]
But
[4..1]
evaluates to
> val it : int list = []
Is there a different syntax to achive this without having to do a List.Reverse ?
You have to do:
[4..-1..1]
The -1 is the step