Follow

I have a programming problem I'm unsure how to solve.
I have two arrays and a function. The function works on array1 on a given range. Array2 contains ranges in array1 that are forbidden to be worked on.

· · Web · 3 · 1 · 2
@matrix Best solution probably depends on the language.

You can mask off the values in array1 before calling the function, and that's my first guess.

If your function operates element-wise, you can check each element of array1 for not being in array2 right before you pass each element to the function. This won't work if the array structure is meaningful as anything other than a container for your elements, though.

@ceo_of_monoeye_dating I'm working in C.
The array structure is meaningful so I can't touch either of the arrays.

@matrix The way you phrase the problem doesn't make sense to me. What are you trying to accomplish? Write a function that does something with array2's forbidden ranges? What does "works on" mean? What is the interface of the function you are given (parameters and return values and types)?

@johnbudd1350 I want to call the function (the function has 3 arguments, start and end index, and array1) on black parts individually, but the start and end of the red parts is in a different array.

@matrix Ok so for the black parts, you are given a list of ranges right? Your goal is to call the function on these ranges, and ignore the red ones? Why does array2 even matter then? You could just loop through your given ranges, and pass each one into your function along with array1. That would solve it unless I'm missing something

@johnbudd1350 I'm not given a list of ranges. I only know length of array1.

@matrix Ok. So what was in array2 again? It has a list of the red ranges?
@matrix Oh, then you just need to loop through array2 and pass in the starting and ending indices, but opposite. So if array2 looks like

[2,4,6,8]

Then you would pass in

(0,2)
(4,6)
(8, array1.length)

@johnbudd1350 Thanks, but how to do it when a range in array2 starts at 0 or ends at end of array1?
If it doesn't you first need to call the function before looping through array2, but you don't know where to end before looping through array2.
I think I might need to make a new array with the acceptable ranges and just loop through that.

@matrix @johnbudd1350 in all cases you must handle
* starting from 0
* ending at end of array1
* cases where indexes exist in both arrays and what that means

it's a fun challenge
@matrix You could check the first and last indices in array2, and change your starting pairs accordingly
@matrix @johnbudd1350
can you create a temporary structure or can you only work on the arrays in-place?
@matrix @johnbudd1350
I'd create your diagram in a new array, containing only indexes. each index means 'switch between black and red'. then traverse that

@Corfiot @johnbudd1350 I can create anything I want. Just can't touch the arrays.

Sign in to participate in the conversation
Game Liberty Mastodon

Mainly gaming/nerd instance for people who value free speech. Everyone is welcome.