Posts

countfirst1.js

Image
  /*Count first You have been given a sequence of n integers. Lets say the given sequence is a0, a1, a2, a3 ... an-1. You need to find the number of occurrences of the first value (i.e. a0) in the given sequence. Input Format: First line contains an integer n, denoting the length of the sequence. Next n lines contains one integer each. Output Format: One integer, denoting the result, as mentioned above. Example: Input: 5 10 20 30 40 10 Output: 2 Explanation: First line contains 5, meaning the sequence has 5 integers. First element is 10. In the given sequence, 10 occurs 2 times. So, the output is 2. */ 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 noOfInputs = parseInt ( readLine ()); // 5 let ...

amazingsum.js

Image
  /*Amazing Sum You have been given n integer values. Lets say the given values are [latex]a_1[/latex], [latex]a_2[/latex], [latex]a_3[/latex], [latex]a_4[/latex] ... [latex]a_n[/latex] If the sum of two consecutive input values is greater than 100, then the given values have amazing sum Input Format: First line denotes n, the number of inputs. The next n lines contains one integer in each line. Output Format: One string, either true or false, denoting whether the given values has amazing sum or not. Example: Input: 5 20 12 23 41 17 Output: false Explanation: The maximum sum of two consecutive values here is 64 (i.e. 23 + 41), which is not greater than 100, so the answer is false. */ 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 (); } //==========================================================================...

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$