NOT ABLE TO SUBMIT A SUCCESSFULLY RUNNING CODE IN BASH (UNIX)

Hi All,

Im trying to submit a successfully running code. Im able to get the output as asked. But Im getting the desired values in the output and getting an error (NZEC error) in the error box and Im not able to submit the code.

Can some one please guide me how to submit the code or tell me what the error is.

Thanks in advance,

NZEC Stands for “NON ZERO EXIT CODE”, this means your program is not successfully executing.
There must be some edge cases where your program gets into some runtime trouble. I suggest you to think carefully what can cause your program to go out of bounds. Like segmentation faults maybe.

Thanks a lot Yesh. The code is giving the correct answer with out any errors. But when I’m asking to submit, a red prompt pops up saying wrong answer. Can you please tell me why.

share the code and problem

PROBLEM:-


Your program is to use the brute-force approach in order to find the Answer to Life, the Universe, and Everything.
More precisely… rewrite small numbers from input to output.
Stop processing input after reading in the number 42. All numbers at input are integers of one or two digits.

Example
Input:
1
2
88
42
99

Output:
1
2
88


SOLUTION


#!/bin/bash

for i in 1 2 88 42 99;
do
if [ $i -eq 42 ]; then
exit 0
fi
echo $i
done

Thanks a lot for looking into this probelm.

The Problem is Test.

@hoody’s attempted solution is here.

@hoody - your solution needs to read numbers in from stdin and print them out until one of the numbers is 42, and you are not doing this.

For example, if I pipe the following test input into stdin:

5
17
5
29
42

then your solution should print (to stdout):

5
17
5
29

but instead it’s hard-coded to print out:

1
2
88
3 Likes

ssjgz …ohhk… I tried doing that first and when I got some error, hard corded the values. i will try to give the standard input again.

Thank you :slight_smile:

1 Like

try reading input from terminal using int(input)

Hi Uday,

I have tried to read the input from the terminals. It worked in another compiler. But does not read from input when I compile it in the codechef.

Please find the code below: -

#!/bin/bash

x=y

while [ “$x” = “y” ]
do

echo “Enter the number”
read n
echo $n

if [ $n -ne 42 ]; then
x=y
else
x=z
fi

done

Can you please help me with this code.

Please either format your code or link to your submission - the forum software has mangled it and introduce errors! :slight_smile:

For a start, don’t echo "Enter the number" - your output has to match the expected output precisely, and the expected output won’t include a line with Enter the number in it :slight_smile:

Edit:

Actually, as it happens, the mangling doesn’t seem to affect things too much - removing that echo "Enter the number" will get you most of the way there, but you’ll still fail the sample testcase, but it’s hopefully obvious how to fix that :slight_smile:

2 Likes

Hi ssjgz,

Thanks for reverting back so quickly. I just realized I made some mistakes in the code.
eg: I did not use $x instead I used x to assign value y and z to assign to the variable. And I made some changes to the code too on your recommendation.

#!/bin/bash

x=y

while [ “$x” = “y” ]
do

read n

if [ $n -ne 42 ]; then
$x=y
else
$x=z
fi

done

But still was facing the same issue. Looking into it closly, I realised that the read n function is not working as expected. Can you please tell me why would that happen. as every thing looks fine to me.

@hoody - this has actually just introduced more errors - you were closer to a solution with your previous attempt :slight_smile:

Running this version, I now get:

>echo "5
17
5
29
42" | ./hoody-life-universe-and-everything.sh
./hoody-life-universe-and-everything.sh: line 11: y=y: command not found
./hoody-life-universe-and-everything.sh: line 11: y=y: command not found
./hoody-life-universe-and-everything.sh: line 11: y=y: command not found
./hoody-life-universe-and-everything.sh: line 11: y=y: command not found
./hoody-life-universe-and-everything.sh: line 13: y=z: command not found
./hoody-life-universe-and-everything.sh: line 10: [: -ne: unary operator expected
./hoody-life-universe-and-everything.sh: line 13: y=z: command not found
./hoody-life-universe-and-everything.sh: line 10: [: -ne: unary operator expected
./hoody-life-universe-and-everything.sh: line 13: y=z: command not found
./hoody-life-universe-and-everything.sh: line 10: [: -ne: unary operator expected

with the last two lines repeating over and over :slight_smile:

2 Likes

ok…changing x to $x created the problem. find the code below: -

#!/bin/bash

#set -x

x=y

while [ “$x” = “y” ]
do

read n
echo $n
if [ $n -ne 42 ]; then
x=y
else
x=z
fi

done

OUTPUT:

28
28
36
36
42
42

Its working as expected in a different compiler. Just not able to submit here. And the read n function would not work as it should …

Seems to be working fine for me:

although it’s giving the wrong answer for an easily-fixed reason, as I mentioned a couple of posts above :slight_smile:

2 Likes

OK…I didnt know that code was supposed to read from custom input… Good lesson.
I corrected my code and submitted it and the output seems to be exactly as required. But still not able to submit the answer. Looks like my bad day. Any help on that please. …?

OMG…it said correct answer. Thanks a lot to all you guys out there.

It was asking me not to display 42 in the output. the below code worked.

#!/bin/bash

x=y

while [ “$x” = “y” ]
do

read n

if [ $n -ne 42 ]; then
x=y
else
x=z
exit 0
fi

echo $n

done

Thanks a lot to all of you…That was my first submission.

1 Like

if you get stuck in a certain problem try looking out for solutions in “all submissions” section of the problem . and try to learn the concept from them . this is best way to start.
@hoody

Uday,

Sure. Thanks a lot for the help and advise.