[Community Puzzle] Offset Arrays

https://www.codingame.com/training/easy/offset-arrays

Send your feedback or ask for help here!

Created by @Andriamanitra,validated by @Konstant,@TBali and @awnion.
If you have any issues, feel free to ping them.

Nice puzzle, a lot of fun in solving it. Managed to solve it without using Recursion, instead I used Dictionary.

1 Like

It’s interesting :slightly_smiling_face:
But i validated all test cases, yet validator 5 don’t validate…
And as i have no feedback on what’s going on with validators… i’m stuck @ 90% :frowning:

I use regex and recursion, everything works perfectly in test cases, but validator 5 must have something that is not covered in test cases…

(i’m doing it in javascript)

Validator 5 is very similar to the test “Only one element” - the difference is that the indexes are positive.

1 Like

The test case 5 “Only one element” validates… (as i said all the tests validates)
I’ve tried it in cutom changing to positive indexes and it works…
3
A[0…0] = 69
B[-3…-3] = 1547
C[133…133] = 55
C[133]
My code returns 55

i also tried it with:
3
A[0…0] = -3
B[-3…-3] = 133
C[133…133] = 55
C[B[A[0]]]
returns 55

I really don’t get where the problem is…

No idea what your problem might be :slight_smile:
Here’s the Validator 5:

3
A[8..8] = 722
B[54..54] = 2
C[0..0] = 4
A[8]

722

2 Likes

VinceD.Ziree, It’s an easy puzzle! Try another way, without regular expressions and other complexities.

Arf…
got a ‘+’ instead of ‘*’ in my regex…(which makes no sense now that i’ve noticed it, but it worked with all test cases and validators except that validator 5 lol)
If i’d tried in custom case with positive single digit array values i would have noticed it… :confused:

My code isn’t that complex (i’m too lazy to bother with something complex lol), and the regex for this aren’t that complex either…
I’ve just been a little dumb :slightly_smiling_face:

Anyway… Thank you !

Same here, I dont think recursion is needed

Please Help me. I passed all test case. But 8/10 validators passed (not 2 and 4).

My Code is this.

Python3

import re

ordered_names = [
    "FIRST", "SECOND", "THIRD", "FOURTH", "FIFTH", "SIXTH",
    "SEVENTH", "EIGHTH", "NINTH", "TENTH", "ELEVENTH", "TWELFTH",
    "THIRTEENTH", "FOURTEENTH", "FIFTEENTH", "SIXTEENTH", "SEVENTEENTH", 
    "EIGHTEENTH", "NINETEENTH", "TWENTIETH", "TWENTY_FIRST", "TWENTY_SECOND", 
    "TWENTY_THIRD", "TWENTY_FOURTH", "TWENTY_FIFTH", "TWENTY_SIXTH"
    ]

ARR = dict()

n = int(input())
nb_array = 0

for i in range(n):
    expression = input()

    arr_name = expression[0]
    
    interval = expression[expression.index("[") + 1: expression.index("]")]
    interval = re.sub(r"(\.+)", " ", interval).split()
    
    values = expression[expression.index("=") + 1:]
    values = values.split()

    first_index, last_index = map(int, interval)

    # intialize Array with dict if not exist
    try:
        eval(arr_name)
    except:
        exec(f"{arr_name} = dict()")
        exec(f"{ordered_names[nb_array]} = {arr_name}")

        if not ARR: ARR = eval(arr_name)

        nb_array += 1
    else:
        pass


    for index in range(first_index, last_index + 1):
        value = values[index - first_index]
        exec(f"{arr_name}[{index}] = {value}")
    
    #print(eval(arr_name))

x = input()

# replace "-" by "_": example: TWENTY-FIRST[-1] ->  TWENTY_FIRST[-1]
x = re.sub(r"([A-Z])-([A-Z])", r"\1_\2", x)

print(eval(x))

Please, give me additional test cases to test this last.

arr_name = expression[0] goes wrong when the arr_name is more than 1 character long or the first characters of different arrays’ names are the same.

1 Like

Hi @Andriamanitra, thanks for this puzzle.
There is an issue on the test called “Negative indexing” :

1
ARR[-5..-3] = 11 22 33
ARR[-4]

You use the name “ARR” for the array while in the description of the puzzle, you say explicitly in the constraints section that

Array names consist only of uppercase letters A to Z

The array ARR should be renamed to A to fit correctly the constraints section (or the constraint about the name should be removed).

Thanks!

I think the constraint was accurate as is, but I rephrased it as “Each array name consists of only uppercase letters (A to Z)” to avoid any possible confusion.

2 Likes

That’s a nice way of fixing this issue, thanks for your time.

1 Like