Complex input structure and stub generator

Hello,
I’m making an IN/OUT puzzle and I have this problem with the stub generator, that is - I don’t know how to do it since it’s a little complicated and all I tried has failed.

In this puzzle, the program should read the number of different polygons, then it should read the number of vertices of each polygon (in a loop; would be usually different) and then it should read the polygons’ vertices (in another loop or multiple loops). Alternatively, it could read the vertices right after reading the number of them for each polygon or they could be written in one line.

So these are some of the ways the input could look like:

Input version 1
2
3
3
X11 Y11
X12 Y12
X13 Y13
X21 Y21
X22 Y22
X23 Y23
Input version 2
2
3
X11 Y11
X12 Y12
X13 Y13
3
X21 Y21
X22 Y22
X23 Y23

And this would be the code that I’d like the stub generator to generate (examples in C and Python):

Version 1

C

int n;
scanf("%d", &n);
int m[n];
for (int i = 0; i < n; i++) {
    scanf("%d", &m[i]);
}
for (int i = 0; i < n; i++) {
    for (int j = 0; j < m[i]; j++) {
        float x;
        float y;
        scanf("%f%f", &x, &y);
    }
}

Python

n = int(input())
m = []
for i in range(n):
    m.append(int(input()))
for i in range(n):
    for j in range(m[i])
        x, y = [float(j) for j in input().split()]
Version 2

C

int n;
scanf("%d", &n);
for (int i = 0; i < n; i++) {
    int m;
    scanf("%d", &m);
    for (int j = 0; j < m; j++) {
        float x;
        float y;
        scanf("%f%f", &x, &y);
    }
}

Python

n = int(input())
for i in range(n):
    m = int(input())
    for j in range(m)
        x, y = [float(j) for j in input().split()]

The closest I got was this stub:

read n:int
loop n read m:int
loop m read x:float y:float

which obviously doesn’t work. :slightly_frowning_face:

I don’t know if there is a way to do arrays or put multiple commands in one loop but I sure didn’t see a documented one. I wouldn’t care much about this but since this is an IN/OUT puzzle, it doesn’t let me try out my code or publish it unless the stub matches the input data. Do you know of a possible approach?

Thank you!

Can’t you just read the strings and let the player parse it afterwards? (as far as I know this is how it goes for C/C++, the stub generator is somewhat limited).

You can copy paste something like this (rename the variables as you want) - at least you will be able to enter the IDE:

read i:int
read j:int
read k:int
loop j read jRow:string(256)
loop k read kRow:string(256)

STATEMENT
Do the input parsing yourself!

NB: The above stub is for your version 1.

@Rafarafa your solution doesn’t work once there are more than 2 polygons.

An other idea would be to specify p, how many points there will be:

read p:int
read n:int
loop n read m:int
loop p read x:float y:float
write answer

So that we have n polygons, each with m points for a total of p points, and let the user put 2 and 2 together. Not optimal but at least no parsing for C++me.

It seems better to move read p:int to be after loop n read m:int.

1 Like

Ah sorry, I misread the OP. I don’t know why but I thought the number of coordinates was variable instead (x, y, z etc.).

1 Like

The above suggestion of letting coders to parse the string is the way to go.
You can make your input to look like this:

3
6 X11 Y11 X12 Y12 X13 Y13
8 X21 Y21 X22 Y22 X23 Y23 X24 Y24
10 X31 Y31 X32 Y32 X33 Y33 X34 Y34 X35 Y35

One line for one polygon.
For each polygon line, read one int and then read the rest as a string.
The int value indicates how many float point numbers are following and coders can parse the string easily.

Optionally, you can even drop the leading int. When coders parse the string they will discover how many float values are there so that they know what kind of polygon it is.

1 Like

Thanks, I will use this approach so I don’t have to change the format of the inputs much.