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, …
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.
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....
Palindrome Javascript I am trying to write this javascript function to check if a number is...
Palindrome Javascript I am trying to write this javascript function to check if a number is Palindrome.. Palindrome means - a word, phrase, or sequence that reads the same backward as forward, e.g., madam or nurses run. This is the code i have, but it doesnt work. Code: let convertButton = document.getElementsByClassName("btn")[0]; let userInput = document.getElementById("number").value; let results = document.getElementById("result").value; convertButton.addEventListener("click", function (event) { event.preventDefault(); console.log(userInput); if (validatePalidrome(userInput)) document.getElementById("result").innerHTML = "true"; else document.getElementById("result").innerHTML = "false"; }); function validatePalidrome(numbers) { let...
JavaScript Programming Assignment PLEASE NOTE:  You must create and call a main function, and if instructed include...
JavaScript Programming Assignment PLEASE NOTE:  You must create and call a main function, and if instructed include additional functions called by the main. Make sure to use ES6 style of keywords => instead of the older function and for local scope variables use the keyword let and not a keyword var. Make sure to follow the requirements and recheck before submitting. PROJECT GOAL: Assume that hot dogs come in packages of 10, and hot dog buns come in packages of 8....
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT