Ruby exclude value in range - ruby

I want to build an array using range (0..x) while excluding certain ranges and values.
Example:
array = [1, 2, 3, .., 50, 60, .., 98, 100]
How is this done?

Your example is:
array = [1, 2, 3,..., 50, 60,..., 98, 100]
If this means array is to contain the numbers 1 through 50, 60 through 98 and 100, then you can write that:
array = [*1..50, *60..98, 100]
#=> [1, 2, 3,...49, 50, 60, 61, 62,...98, 100]

Subtracting one range from another:
(1..100).to_a - (51...60).to_a
To remove additional specific values, create an array of those values and subtract them too:
(1..100).to_a - (51...60).to_a - [82, 56, 23]
To add some cleanliness:
values_to_remove = [(51..60), [34,67,98]].flat_map(&:to_a)
(1..100).to_a - values_to_remove

I want to build an array using range (0..x) while excluding certain
ranges and values.
As per ruby-docs, you can excluded a range using Enumerable#grep_v:
(1..10).grep_v(2..4) #=> [1, 5, 6, 7, 8, 9, 10]
grep_v only defines one parameter so to exclude more than one range you would have to do something like:
(1..10).grep_v(2..4).grep_v(6..8) #=> [1, 5, 9, 10]
Values can be used as the argument if a range is not required e.g. grep_v(1).

Since Rails 6, you can build the example array in question using ranges + excluding:
(1..100).excluding(*(51..59), 100)

Related

Ruby loop through each element in an array n times

I've had a tough time finding an exact example of this.
If I have an array that contains 5 elements. E.g.
list = [5, 8, 10, 11, 15]
I would like to fetch what would be the 8th (for example) element of that array if it were to be looped. I don't want to just duplicate the array and fetch the 8th element because the nth element may change
Essentially the 8th element should be the number 10.
Any clean way to do this?
Math to the rescue
list[(8 % list.length) - 1]
A link about this modulo operator that we love
This should do:
def fetch_cycled_at_position(ary, num)
ary[(num % ary.length) - 1]
end
ary = _
=> [5, 8, 10, 11, 15]
fetch_cycled_at_position(ary, 1) # Fetch first element
=> 5
fetch_cycled_at_position(ary, 5) # Fetch 5th element
=> 15
fetch_cycled_at_position(ary, 8) # Fetch 8th element
=> 10
You could use rotate:
[5, 8, 10, 11, 15].rotate(7).first
#=> 10
It's 7 because arrays are zero based.
Just out of curiosity using Array#cycle:
[5, 8, 10, 11, 15].cycle.take(8).last
This is quite inefficient but fancy.
I ran these in my irb to get the output,
irb(main):006:0> list = [5, 8, 10, 11, 15]
=> [5, 8, 10, 11, 15]
irb(main):007:0> list[(8 % list.length) - 1]
=> 10
hope it will help you.

Codewars exercise "Count by X" not working

I'm tryng to do this exercise:
Create a function with two arguments that will return a list of length
(n) with multiples of (x).
Assume both the given number and the number of times to count will be
positive numbers greater than 0.
Return the results as an array (or list in Python, Haskell or Elixir).
Examples:
count_by(1,10) #should return [1,2,3,4,5,6,7,8,9,10]
count_by(2,5) #should return [2,4,6,8,10]
Quite easy, nothing to say. BUT I really do not understand why my code is not working.
PLease DO NOT GIVE ME NEW CODE OR SOLUTION, I JUST WANT TO UNDERSTAND WHY MINE DOESN'T WORK.
My solution:
def count_by(x, n)
arrays = []
arrays.push(x)
valore_x = x
unless arrays.count == n
arrays.push( x + valore_x)
x += valore_x
end
return arrays
end
count_by(3, 5)
ERROR MESSAGE =
Expected: [1, 2, 3, 4, 5], instead got: [1, 2]
✘ Expected: [2, 4, 6, 8, 10], instead got: [2, 4]
✘ Expected: [3, 6, 9, 12, 15], instead got: [3, 6]
✘ Expected: [50, 100, 150, 200, 250], instead got: [50, 100]
✘ Expected: [100, 200, 300, 400, 500], instead got: [100, 200].
So looks like that my code do not put all the numbers. Thanks.
Now you have answer on your question, so I just recommend one more variant of solution, I think it's more ruby-way :)
def count_by(x, y)
y.times.with_object([]) do |i, result|
result << x*(i+1)
end
end
Change
unless arrays.count == n
to
until arrays.count == n
unless is not a loop. It's just like an if, but the code is executed if the condition is false.
until is just like while, but the code is executed while the condition is false.
Array::new can be used here.
def count_by(x, n)
Array.new(n) { |i| x*(i+1) }
end
count_by(1,5) #=> [1, 2, 3, 4, 5]
count_by(2,5) #=> [2, 4, 6, 8, 10]
count_by(3,5) #=> [3, 6, 9, 12, 15]
count_by(50,5) #=> [50, 100, 150, 200, 250]
count_by(100,5) #=> [100, 200, 300, 400, 500]
count_by(0,5) #=> [0, 0, 0, 0, 0]
Just a few other ways to achieve the same result. Using Numeric#step:
x.step(by: x, to: x*y).entries
shorter but less readable:
x.step(x*y, x).entries
or using a range with Range#step:
(x..x*y).step(x).entries
In each of the examples entries can be replaced with to_a

How do I create a subset of an array based on an array of indexes for that array in Ruby

If I have an array like this:
[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20]
and I want to select a subset of that array based on this arbitrary array of indexes:
[0,1,4,7,8,13,14,15,18,19]
with the result being this subset of the first array:
[1,2,5,8,9,14,15,16,19,20]
My question is, how do I make a simple function (1 or 2 lines) out of the array of indexes and the starting array to get the subset?
arr = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20]
indexes = [0,1,4,7,8,13,14,15,18,19]
arr.values_at(*indexes) # => [1, 2, 5, 8, 9, 14, 15, 16, 19, 20]
arr = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20]
index = [0,1,4,7,8,13,14,15,18,19]
arr.select.with_index{|m,i| m if index.include? i}
#=> [1, 2, 5, 8, 9, 14, 15, 16, 19, 20]
arr = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20]
index = [0,1,4,7,8,13,14,15,18,19]
arr.each_with_index {|value,index| p value if indexes.include?(index)}

Finding continuous number sequence

How to find the longest continuous number sequence in array of number arrays? Each array of numbers represent one or zero numbers in resulting sequence.
Example ([] - represents array (like in javascript)):
[
[1, 5, 6],
[7],
[22, 34],
[500, 550],
[60, 1],
[90, 100],
[243],
[250, 110],
[150],
[155],
[160]
]
Correct output would be: [1, 7, 22, 60, 90, 110, 150, 155, 160]
Detailed output:
1, -- index 1 all 1, 5 and 6 would match here, pick the smallest
7, -- index 2
22, -- index 3
-- index 4 skipped, the sequence would end here or wouldn't be the longest possible
60, -- index 5 picked 60, because 1 wouldn't continue in the sequence
90, -- index 6
-- index 7 skipped, the sequence would end here or wouldn't be the longest possible
110, -- index 8
150, -- index 9
155, -- index 10
160 -- index 11
A possible approach is to use dynamic programming using as parameters the last value and the index of first sub-array to consider.
This is a solution in Python based on recursion with memoization
data = [[1, 5, 6],
[7],
[22, 34],
[500, 550],
[60, 1],
[90, 100],
[243],
[250, 110],
[150],
[155],
[160]]
def longest(x0, i, _cache=dict()):
if i == len(data):
return []
try:
return _cache[x0, i]
except KeyError:
best = longest(x0, i+1)
for x in data[i]:
if x >= x0:
L = [x] + longest(x, i+1)
if len(L) > len(best):
best = L
_cache[x0, i] = best
return best
print longest(0, 0)

How do I replace an array's element?

How can I substitue an element in an array?
a = [1,2,3,4,5]
I need to replace 5 with [11,22,33,44].flatten!
so that a now becomes
a = [1,2,3,4,11,22,33,44]
Not sure if you're looking to substitute a particular value or not, but this works:
a = [1, 2, 3, 4, 5]
b = [11, 22, 33, 44]
a.map! { |x| x == 5 ? b : x }.flatten!
This iterates over the values of a, and when it finds a value of 5, it replaces that value with array b, then flattens the arrays into one array.
Perhaps you mean:
a[4] = [11,22,33,44] # or a[-1] = ...
a.flatten!
A functional solution might be nicer, how about just:
a[0..-2] + [11, 22, 33, 44]
which yields...
=> [1, 2, 3, 4, 11, 22, 33, 44]
The version of bta using a.index(5) is the fastest one:
a[a.index(5)] = b if a.index(5) # 5.133327 sec/10^6
At least 10% faster than Ryan McGeary's one:
a.map!{ |x| x == 5 ? b : x } # 5.647182 sec/10^6
However, note that a.index(5) only return the first index where 5 is found.
So, given an array where 5 appears more than once, results will be different:
a = [1, 2, 3, 4, 5, 5]
b = [11,22,33,44]
a[a.index(5)] = b if a.index(5)
a.flatten # => [1, 2, 3, 4, 11, 22, 33, 44, 5]
a.map!{ |x| x == 5 ? b : x }
a.flatten # => [1, 2, 3, 4, 11, 22, 33, 44, 11, 22, 33, 44]
Array#delete will return the item or nil. You may use this to know whether or not to push your new values
a.push 11,22,33,44 if a.delete 5
You really don't have to flatten if you just concatenate. So trim the last element off the first array and concatenate them:
a = [ 1, 2, 3, 4, 5 ] #=> [1, 2, 3, 4, 5]
t = [11, 22, 33, 44] #=> [11, 22, 33, 44]
result = a[0..-2] + t #=> [1, 2, 3, 4, 11, 22, 33, 44]
a[0..-2] is a slice operation that takes all but the last element of the array.
Hope it helps!
This variant will find the 5 no matter where in the array it is.
a = [1, 2, 3, 4, 5]
a[a.index(5)]=[11, 22, 33, 44] if a.index(5)
a.flatten!
Here is another simple way to replace the value 5 in the array:
a[-1, 1] = [11, 22, 33, 44]
This uses the Array#[]= method. I'm not exactly sure why it works though.
gweg, not sure what you're trying to do here, but are you looking for something like this?
a = [1, 2, 3, 4, 5]
a.delete_at(4)
a = a.concat([11,22,33,44])
There are a number of ways of doing this -- I don't think the code above is especially nice looking. It all depends on the significance of '5' in your original array.

Resources