Question

In: Computer Science

Split the Number IN JAVASCRIPT Programming challenge description: You are given a number N and a...

Split the Number

IN JAVASCRIPT

Programming challenge description:

You are given a number N and a pattern. The pattern consists of lowercase latin letters and one operation "+" or "-". The challenge is to split the number and evaluate it according to this pattern e.g.
1232 ab+cd -> a:1, b:2, c:3, d:2 -> 12+32 -> 44

Input:

Your program should read lines from standard input. Each line contains the number and the pattern separated by a single whitespace. The number will be in range [100, 1000000000]. All test cases contain valid expressions (no leading zeros).

Output:

Print out the result of the pattern evaluation.

Test 1

Test InputDownload Test 1 Input

3413289830 a-bcdefghij

Expected OutputDownload Test 1 Input

-413289827

Test 2

Test InputDownload Test 2 Input

776 a+bc

Expected OutputDownload Test 2 Input

83

Solutions

Expert Solution

Solution:

//importing the readline module to allow taking standart input
const readline = require ('readline').createInterface ({
  input: process.stdin,
  output: process.stdout,
});
//prompting user to enter the requrired input
readline.question ('Enter the number and pattern: ',
//the entered value is stored in 'input' variable.
input => {
  //Step 1. Split the input by specifying whitespace character as the delimeter
  var splittedInput = input.split (' ');
  //the number is in the 0th index of splitted string
  var number = splittedInput[0];
  //the pattern is on the 1st index of splitted string
  var pattern = splittedInput[1];
  //decalring the digits array to store the digits of number
  var digits = [];
  //while loop to extract the digits and add them in the digits array
  while (number != 0) {
    digit = number % 10;
    digits.push (digit);
    //Math.floor is used to make sure that number remains in integer form
    number = Math.floor (number / 10);
  }
  //reversing the digits array to get the actual order of digits.
  digits.reverse ();
  //declaring a variable to store final expression
  var expression = '';
  //variable to count the number of digits traversed
  var count = 0;
  //for loop to traverse the expression received from user input
  //and set the final expression
  for (let i = 0; i < pattern.length; i++) {
    //if the element is + or - , add them to expression
    if (pattern[i] == '+' || pattern[i] == '-') expression += pattern[i];
    //else add the digit to the expression and increase the count variable
    else {
      expression += digits[count];
      count++;
    }
  }
  //evaluate the final expression using the inbuilt eval() function
  var result = eval (expression);
  // print the final result
  console.log ("Result is: "+result);
  //close the input stream
  readline.close();
});

Screenshot of code for better understanding:

Sample Output:

After reading the input from standard input, we split it using the string.split() function to extract the number and the pattern. Then we extract the individual digits of number in array. After this, we loop through the expression to make our final expression replacing letter with the digits and then evaluate it using the eval function of javascript.


Related Solutions

Word to Digit Programming challenge description: Given a string representation of a set of numbers, print...
Word to Digit Programming challenge description: Given a string representation of a set of numbers, print the digit representation of the numbers. Input: Your program should read lines from standard input. Each line contains a list of word representations of numbers separated by a semicolon. There are up to 20 numbers in one line. The numbers are "zero" through "nine". Output: Print the sequence of digits. Test 1 Input zero;two;five;seven;eight;four Expected Test 1 output 025784 Test 2 Input three;seven;eight;nine;two Expected...
Challenge: Number Stats 2 Description: Extend the program you wrote for Number Stats to determine the...
Challenge: Number Stats 2 Description: Extend the program you wrote for Number Stats to determine the median and mode of the numbers read from the file. Purpose: The purpose of this challenge is to provide experience working with numerical data in a file and generating summary information including the determination of median and mode. It also provides experience adding new functionality to an existing program. Requirements: Extend the program you wrote for Number Stats to determine the median and mode...
Short description of your assigned challenge Describe the innovation you researched to address the challenge The...
Short description of your assigned challenge Describe the innovation you researched to address the challenge The result (what did it prove?) How/Why it impacts or supports the challenge the challenge is "make Solar energy economical"
Code in C-language programming description about convert binary number to decimal number.
Code in C-language programming description about convert binary number to decimal number.
Solve for number 2, Use Functional Programming ONLY, so No For loops Use JavaScript: Start with...
Solve for number 2, Use Functional Programming ONLY, so No For loops Use JavaScript: Start with an array called inputtable. The array should have numbers between 1 and 10. NOTE: Do NOT use a form of a ‘for’ loop anywhere, including iterators. This is meant to be a functional exercise. Get the odd multiples of 5 between 1 and 100. 5, 15, …
Cpp challenge Description The purpose of this challenge is to use the WHILE loop to control...
Cpp challenge Description The purpose of this challenge is to use the WHILE loop to control program flow. This challenge will use the WHILE loop in various ways. Requirements Write all your code for the following steps in one file. Ask the user to enter a single string. Ask the user to enter an integer value for a variable called repetitions. Use a while loop to display this string (of your choice) repeated repetitions times, one string per line Use...
Using HTML and JavaScript, receive a positive number, n, and output all prime numbers that are...
Using HTML and JavaScript, receive a positive number, n, and output all prime numbers that are smaller than n and have a digit 7. For example, if n is 100, the program should output 7, 17, 37, 47, 67, 71, 73, 79, and 97.
Cpp challenge Description The purpose of this challenge is to use various flow control structures. This...
Cpp challenge Description The purpose of this challenge is to use various flow control structures. This challenge uses techniques for displaying formatted output, simulating a payment structure for paying off outstanding debt such as a credit card or any kind of interest-bearing loan. Requirements Declare double variables called balance, monthly, apr, apr_monthly, start_bal, interest, end_bal Declare an integer called months Prompt the user to enter values for balance, monthly, and apr Use a while loop to display the payment breakdown...
In Programming Challenge 12 of Chapter 3, you were asked to write a program that converts...
In Programming Challenge 12 of Chapter 3, you were asked to write a program that converts a Celsius temperature to Fahrenheit. Modify that program so it uses a loop to display a table of the Celsius temperatures 0–20, and their Fahrenheit equivalents. Display table on screen and it a .txt file. c++
In this programming challenge you are to create two Python programs: randomwrite.py and randomread.py. One program,...
In this programming challenge you are to create two Python programs: randomwrite.py and randomread.py. One program, randomwrite.py, is to write a set of random numbers to a file. The second program, randomread.py, is to read a set of random numbers from a file, counts how many were read, displays the random numbers, and displays the total count of random numbers. Random Number File Writer (randomwrite.py) Create a program called randomwrite.py that writes a series of random integers to a file....
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT