[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.

2 Likes

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!

1 Like

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

Same opinion, I also managed to solve it without recursion, but with while.