Posts

Showing posts from January, 2023

Problem 18. predictquadrant.js

  /*Question Predict the quadrant Given a point on a two dimensional plane, predict the quadrant to which it belongs. Quadrants are named as Q1, Q2, Q3 and Q4. Given a point P(x, y), quadrant of P is defined as follows. Q1, if x > 0 and y > 0 Q2, if x < 0 and y > 0 Q3, if x < 0 and y < 0 Q4, if x > 0 and y < 0 Input First line contains a positive integer n, denoting the number of test cases. It is followed by n lines. Each of the n lines contains two space separated integers x and y. Assume that neither x nor y is 0. Output n lines containing the quadrant to which the corresponding point belongs. Example Input: 2 2 -9 1 3 Output: Q4 Q1 First line is 2, which means there are 2 test cases. Next 2 lines will contain one point each. First point is (2, -9). It belongs to fourth quadrant, so we should print Q4. Second point is (1, 3). It belongs to first quadrant, so we should print Q1. */ let fs = require ( "fs" ); let data = fs . readFileSync ( 0 ,...

Problem 17. geometricprogression.js

  /*Question Geometric Progression Given first 3 numbers of a geometric progression, predict the next number. You can refer to the following link for information about geometric preogression https://en.wikipedia.org/wiki/Geometric_progression Input 3 integers should be taken as a input Output single integer. Note: Convert the output to integer Example Input: 2 4 8 Output: 16 */ 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 n1 = parseInt ( readLine ()); // 2 let n2 = parseInt ( readLine ()); // 4 let n3 = parseInt ( readLine ()); // 8 console. log ( parseInt (n3 * (n2 / n1))); // n2/n1 will give the common ratio between the numbers // 4/2 = 2 , therefore 2 is the commo...

Problem 16. chocolatebills.js

  /*Question Chocolate bill Bablu went to a shop to buy some chocolates and ice creams. Help him to calculate the bill. Input 4 lines containing one integer each First line denotes the cost of 1 chocolate. Second line denotes the number of chocolate bought by Bablu. Third line denotes the cost of 1 ice cream. Fourth line denotes the number of ice creams bought by Bablu. Output One line containing one integer, denoting the total money to be paid by Bablu. Example Input: 5 3 15 2 Output: 45 Explanation: First line contains 5, which means the cost of one chocolate is 5 rupees. Second line contains 3, which means Bablu bought 3 chocolates. Third line contains 15, which means the cost of one ice cream is 15 rupees. Fourth line contains 2, which means Bablu bought 2 ice creams. So, the total bill is 45 rupees. */ let fs = require ( "fs" ); let data = fs . readFileSync ( 0 , 'utf-8' ); let idx = 0 ; data = data. split ( ' \n ' ); function readLine () { i...

Problem 15. countfirst.js

  /*Question 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 lengthOfSeq = pa...

Problem 14. odd_even.js

  /*Question Count odd even numbers Count number of odd and even number in given list. Input First line contains length of the list. Each line contains integer specifying each element in list. Output 2 integers in each line specifying count of odd and even numbers respectively. Example Input: 5 12 14 15 13 18 Output: 2 3 */ 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 lengthOfSeq = parseInt ( readLine ()); // 5 let odd_count = 0 ; let even_count = 0 ; for ( let i = 0 ; i < lengthOfSeq; i ++ ) { let integers = parseInt ( readLine ()); // this will read the numbers one by one if (integers % 2 != 0 ) { odd_count ++ ; } else { ...

Problem 13. Ivrs.js (while loop is introduced) lecture 6 stoc module

  /*Question Chillkart is a new online shopping website and wants to build a Ivrs. The Ivrs has the following option: Option 1 - know the details of recent order. Option 2 - know about today's discount offer. Option 3 - know the balance in your account. 0 - exit Chillkart has data of various callers. for each call Chillkart wants to count how many times each option was choosen. Input: Each line containing one integer denoting the customer's choice. Stop processing when 0 is encountered. Output: 3 lines containing one integer each. First line should contain the total number of times the customer choose option 1. Second line should contain the total number of times the customer choose option 2. Third line should contain the total number of times the customer choose option 3. Example: Input: 2 3 0 1 1 1 0 Expected output: 0--> no of times option 1 was choosen 1--> no of times option 2 was choosen 1--> no of times option 3 was choosen 3 0 0 Algorithm: 1. Read the customer...

Problem 12. odd_count.js (forloop is introduced)

Image
  /*Question Given a sequence of integer, count the number of odd number in the sequence. Input: First line contains a positive integer, say n , denoting the length of the sequence. Output: One line containing an integer, denoting the number of odd numbers in the input sequence. Example: Input: 5 8 77 24 22 27 Output: 2 Algorithm: 1. Read the length of the sequence. 2. Read the integer from the sequence. 3. Check whether odd or even, if odd, increment he oddCount variable. 4. Repeat step 3 for every number in the sequence 5. Log the odd count. */ 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 lengthOfSeq = parseInt ( readLine ()); // 5 let sequence = readLine (). split ( ...

Problem 11. ratingcontest.js

Rating Contest A programming competition site regularly holds programming contests at different levels. The first level contest is called ABC, which is open for contestants with ranking less than 1200. The contest level after the ABC is called ARC, which is open for contestants with ranking less than 2800. The contest level after the ARC is called AGC, which is open for all contestants. Help Ramesh in figuring out which is the next level for him given his current rank 'R'. Input One Integer, denoting R. Output Print the name of the next contest rated for Ramesh (ABC, ARC or AGC). Example Input1: 1199 Output1: ABC Explanation1: 1199 is less than 1200, so ABC will be rated. Input2: 1200 Output2: ARC */ 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 (); } //.....................................................