Redis Lua script not working as expected - arrays

As a practice exercise, I'm writing a lua script for Redis which basically executes the JavaScript method Array#copyWithin().
Quoting from MDN,
The copyWithin() method copies the sequence of array elements within
the array to the position starting at target. The copy is taken from
the index positions of the second and third arguments start and end.
Here is the script I've written so far :
local list = redis.call('lrange', KEYS[1], 0, -1)
local target = tonumber(ARGV[1])
local startIndex = tonumber(ARGV[2])
local len = #list
local endIndex = len
--Handle negative startIndex
if startIndex < 0 then
startIndex = len+startIndex
end
--If the third argument is provided, get the endIndex from it
if #ARGV > 2 then
local arg = tonumber(ARGV[3])
if arg >= 0 then
if arg < endIndex then
endIndex = arg
end
else
if len+arg >= 0 then
endIndex = len+arg
else
endIndex = 0
end
end
end
--An array containing the elements which will be copied
local targeted_elements = {}
--Fill elements from the list
for i=1, (endIndex-startIndex+1) do
targeted_elements[i] = list[startIndex+i]
end
--Make sure no additional elements are pushed to the end of array in case of range overflow
local target_end = #targeted_elements
if target + target_end > len then
target_end = len-target
end
--replace all the changed elements of the list in redis
for i=1, target_end do
redis.call('lset', KEYS[1], target+(i-1), targeted_elements[i])
end
During testing, the first test case is successfully cleared :
Test case : convert [1, 2, 3, 4, 5] into [4, 5, 3, 4, 5] using copyWithin(0, 3)
LRANGE MyList 0 -1
> [1, 2, 3, 4, 5]
EVALSHA "sha1 of script" 1 MyList 0 3
(basically the same as `[1, 2, 3, 4, 5].copyWithin(0, 3)`)
> nil
LRANGE MyList 0 -1
> [4, 5, 3, 4, 5]
The second test case however, did not go that smooth.
Test case : convert [1, 2, 3, 4, 5] into [4, 2, 3, 4, 5] using copyWithin(0, 3, 4)
LRANGE MyList 0 -1
> [1, 2, 3, 4, 5]
EVALSHA "sha1 of script" 1 MyList 0 3 4
(basically the same as `[1, 2, 3, 4, 5].copyWithin(0, 3, 4)`)
> nil
LRANGE MyList 0 -1
> [4, 5, 3, 4, 5]
After some debugging, I found that the value of targeted_elements is {4, 5} in both the cases, whereas it should be {4} in case 2.
Is there anything suspicious in the loops? Any help would be great.

I fixed this by modifying the following part of the script :
--Fill elements from the list
for i=1, (endIndex-startIndex+1) do
targeted_elements[i] = list[startIndex+i]
end
Changed it into this:
--Fill elements from the list
for i=1, (endIndex-startIndex) do
targeted_elements[i] = list[startIndex+i]
end
The +1 in the for expression added an additional element to the array. It worked in the first case because the selected part was from 3, 5, so 5-3+1 = 3 which means 3 elements should be selected. But since only 2 elements are left, the case still works.
Whereas for the second case, 4-3+1 = 2, which means 2 elements were being selected, instead of 1.

Related

Remove next elements in array with ruby

Given an array containing numbers the following rules apply:
a 0 removes all previous numbers and all subsequent adjacent even numbers.
a 1 removes all previous numbers and all subsequent adjacent odd numbers.
if the first element of the array is 1 it can be removed
I am trying to write an algorithm to reduce the array but I could come up only with a bad looking solution:
def compress(array)
zero_or_one_index = array.rindex { |element| [0,1].include? element }
array.slice!(0, zero_or_one_index) if zero_or_one_index
deleting = true
while deleting
deleting = false
array.each_with_index do |element, index|
next if index.zero?
previous_element = array[index - 1]
if (previous_element == 0 && element.even?) ||
(previous_element == 1 && element.odd?)
array.delete_at(index)
deleting = true
break
end
end
end
array.shift if array[0] == 1
end
The problem is that delete_if and similar, start messing up the result, if I delete elements while iterating on the array, therefore I am forced to use a while loop.
Examples:
compress([3, 2, 0]) #=> [0]
compress([2, 0, 4, 6, 7]) #=> [0,7]
compress([2, 0, 4, 1, 3, 6]) #=> [6]
compress([3, 2, 0, 4, 1, 3, 6, 8, 5]) #=> [6,8,5]
This problem arises in the context of some refactorings I am performing on cancancan to optimize the rules definition.
Here is how I would solve the problem:
def compress(arr)
return arr unless idx = arr.rindex {|e| e == 0 || e == 1}
value = arr[idx]
method_options = [:even?,:odd?]
arr[idx..-1].drop_while do |n|
n.public_send(method_options[value])
end.tap {|a| a.unshift(value) if value.zero? }
end
First we find index of the last occurrence of 0 or 1 using Array#rindex. If none then return the Array.
Then we get the value at that index.
Then we use Array#[] to slice off the tail end of the Array starting at the index.
Then drop all the consecutive adjacent :even? or :odd? numbers respective to the value (0 or 1) using Array#drop_while.
Finally if the value is 0 we place it back into the front of the Array before returning.
Examples
compress([3, 2, 0])
#=> [0]
compress([2, 0, 4, 6, 7])
#=> [0,7]
compress([2, 0, 4, 1, 3, 6])
#=> [6]
compress([3, 2, 0, 4, 1, 3, 6, 8, 5])
#=> [6,8,5]
compress([4, 5, 6])
#=> [4,5,6]
compress([0])
#=> [0]
compress([1])
#=> []
If your goal was to be mutative, as your question and gist seem to suggest, I honestly would not change what I have but rather go with:
def compress!(arr)
arr.replace(compress(arr))
end
For example
a = [3, 2, 0, 4, 1, 3, 6, 8, 5]
a == compress!(a)
#=> true
a
#=> [6,8,5]

Ruby merge sort -- how does this recursive function work?

I'm doing an assignment writing a recursive merge sort algorithm in Ruby. I'm trying to break this down piece by piece in order to be able to wrap my head around it. What I have so far is trying to accomplish the "divide" step until there is only one element left in each array.
a = [5, 2, 4, 6, 1, 7, 3, 8]
def divide(arr)
return arr if arr.length < 2
else
arr1 = puts divide(arr[0..arr.length/2-1])
arr2 = puts divide(arr[arr.length/2..arr.length])
end
I would think the output would be:
[5] [8]
But it prints out:
5
2
4
6
1
7
3
8
How does it work?
You have at least two problems.
First, the else statement has no effect, it's not how you do if else in Ruby.
Second, if a.length < 2 is false then your method will return nil. puts returns nil, x = nil returns nil.
I've added some prints to demonstrate how your code works, I hope it'll help:
$level = 0
def divide(arr)
return arr if arr.length < 2
$level += 1
puts "Working with array #{arr}"
arr1 = divide(arr[0..arr.length/2-1])
puts "Level = #{$level} arr1 = #{arr1}"
arr2 = divide(arr[arr.length/2..arr.length])
puts "Level = #{$level} arr2 = #{arr2}"
$level -= 1
nil
end
divide([5, 2, 4, 6, 1, 7, 3, 8])
The output:
Working with array [5, 2, 4, 6, 1, 7, 3, 8]
Working with array [5, 2, 4, 6]
Working with array [5, 2]
Level = 3 arr1 = [5]
Level = 3 arr2 = [2]
Level = 3 arr1 =
Working with array [4, 6]
Level = 4 arr1 = [4]
Level = 4 arr2 = [6]
Level = 4 arr2 =
Level = 4 arr1 =
Working with array [1, 7, 3, 8]
Working with array [1, 7]
Level = 6 arr1 = [1]
Level = 6 arr2 = [7]
Level = 6 arr1 =
Working with array [3, 8]
Level = 7 arr1 = [3]
Level = 7 arr2 = [8]
Level = 7 arr2 =
Level = 7 arr2 =

How do I remove the beginning elements of my array only if the first element satisfies a condition?

In Ruby, let's say I have an array of ordreed, unique numbers
[0, 1, 2, 4, 6, 8, 10]
If the first element of the array is zero, how do I remove all the elements from teh beginning of the array that are consecutive, starting wiht zero? That is, in the above example, I would want to remove "0", "1", and "2" leaving me with
[4, 6, 8, 10]
But if my array is
[1, 2, 3, 10, 15]
I would expect the array to be unchanged because the first element is not zero.
You could use a mix of drop_while and with_index to only remove the first matching elements:
[0, 1, 2, 4, 6, 8, 10].drop_while.with_index{|x, i| x == i}
# [4, 6, 8, 10]
[1, 1, 2, 4, 6, 8, 10].drop_while.with_index{|x, i| x == i}
# [1, 1, 2, 4, 6, 8, 10]
Note that the second and third elements don't get deleted in the second example, even though they're equal to their indices.
Drop elements, as long as they are equal to their index:
a=a.drop_while.with_index{|e,i| e==i}
Sounds like you're trying to delete entities if they match their idx (provided the first idx is 0). Try this:
if array.first == 0
new_array = array.reject.each_with_index{ |item, idx| item == idx }
end
Although this will only work with ordered arrays of unique numbers, if you're not sure that they are then include: array = array.sort.uniq
You could do:
x = -1
while my_array.first == x + 1 do
x = my_array.shift
end
Note that array.shift is the same as array.pop except that it works from the start of the array.
If I understand you right, then it can be one of possible solutions:
def foo(array)
if array.first.zero?
array.keep_if.with_index { |e, ind| e != ind }
else
array
end
end
> foo([0, 1, 2, 5, 6, 7])
#=> => [5, 6, 7]
> foo([1, 2, 3])
#=> [1, 2, 3]
In short form:
a[0] == 0 ? a[3..-1] : a
In longer form:
if a.first == 0
a[3..(a.size)]
else
a
end

Ruby Implementing merge_sort algorithm leaving off elements from input array

I am trying to implement a merge sort function into my app. It takes an array as an input, sorts it, and outputs the sorted array.
def sort(list)
swapped = true
sorted_list = []
slice_count = list.size.to_i
chunked_list = list.each_slice(slice_count).to_a.each{ |element| element.fill nil, slice_count, 0 }.transpose.map(&:compact)
while swapped do
swapped = false
(slice_count-1).times do |i|
if chunked_list[i][0] > chunked_list[i+1][0]
chunked_list[i], chunked_list[i+1] = chunked_list[i+1], chunked_list[i]
swapped = true
end
end
break if !swapped
end
(slice_count-1).times do |i|
sorted_list.push(chunked_list[i][0])
end
puts "Sorted list (merge): #{sorted_list}"
end
My issue arises from getting the input array.
Running merge.sort([0, 3, 8, 5, 4, 9, 22]) outputs a sorted array WITHOUT the 0 and 22: Sorted list (merge): [3, 4, 5, 8, 9]
Debugging and returning the 'list' variable in pry gives me [3, 4, 5, 8, 9, 22], which includes the 22 not present in the final output, but still excluding the 0 element from the input array. Why is it not taking the full array?
you need to remove - 1 on (slice_count).times do
def sort(list)
swapped = true
sorted_list = []
slice_count = list.size.to_i
chunked_list = list.each_slice(slice_count).to_a.each{ |element| element.fill nil, slice_count, 0 }.transpose.map(&:compact)
while swapped do
swapped = false
(slice_count-1).times do |i|
if chunked_list[i][0] > chunked_list[i+1][0]
chunked_list[i], chunked_list[i+1] = chunked_list[i+1], chunked_list[i]
swapped = true
end
end
break if !swapped
end
(slice_count).times do |i|
sorted_list.push(chunked_list[i][0])
end
puts "Sorted list (merge): #{sorted_list}"
end
Sorted list (merge): [0, 3, 4, 5, 8, 9, 22]

Iterate through array forwards then backwards

[1,2,3,4,5]
=>1,2,3,4,5,4,3,2,1
=>1,2,3,2,3,4,5,4,3 #I need to be able to reverse the iteration at certain points
I first tried something like:
a = [1,2,3,4,5]
a.each {|i|
if i % 9 == 0
a.reverse!
}
but that just reverses the entire array and starts counting from the index it left off on. I need to to shift the direction of each, so to speak.
i, counter = 0, 1 # initialize index to 0, counter to 1
while(i < a.length && i >= 0) do
puts a[i]
i+= counter # increment counter
counter*= -1 if(condition) # Multiply counter with -1 to reverse it
end
Well, here's a moving "cursor" for your array:
module Cursor
def current_index
#current_index ||= 0
end
def step
#current_index = current_index + direction
handle_boundary
end
def step_back
#current_index = current_index + (direction * -1)
handle_boundary
end
def handle_boundary
if current_index == length || current_index == 0
turn_around
end
end
def direction
#direction ||= 1
end
def turn_around
#direction = direction * -1
end
def current
self[current_index]
end
end
And here's how you use it:
array = [1,2,3,4,5]
arary.extend Cursor
array.current # returns the item in current position
array.step # moves a step forward, turns around when it reaches either end of the array
array.step_back # moves a step backward without switching the direction
array.turn_around # switch the direction
Now you can travel around as you want :D
You can make use of Enumerator class to create custom enumerable that can providing custom iteration through the array. In below code, I am monkey-patching Array class for convenience (also due to resemblance of the method to Array#cycle), though solution can be done without monkey-patching as well.
class Array
def reversible_cycle
Enumerator.new do |y|
index = 0
direction = :forward
loop do
direction = :backward if index + 1 >= size
direction = :forward if index <= 0
y << self[index]
index += (direction == :forward ? +1 : -1)
end
end
end
end
p [1,2,3,4,5].reversible_cycle.take(9)
#=> [1, 2, 3, 4, 5, 4, 3, 2, 1]
p [1,2,3,4,5].reversible_cycle.take(13)
#=> [1, 2, 3, 4, 5, 4, 3, 2, 1, 2, 3, 4, 5]
p [1,2,3,4,5].reversible_cycle.take(17)
#> [1, 2, 3, 4, 5, 4, 3, 2, 1, 2, 3, 4, 5, 4, 3, 2, 1]
p [1,2,3,4,5].reversible_cycle.take(21)
#=> [1, 2, 3, 4, 5, 4, 3, 2, 1, 2, 3, 4, 5, 4, 3, 2, 1, 2, 3, 4, 5]
For scenarios where you are changing direction without iterating the array fully in one direction, you will have to give some examples so that one can see how to modify the above code to accommodate that
You could use Ruby's under-appreciated flip-flop operator.
arr = [1,2,3,4,5]
sz = arr.size
(2*sz-1).times { |i| puts i==0..i==arr.size-1 ? arr[i] : arr[sz-i-2] }
1
2
3
4
5
4
3
2
1

Resources