I want show numbers in dropdown like 0,10,20,30,40,60,70
For it I write
0..70
But it generate all numbers from 0 to 70 .I want only multiples of 10
Take a look at Numeric.step.
0.step(70,10).to_a
In addition to Numeric.step
0.step(70,10).to_a
you can also use Range#step:
Range.new(0,70).step(10).to_a
(0..70).step(10).to_a
You could create a new Enumerator for this:
multiples_of_10 = Enumerator.new do |y|
x = 0
loop do
y << x
x += 10
end
end
multiples_of_10.take(8) # => [0, 10, 20, 30, 40, 50, 60, 70]
Related
Having some Lua trouble with a a modification of Fisher-Yates shuffle in place. For example, let's say I have a 16 item table (sequence). I want to shuffle integers 1-4 then apply the shuffled pattern in the table to 1-4, 5-8, 9-12, 13-16. So:
{ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16 }
with a 4 item shuffling pattern of 4,2,3,1 would become:
{ 4, 2, 3, 1, 8, 6, 7, 5, 12, 10, 11, 9, 16, 14, 15, 13 }
The code here is from context and includes the "rising edge" input I am using to reshuffle. If you look at the test pic below you can see that yes, it shuffles each section in place, but it reshuffles each section -- I want the shuffled pattern to repeat.
t = {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15}
range = 4
local function ShuffleInPlace(t)
for i = #t, 2, -1 do
local j = math.random(1, range)
local k = (math.floor(i/(range+.001)))*range + j
t[i], t[j] = t[j], t[i]
end
end
-- initialize new table for shuffling
if s == nil then s = {} end
-- use gate rising edge to shuffle
if prev == nil then prev = 0 end
if gate > 0 and prev <= 0 then
s = t
ShuffleInPlace(s)
end
prev = gate
Test pic:
LMD, thank you, your helpful reply is uncovering a solution (by creating the shuffled "pattern" sequence first, outside the iterator). (Still some issues with the first value I'm working out. And I might be looking at some biproducts of the not-so-great math.random function, but that's another story). I'm a novice so any suggestions are appreciated!
-- range input is 0 to 1
seqRange = math.floor(range*(#t*.99))
local function ShuffleRange(x)
if rdm == nil then rdm = {} end
for m = 1, x do rdm[m] = m end
for m = #rdm, 2, -1 do
local j = math.random(m)
rdm[m], rdm[j] = rdm[j], rdm[m]
return rdm[m]
end
end
local function ShuffleInPlace(t)
y = ShuffleRange(seqRange)
for i = #t, 2, -1 do
local j = (math.floor(i/(seqRange*1.001)))*seqRange + y
t[i], t[j] = t[j], t[i]
end
end
Here's how I would do it, implementing the simple approach of first generating a series of swaps and then applying that to the sublists of length n:
math.randomseed(os.time()) -- seed the random
local t = {}; for i = 1, 16 do t[i] = i end -- build table
local n = 4 -- size of subtables
local swaps = {} -- list of swaps of offsets (0-based)
for i = 0, n - 1 do
-- Insert swap into list of swaps to carry out
local j = math.random(i, n - 1)
table.insert(swaps, {i, j})
end
-- Apply swaps to every subtable from i to i + n
for i = 1, #t, n do
for _, swap in ipairs(swaps) do
-- Swap: First add offsets swap[1] & swap[2] respectively
local a, b = i + swap[1], i + swap[2]
t[a], t[b] = t[b], t[a]
end
end
print(table.concat(t, ", "))
Example output: 4, 2, 1, 3, 8, 6, 5, 7, 12, 10, 9, 11, 16, 14, 13, 15
I'm currently working with the "Charts" pod.
My app shows a bar chart of athletes results, with:
X Axis: number of reps / time / rounds / weight
Y Axis: number of athletes
I would like to gather the number of reps in different groups.
Something that would be like: 10 < x < 20, 20 < x < 30, etc...
Rather than the real total of reps.
Something like that:
What would be the best way to do so? I though about some approaches:
Round the number of reps to transform 19 and 15 to 10 and 10 for example (for the 10 < x < 20 category)
The problem with that method is that I don't know if I can do the same for the "time (seconds)
Create a new array with dictionnaries inside, something like:
[["10-20": 15, 17, 19], ["20-30": 21, 22, 22, 24], etc..]
But I don't know how to achieve that...
What would be the best way?
You can use Dictionary's init(grouping:by:) initializer to create such a dictionary:
let array = [15,17,19,22,24,24,27]
let dict = Dictionary(grouping: array, by: { $0 / 10 })
// dict is [2: [22, 24, 24, 27], 1: [15, 17, 19]]
If I understood you correctly, you probably have a bunch of Athletes and they have a reps property. You can group by $0.reps / 10 instead:
Dictionary(grouping: athletes, by: { $0.reps / 10 })
And then map the keys and values to this:
.map { ("\($0.key * 10) - \(($0.key + 1) * 10)", $0.value.count) }
// now you have this:
// [("20 - 30", 4), ("10 - 20", 3)]
My professor taught me this (below), for creating a 10 numbers table, but
v = {}
for i = 1, 10 do
v[i] = i
end
print(v[3])
(the output will get me 3, as expected)
why do I recieve "nill" if I try doing this? (bellow)
v = {}
for i = 1, 10, 2 do
v[i] = i
end
print(v[42])
As you can see, I was trying to make a table like this
v = {1,3, 5, 7, 9}
Why it is not working? :(
<3
You're filling only the odd-numbered positions.
Try
for i = 1,5 do
v[i] = 2*i-1
end
I need to split the integer into an array.
For example,
The integer variable a equals 48.
a = 48
I need to split the 48 into an array based on the count of 10.
I need to get the array as below,
arr = [ 10, 20, 30, 40, 48]
EDIT:-
I tried following code and it work fine.
max = 48
arr = []
j = 0
for i in 1..max
if i % 10 == 0
arr[j] = i
j = j + 1
end
end
if max % 10 != 0
arr[j] = max
end
p arr
# => [10, 20, 30, 40, 48]
But if I have a bignum integer it will take more time. Is there any built-in method to this like a split for string.
I know the split method and also I know how to split the string into an array based one the character available in the string. but I don't know how to use the split method splitting the integer into an array based on the count.
Any one please explain me how to do this ?
Thanks in advance!
Starting with a = 48 then try:
(10..a).step(10).to_a.push(a) #=> [10, 20, 30, 40, 48]
Using Range#step to increment the range by steps of ten before converting to an array and finally appending your value of a. Alternatively you can also write:
10.step(a,10).to_a << a #=> [10, 20, 30, 40, 48]
You can try this
a = 48
array = []
(1..a/10).each{|x| array.push(x*10)}
a%10 != 0 ? array.push(a) : array
#=> [10, 20, 30, 40, 48]
I suggest this solution feel free to change some code mistakes since im not familiar with ruby :
$a = 48
$num = 10
array = Array.new
while $num < $a do
array << $num
$num +=10
end
array << $a
You can use map and lambda in this case.
a = 48
arr = ((1..a/10).map(&->(x){x*10}) << a).uniq
p arr
# => [10, 20, 30, 40, 48]
For all elements in Range (1..a/10) (in this case 1..4), map method call lambda function. This lambda have one argument and return x*10 (for first element 1*10 # => 10) and add all results to the array.
I hope this helps.
I'm a beginner in programming and have problems with this if statement:
if (f.year == (10 || 20 || 30 || 40 || 50 || 60 || 70 || 80 || 90 || 100 || 110 || 120)) && (f.rund != true)
The first problem is that this code is very complicated. Actually I only want to check if the f.year is a round two-digit number.
Next my code does not work correctly. Somehow it only selects the f.year that are equal 10.
How can I solve these problems?
It's because
(10 || 20 || 30 || 40 || 50 || 60 || 70 || 80 || 90 || 100 || 110 || 120)
expression always evaluates to 10.
You can solve the problem with, for example:
(1..12).map { |el| el * 10 }.include?(f.year)
or, as suggested by #AurpRakshit:
(1..12).map(&10.method(:*)).include?(f.year)
Here you have more examples of generating this kind of array.
Or, if you really want to check if f.year is round two-digit number, you can:
(10...100).include?(f.year) && f.year % 1 == 0
You can use Range#step or Numeric#step:
(10..120).step(10).to_a #=> [10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 110, 120]
10.step(120, 10).to_a #=> [10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 110, 120]
And call Enumerable#include?:
(10..120).step(10).include? year
10.step(120, 10).include? year
To answer your first point, the code should read:
if (f.year == 10 || f.year == 20 || f.year == 30 ...
Your expression f.year == (10 || 20 || 30 ... doesn't work, because it is evaluated by ruby as follows:
The brackets force 10 || 20 || 30 ... to be evaluated first
The || operator returns its left operand if it is true, otherwise it returns its right operand
Ruby considers anything that isn't nil or false to be "true", so the expression 10 || 20 || 30 ... evaluates to 10
So your expression boils down to (f.year == 10) && (f.rund != true)
You are already told why your code doesn't work as expected, I'm answering just to suggest to use a mathematical approach here instead of using include?, your condition could be written as:
if f.year.modulo(10).zero? && f.year.between?(10, 120) && !f.rund
...
It may be a little less clear but it is much faster.
Update
The drawback of this solution is that it fails when f.year is not a Numeric object:
nil.modulo(10)
# NoMethodError: ...
While:
[10].include?(nil)
# => false
The benchmarck:
require 'fruity'
a = (1..10000)
compare do
map_include do
a.each do |i|
(1..12).map(&10.method(:*)).include?(i)
end
end
step_include do
a.each do |i|
(10..120).step(10).include?(i)
end
end
divmod_include do
a.each do |i|
q, r = i.divmod(10); (1..12).include?(q) && r.zero?
end
end
math do
a.each do |i|
i.modulo(10).zero? && i.between?(10, 120)
end
end
end
Running each test once. Test will take about 2 seconds.
math is faster than divmod_include by 1.9x ± 0.01
divmod_include is faster than step_include by 9x ± 0.1
step_include is faster than map_include by 3.4x ± 0.1
I am not sure about your question, but the first condition can be written as
q, r = f.year.divmod(10); (1..12).include?(q) && r.zero?
or
[10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 110, 120].include?(f.year)
It's hard to tell what the OP wants but...
require 'fruity'
ARY = (1..1000).to_a
compare do
test_mod_and_le do
ARY.each do |i|
(i % 10 == 0) && (i <= 120)
end
end
test_mod_and_range do
ARY.each do |i|
(i % 10 == 0) && ((10..120) === i)
end
end
test_case_when do
ARY.each do |i|
case i
when 10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 110, 120
true
else
false
end
end
end
map_include do
ARY.each do |i|
(1..12).map(&10.method(:*)).include?(i)
end
end
step_include do
ARY.each do |i|
(10..120).step(10).include?(i)
end
end
divmod_include do
ARY.each do |i|
q, r = i.divmod(10); (1..12).include?(q) && r.zero?
end
end
math do
ARY.each do |i|
i.modulo(10).zero? && i.between?(10, 120)
end
end
end
Which outputs:
Running each test 32 times. Test will take about 4 seconds.
test_case_when is similar to test_mod_and_le
test_mod_and_le is faster than test_mod_and_range by 19.999999999999996% ± 10.0%
test_mod_and_range is faster than math by 50.0% ± 10.0%
math is faster than divmod_include by 80.0% ± 10.0%
divmod_include is faster than step_include by 5.9x ± 0.1
step_include is faster than map_include by 2.9x ± 0.1
Conditions do not work this way. All numbers within the brackets, connected with OR are evaluated before checking the equality with f.year.
Most answers here seem overcomplicated. You can use basic math to solve your issue:
if year % 10 == 0 && year.to_s.size == 2
# do stuff
end
The modulo operator % returns the remainder when dividing by 10 in this example. If the remainder is 0, it's a multiple of 10. You can use any number. Modulo 2 would check whether the number is even.
The second part checks the number of digits. It converts it to a string first with to_s and then checks the length of it, basically how many characters are in there. Converting 10 to string results in '10' which has 2 characters.
Your question seems a bit unclear. Do you want to include the numbers 100, 110 and 120 like in your code example? Or do you want only two digit numbers like stated in your text?