Posts

Showing posts from February, 2023

Problem 40. tableof5.js ( coding test)

  /*Question Iterate Given a positive integer n, print the first n natural numbers which are divisible by n. Input Format: One line containing a positive integer, denoting n. Output Format: n lines containing one integer each. Example: Input: 5 Output: 5 10 15 20 25 Explanation: 5, 10, 15, 20, 25 are the first 5 natural numbers divisible by 5. */ let fs = require ( "fs" ); let data = fs . readFileSync ( 0 , 'utf-8' ); let idx = 0 ; data = data. split ( ' \n ' ); function readLine () { idx ++ ; return data[idx - 1 ]. trim (); } //====================================================================================================== let num = parseInt ( readLine ()); //5 for ( let i = 1 ; i <= num; i ++ ) { console. log (num * i); } TERMINAL: Nobins-MacBook-Pro:STOC_MODULE 11SEPT 2022 nobinpunyo$ node tableof5.js <input.txt 5 10 15 20 25 Nobins-MacBook-Pro:STOC_MODULE 11SEPT 2022 nobinpunyo$

Problem 39. arithmetics.js ( coding test)

  /*Question Arithmetics Given two integers a and b, print the following three lines as output. The first line contains the sum of the two numbers. The second line contains the difference of the two numbers (first - second). The third line contains the product of the two numbers. Input The first line contains the first integer, a. The second line contains the second integer, b. Output Print the three lines as explained above. Example Input: 3 2 Output: 5 1 6 Explanation: 3+2 => 5 3-2 => 1 3*2 => 6 */ let fs = require ( "fs" ); let data = fs . readFileSync ( 0 , 'utf-8' ); let idx = 0 ; data = data. split ( ' \n ' ); function readLine () { idx ++ ; return data[idx - 1 ]. trim (); } //====================================================================================================== let a = parseInt ( readLine ()); //3 let b = parseInt ( readLine ()); // 2 console. log (a + b); console. log (a - b); console. log (a * b); TER...

Problem 38. diamond.js

  /*Question Print diamond shape with n=5 */ let fs = require ( "fs" ); let data = fs . readFileSync ( 0 , 'utf-8' ); let idx = 0 ; data = data. split ( ' \n ' ); function readLine () { idx ++ ; return data[idx - 1 ]. trim (); } //====================================================================================================== let n = parseInt ( readLine ()); // 5 for ( let i = 1 ; i <= n; i ++ ) // Responsible for the 5 rows { for ( let j = 1 ; j <= n - i; j ++ ) // Responsible for printing spaces { process.stdout. write ( " " ); } for ( let k = 1 ; k <= 2 * i - 1 ; k ++ ) // Responsible for printing stars * { process.stdout. write ( '*' ); } console. log (); } for ( let i = n - 1 ; i >= 1 ; i -- ) // here we have taken n-1 because we only need 4 rows { for ( let j = 1 ; j <= n - i; j ++ ) // Responsible for printing spaces ...

Problem 37. invertedpyramid.js (***Revise***)

Image
  /*Question Print inverted pyramid with n=5 */ let fs = require ( "fs" ); let data = fs . readFileSync ( 0 , 'utf-8' ); let idx = 0 ; data = data. split ( ' \n ' ); function readLine () { idx ++ ; return data[idx - 1 ]. trim (); } //====================================================================================================== let n = 5 ; for ( let i = n; i >= 1 ; i -- ) // Responsible for the 5 rows { for ( let j = 1 ; j <= n - i; j ++ ) // Responsible for printing spaces { process.stdout. write ( " " ); } for ( let k = 1 ; k <= 2 * i - 1 ; k ++ ) // Responsible for printing stars * { process.stdout. write ( '*' ); } console. log (); } TERMINAL: Nobins-MacBook-Pro:STOC_MODULE 11SEPT 2022 nobinpunyo$ node invertedpyramid.js <input.txt ********* ******* ***** *** * Nobins-MacBook-Pro:STOC_MODULE 11SEPT 2022 nobinpunyo$

Problem 36. fullpyramid.js (***Revise***)

Image
  /*Question Full Pyramid with n= 5 */ let fs = require ( "fs" ); let data = fs . readFileSync ( 0 , 'utf-8' ); let idx = 0 ; data = data. split ( ' \n ' ); function readLine () { idx ++ ; return data[idx - 1 ]. trim (); } //====================================================================================================== let n = 5 ; for ( let i = 1 ; i <= n; i ++ ) // Responsible for the 5 rows { for ( let j = 1 ; j <= n - i; j ++ ) // Responsible for printing spaces { process.stdout. write ( " " ); } for ( let k = 1 ; k <= 2 * i - 1 ; k ++ ) // Responsible for printing stars * { process.stdout. write ( '*' ); } console. log (); } TERMINAL: Nobins-MacBook-Pro:STOC_MODULE 11SEPT 2022 nobinpunyo$ node fullpyramid.js <input.txt * *** ***** ******* ********* Nobins-MacBook-Pro:STOC_MODULE 11SEPT 2022 nobinpunyo$

Problem 35. macbook.js (*** continue is used revise***)

  /*Question Input Format: First line denotes n, the number of inputs. The next n lines contains one integer in each line. Output Format: One integer denoting the result, as mentioned above. Example: Input: 5 10 -20 30 40 50 Output: 130 Explanation: From the given 5 integers, the result will be: 10+30+40+50 = 130 */ let fs = require ( "fs" ); let data = fs . readFileSync ( 0 , 'utf-8' ); let idx = 0 ; data = data. split ( ' \n ' ); function readLine () { idx ++ ; return data[idx - 1 ]. trim (); } //====================================================================================================== let numinput = parseInt ( readLine ()); // 5 let functionalmac = 0 ; for (i = 1 ; i <= numinput; i ++ ) { let lifespan = parseInt ( readLine ()); if (lifespan < 0 ) { continue ; } functionalmac += lifespan; // functionalmac = functionalmac + lifespan } console. log (functionalmac); TERMINAL: Nobins-MacBoo...

Problem 34. train.js

  /*Question Train There is an N-car train. You are given an integer i. Find the value of j such that the following statement is true: "the i-th car from the front of the train is the j-th car from the back." Constraints 1 <= i <= N Input Two space seperated integers, denoting N,i respectively. Output One integer, denoting j. Example Input1: 4 2 Output1: 3 Explanation1: The second car from the front of a 4-car train is the third car from the back. */ let fs = require ( "fs" ); let data = fs . readFileSync ( 0 , 'utf-8' ); let idx = 0 ; data = data. split ( ' \n ' ); function readLine () { idx ++ ; return data[idx - 1 ]. trim (); } //.......................................................................................... let carAndPosition = readLine (). split ( " " ); // ['4', '2'] // console.log(typeof carAndPosition); ['4', '2'] console. log (carAndPosition[ 0 ] - carAndPositi...

Problem 33. patter122333.js

/*Question Pattern122333 Write a program to make such a pattern like right angle triangle with a number which will repeat a number in a row, as shown below. For n = 4, the pattern should be like: 1 22 333 4444 Input One Integer, denoting n. Output The required pattern Example Input1: 5 Output1: 1 22 333 4444 55555 */ let fs = require ( "fs" ); let data = fs . readFileSync ( 0 , 'utf-8' ); let idx = 0 ; data = data. split ( ' \n ' ); function readLine () { idx ++ ; return data[idx - 1 ]. trim (); } //.......................................................................................... let n = parseInt ( readLine ()); // 5 for ( let i = 1 ; i <= n; i ++ ) // responsible for the rows i,e 5 rows { let j = 1 // initialization of while loop let temp = '' while (j <= i) { temp = temp + i; j ++ ; } console. log (temp); } TERMINAL: Nobins-MacBook-Pro:STOC_MODULE 11SEPT 2022 nobinpu...

Problem 32. pattern4.js(*** Revise***)

  /*Question Given a positive integer n, generate the following format. suppose n is 3, then the format is 1 1 1 2 1 1 2 1 2 3 Algorithm: 1) Read the input number 2) Print the pattern */ let fs = require ( "fs" ); let data = fs . readFileSync ( 0 , 'utf-8' ); let idx = 0 ; data = data. split ( ' \n ' ); function readLine () { idx ++ ; return data[idx - 1 ]. trim (); } //.......................................................................................... let num = parseInt ( readLine ()); // 3 let tmp = '' ; for ( let i = 1 ; i <= num; i ++ ) { for ( let j = 1 ; j <= i; j ++ ) { tmp = tmp + ' ' + j; } console. log (tmp); console. log (); } TERMINAL: Nobins-MacBook-Pro:STOC_MODULE 11SEPT 2022 nobinpunyo$ node pattern4.js <input.txt 1 1 1 2 1 1 2 1 2 3 Nobins-MacBook-Pro:STOC_MODULE 11SEPT 2022 nobinpunyo$

Problem 31. pattern3.js (*** Revise***)

  /*Question Given a positive integer n, generate the following format. suppose n is 3, then the format is 1 1 2 1 2 3 Input: One line containing a positive integer, denoting n . Output: Above described format. Example: Input: 5 output: 1 1 2 1 2 3 1 2 3 4 1 2 3 4 5 Algorithm: 1) Read the input number 2) Print the pattern */ let fs = require ( "fs" ); let data = fs . readFileSync ( 0 , 'utf-8' ); let idx = 0 ; data = data. split ( ' \n ' ); function readLine () { idx ++ ; return data[idx - 1 ]. trim (); } //.......................................................................................... let num = parseInt ( readLine ()); // 3 for ( let i = 1 ; i <= num; i ++ ) { let tmp = '' ; for ( let j = 1 ; j <= i; j ++ ) { tmp = tmp + ' ' + j; } console. log (tmp); console. log (); } TERMINAL: Nobins-MacBook-Pro:STOC_MODULE 11SEPT 2022 nobinpunyo$ node pattern3.js <input.txt ...