There is no Spoon - Episode 1 puzzle discussion

also for example for task 07 (complex)

00.0
0.00
.0.0
000.

first turn is correct but for second I get

Output
1 0 -1 -1 -1 -1
### Game information:
(-1, -1) is not to the right of (1, 0). Expected (3, 0).

How for point 1 0 next to the right can be 3 0 ?
next is 2 0 which is mean -1 - 1

1 Like

Please refer to my comment immediately above yours.

I am stumped
 I’m finding most of the problems on this site to be pretty difficult to understand. I have made progress in this “There Is No Spoon” challenge, but I can’t quite figure out why I’m not passing all the tests. With my current code, I pass tests 1, 2, 3, and 7. If I move the console.log() down (so it’s at the end of the outer for loop), I no longer pass tests 1, 2, or 7, but I now pass tests 3 and 6. I have no idea what’s going on here, lol! I’m proud of myself for getting this far, but I feel like my brain is slowly shutting down. Can someone give me some direction as to what I’m missing? Am I on the right track, or have I passed some of the tests by some fluke?

const width = parseInt(readline()); // the number of cells on the X axis
const height = parseInt(readline()); // the number of cells on the Y axis
let x1 = '';
let y1 = '';
let x2 = '';
let y2 = '';
let x3 = '';
let y3 = '';
const grid = [];

for (y = 0; y < height; y++) {
    const line = readline();// width characters, each either 0 or .
    grid.push(line.split(''));
}

for (let y = 0; y < height; y++) {
    for (let x = 0; x < width; x++) {
        if (grid[y][x] != 0) {
            x++;
        }
        if (grid[y][x] == 0) {
            x1 = x;
            y1 = y;
            if (x + 1 < width) {
                for (let xx = x + 1; xx < width; xx++) {
                    if (grid[y][xx] == 0) {
                        x2 = xx;
                        y2 = y;
                        break
                    } else {
                        x2 = -1;
                        y2 = -1;
                    }
                }
            } else {
                x2 = -1;
                y2 = -1;
            }

            if (y + 1 < height) {
                for (let yy = y + 1; yy < height; yy++) {
                    if (grid[yy][x] == 0) {
                        x3 = x;
                        y3 = yy;
                        break;
                    } else {
                        x3 = -1;
                        y3 = -1;
                    }
                }
            } else {
                x3 = -1;
                y3 = -1;
            }
        }
          console.log(x1, y1, x2, y2, x3, y3);  //if I put concole.log here, I pass tests 1 through 3 and 7.
    }
    //if I put the console log here, I pass tests 3 and 6.
}

Note that you are required to output a line only if there is a node at a location. So, it’s better to

  • structure your code better using if/else instead of just ifs (grid[y][x] != 0 vs grid[y][x] == 0), and
  • make sure console.log is within part of an if block.

Writing this piece of code was more enjoyable than I thought it might be; also that was an interesting and fun problem to solve! Thank you for this!

hi, I’m new to coding and i wanna use recursive function on python but doesn’t know why it fails. Can you help me, I don’t understand what i do the wrong way.

def droite(x,y,tableau,lim):
    x=x+1
    int(lim)
    print(x,y,lim, file=sys.stderr, flush=True)
    if x>=lim:
        x=-1
        y=-1
        return x, y
    elif tableau[y][x]=="0":
        return x, y
    else:
        droite(x,y,tableau,lim)


def Bas(x,y,tableau,lim):
    y=y+1
    int(lim)   
    print(x,y,lim, file=sys.stderr, flush=True)   
    if y>=lim:
        x=-1
        y=-1
        return x, y
    elif tableau[y][x]=="0":
        print(x,y,5, file=sys.stderr, flush=True)
        return x, y
    else:
        Bas(x,y,tableau,lim)


width = int(input())  # the number of cells on the X axis
height = int(input())  # the number of cells on the Y axis
x1=0
y1=0
x2=-1  
x3=-1  
y2=-1  
y3=-1  
tab=[]
x2=y2=x3=y3=-1
for i in range(height):
    line = input()  # width characters, each either 0 or .
    L=str(line)
    tab.append(L)

for i in range(height):
    for j in range(width):
        if tab[i][j]=='0':
            x1=j
            y1=i
            x2,y2=droite(x1,y1,tab,width)
            x3,y3=Bas(x1,y1,tab,height)
            print(x1,y1,x2,y2,x3,y3) 

If you add two more lines to your Bas function:

def Bas(x,y,tableau,lim):
    print("bas",x,y,tableau,lim, file=sys.stderr, flush=True)   
    y=y+1
    int(lim)   
    print(x,y,lim, file=sys.stderr, flush=True)   
    if y>=lim:
        x=-1
        y=-1
        return x, y
    elif tableau[y][x]=="0":
        print(x,y,5, file=sys.stderr, flush=True)
        return x, y
    else:
        Bas(x,y,tableau,lim)
        print("none", file=sys.stderr, flush=True)

and then run the first test case, you’ll notice that the function eventually returns None. Hence the error that follows:

TypeError: cannot unpack non-iterable NoneType object

because you cannot unpack None into x3 and y3.

In other words, the Bas function does not return x and y as you intend it to. You would want to further investigate that.

thx for the answer. i will come back if i found another error

Good puzzle, for me problem was 2d arrays and showing (column,row) instead (row,column) so i got a little bit confused.
But it’s good puzzle, i learned some new things

Hi, i’m using C :slight_smile:

First of all, have to change the scanf to get all lines in an array of line Lines[32][32] // lines[line][column] see spec for the limits. normally 30 but no drama :wink:

MindSet: Functionnal Breakdown :wink:
I parse the array with 2 nested for loop [line, column].
for each “0”:

  • print Coords;
  • HandleRight;
  • HandleBot;
    and don’t forget to print a new line
 Indeed you must output n lines for n nodes in the field :wink:

HandleXxx are sub functions use to search a node in a direction, starting from current node

Bonjour,

Je confirme que l’énoncĂ© pourrait vraiment gagner Ă  avoir cette information reprĂ©cisĂ©e : le hint sur l’imbrication des boucles prend alors tout son sens Ă©galement :slight_smile:

I was surprised that a lot of java solutions weren’t using List or an Object approch.

Il m’est arivĂ© la meme chose
autant dire :slight_smile: Si le node possede un noeud a sa droite sur la meme ligne. idem, si un noeud possede un noeud sur la meme colonne.

Hey there! So I’ve been stuck for quite a while on this problem:
For some background: I did have a previous “working solution”, but half the tests failed because I didn’t take in account that the neighbouring nodes don’t have to be “immediate neighbours” (i.e. Node (0,0) and Node (1,0) are immediate neighbours, but Node (0,0) and Node (2,0) are neighbours but my program didn’t acknowledge that)

So I tried another approach: by using a struct representing the coordinates of a node, and I created two functions that search of each neighbouring node:

Couple searchNextR(string *grid, int x, int y, int w){
    int i = x;
    Couple c;
    c.y = y;


    cerr << w << endl;

    while (i <= w || grid[y].at(i) == '0'){

        if (i == w-1){
            break;
        }

        i++;
        c.x = i;
        cerr << i << endl;
    }

    if (grid[y].at(i) != '0'){
        c.x = -1;
        c.y = -1;
    }
    
    return c;
}

Couple searchNextD(string *grid, int x, int y, int h){

    int i = y;
    Couple c;
    c.x = x;

    cerr << h << endl;
    while (i <= h || grid[i].at(x) == '0'){

        if (i == h-1){
            break;
        }
        i++;
        c.y = i;
        cerr << i;
    }

    if (grid[i].at(x) != '0'){
        c.x = -1;
        c.y = -1;
    }
    
    return c;
}

but then I get a weird Standard output, which fails the first test :

1 0 21998 0 -1 -1

can someone help? I think it might be an overflow, but I don’t know where that overflow is


Continuing the discussion from There is no Spoon - Episode 1 puzzle discussion:

Hi,
There’s a biased interpretation of the French version of the riddle:
“the next node to the right” => “premier nƓud situĂ© sur sa droite”.
It’s understood that this refers only to the neighbor located directly in the first coordinate to his right. I saw in the comments that I wasn’t the only one who understood this.

You’d have to change it to something like :
“the nearest neighbor node located on its right” => “le nƓud plus proche voisin situĂ© sur sa droite”.

I don’t know if the ambiguity is present in English for a native speaker.
The problem is the same for the node below.

My c++ code doesn’t work. I don’t undertsnad.

#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <sstream>

using namespace std;

/**
 * Don't let the machines win. You are humanity's last hope...
 **/

int main()
{
    int width; // the number of cells on the X axis
    cin >> width; cin.ignore();
    int height; // the number of cells on the Y axis
    cin >> height; cin.ignore();
    vector <string> lines;
    for (int i = 0; i < height; i++) {
        string line;
        getline(cin, line); // width characters, each either 0 or .
        lines.push_back(line);
    }
    string rep ="";
    for (int i = 0; i < height; i++) 
        for( int j =0; j < width; j++) 
            if( lines[i][j] ==  '0') {
                std:ostringstream os;
                os.clear();
                os.str(std::string());
                os<< j << " " << i << " ";
                rep= os.str();
                // rep+= std::to_string(j)+ to_string(' ')+std::to_string(i) + to_string(' ');
                unsigned foundw = 0;
                for( unsigned k = j+1; k < width; k++) 
                    if( lines[i][k] == '0') if( foundw == 0){
                        os.clear();
                        os.str(std::string());
                        //cerr << "Debug messages..."<< os.str() << endl;
                        os << k << " " << i << " ";
                        rep+= os.str();
                        // rep+= std::to_string(k)+ to_string(' ')+std::to_string(i) + to_string(' ');
                        foundw = 1;
                    }
                if( foundw == 0) {
                    //rep+= std::to_string(-1) + std::to_string(-1)  +  std::to_string(' ' );
                    os.clear();
                    os.str(std::string());
                    os << "-1 -1 ";
                    rep+=os.str();
                }
                unsigned foundh = 0;
                for( unsigned k = i+1; k < height; k++) if( foundh ==0) {
                        os.clear();
                        os.str(std::string());
                        os << j << " " << k << " ";
                        rep+=os.str();
                     //   rep+= std::to_string(j)+ to_string(' ') +std::to_string(k) + to_string(' ');
                        foundh = 1;
                }
                if( foundh == 0) {
                    //rep+= std::to_string(-1) + std::to_string(-1)  +  std::to_string(' ' );
                    os.clear();
                    os.str(std::string());
                    os << "-1 -1 ";
                    rep+=os.str();
            
                }
                
                cout << rep << endl;
            }

    // Write an action using cout. DON'T FORGET THE "<< endl"
    // To debug: cerr << "Debug messages..." << endl;


    // Three coordinates: a node, its right neighbor, its bottom neighbor
 //   cout << "0 0 1 0 0 1" << endl;
}

Your code is like:

if( foundh ==0) {
                        ...
                        foundh = 1;
                }

Your code doesn’t check anything there before setting foundh to 1.

Thanks
It was 1 pm yesterday, I could not think anymore!!

I don’t udersand any thing :sweat_smile:

It might be stupid answer, I’m very new to this, but I used recursive functions in python