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
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
How can I check if a number is a Dudeney Number, using Ruby? Taking as examples or evidence, these results:
dudeney_number?(1) #=> true
dudeney_number?(125) #=> false
dudeney_number?(512) #=> true
dudeney_number?(1728) #=> false
dudeney_number?(5832) #=> true
Well, that's easy; there are only 6!
def dudeney_number?(x)
return [1, 512, 4913, 5832, 17576, 19683].include?(x)
end
See also: http://blog.hostilefork.com/six-dudeney-numbers-proof/
From https://en.wikipedia.org/wiki/Dudeney_number:
A Dudeney number is a positive integer that is a perfect cube such that the sum of its decimal digits is equal to the cube root of the number.
Here's an implementation by that definition:
def dudeney_number?(n)
digit_sum = n.to_s.chars.map(&:to_i).inject(:+)
n == digit_sum ** 3
end
1.upto(20000).select { |n| dudeney_number? n }
#=> [1, 512, 4913, 5832, 17576, 19683]
The method doesn't check if n is a positive integer.
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 is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 9 years ago.
Improve this question
a = 1; b = 2
fibonacci = []
while fibonacci.length < 100
fibonacci.push(a)
fibonacci.push(b)
a = a + b; b = a + b
end
push fibonacci
The error message is "undefined method `push' for main:Obj"
You're trying to #push the array itself on the last line! :)
That's what it's complaining about. The push method is being invoked on the 'main' object, and push is not a Kernel method.
I'm guessing you mean puts. Otherwise it looks okay, if somewhat non-idiomatic. Naturally you can find lots of Ruby solutions for this problem on the site that might read a bit more clearly (see here for a recursive one.)
As others have said before the last line should be 'puts'
Also your numbers are wrong.
a = 1; b = 1
fibonacci = []
while fibonacci.length < 100
fibonacci << a
fibonacci << b
a += b
b += a
end
puts fibonacci
But also the fib starts at 1 and the the second element is also 1.
This make you sequence off, if you start at 1, 2
Fib = 1, 1, 2, 3, 5, 8, ...
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.
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 9 years ago.
Improve this question
Accept an integer n
Based on n, accept n inputs
Ex:
At runtime
n = 2
Then 2 inputs of type string should be accepted
Ex:
At runtime
n = 3
Then 3 inputs of type string should be accepted
array = []
n.times {array << gets.chomp}
n = gets.chomp.to_i
array = []
array[n] = gets.chomp while (n -= 1) >= 0
# array will contain the inputs...
# array.pop will return them in order of input