Codechef problems lol :D

This is very strange. The problem doesn’t even start with a function kind of already filled out. How are we supposed to know where the computer is going to input like on codewars? I know this is right if there were a function filled out already.

Name of problem: Practice makes us perfect. My code is below. (:

function weeks(problem) {
    
let numberOfWeeks = 0;

for (let i = 0; i < problem.length; i++) {
    if (problem >= 10) {
        numberOfWeeks = numberOfWeeks + problem[i];
    }
 }
return numberOfWeeks;
}

hello,
you don’t actually have a javascript question, more of a “how do I use the CodeChef website” question.
But that’s okay. Here’s what you need to do:

Go back to the description of the exercise, then read it.
Some of things you may want to read include:

the section titled " Input Format"
the section titled “Output Format”
the section titled “Constraints”

Perhaps you neglected to scroll down to review them the first time?
(they mention for eg that the function you are to write needs 4 inputs)

Yes, I DID read that, but I didn’t see how that translated to “this is the function format” lol. I have a learning disability, sorry.

still don’t understand what function to start with for the inputs the computer will input. Maybe I DID read it but didn’t understand still? That’s why I am asking (:

I’m just gonna stick to codewars where the problems don’t add fluff and give you all you need to solve the problem (:

okay fair.

Which sentence did you have trouble understanding? I would like to help you, but in order to help me do that, you will have to try to be specific.

I’m not sure how to advise you because I don’t personally know you but if you are interested in a professional job as a developer, you should consider trying to work through vague descriptions of code requirements. After all, people are quite vague. (just look at this conversation we are having. We barely understand each other. )

There is a single line of input, with 4 integers p1,p2,p3,p4. What does single line have to do with anything? The way this problem is worded is really throwing me off. It doesn’t get to the point and give you everything you need

okay so ‘single line’ probably means something like:

myFunction( 1, 2, 3, 4);

(so someone is calling your function with a single line of parameters consisting of 4 integers)

Edit: I second what Randy is saying below. Ignore this.

this would be correct. but since the problem did not come with a function already written, it threw me off. can’t even log the computer’s input to the console to see what the input is lol.

function weeks(problem) {
    
let numberOfWeeks = 0;

for (let i = 0; i < problem.length; i++) {
    if (problem >= 10) {
        numberOfWeeks = numberOfWeeks + problem[i];
    }
 }
return numberOfWeeks;
}

I found one of the submissions, and I have NO idea how this is the right answer… Are we supposed to program the actual test?

let reader = new BufferedReader( new InputStreamReader(System['in']) );

let firstLine = reader.readLine();

let num = firstLine.split(' ');
    
let arr = [];
    
for( i = 0; i < 4; i++ ) {
    arr.push(parseInt(num[i]));
}

let output = 0;

for( index = 0; index < arr.length; index ++) {
    if(arr[index] >= 10) {
        output += 1;
    }
}

print(output);

this one here looks like C code.
You’re not using C are you? (I assumed you were using javascript)

nevermind: i see the lets, maybe it is JS

This is not like you are used to, in which you write a function that get an input

here your input is a stream of data, let’s say it is sort like a file. You need to read that, calculate the requested result, and output the result to a stream of data

the process api is documented here:
https://nodejs.org/api/process.html

I guess you need to give the output with process.stdout

EDIT: see Randy’s post below for better answer

1 Like

The instructions do not say anything about creating a function. The instructions state you must take the input and create a specific output. I used NodeJS as I am more familiar with how to process input as this site appears to feed it to the user.

For NodeJS, you would do something like:

process.stdin.resume();
process.stdin.setEncoding('utf8');

let input = '';
process.stdin.on('data',chunk => input += chunk);
process.stdin.on('end',()=> {
  /*
  write your code here referencing the `input` variable 
  as the actual input received
  */
  console.log(result);// result would be the output expected
})

A slightly shorter version would be:

process.stdin.resume();
process.stdin.setEncoding('utf8');

process.stdin.on('data', input => {
  /*
  write your code here referencing the `input` variable 
  as the actual input received
  */
  console.log(result);// result would be the output expected
});

Thank you so much, Randell. The problem is that I have not yet studied NodeJS, so I have some studying to do before tackling this one. I appreciate this response and am going to save it for my notes (:

Just wanted to apologize for my earlier assumption… I should have assumed the question was more nuanced.

1 Like

Just to mention: This might be mostly a problem caused by sites like CodeWars/CodinGame, but there is/was a print in JavaScript.

Often, you will find solutions for Code Golf challenges use print instead of console.log because … shorter.

I know because I am guilty of this:

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.