Question

In: Computer Science

write an algorithm function called balance() in javascript that takes in a string and returns a...

write an algorithm function called balance() in javascript that takes in a string and returns a strinf with balanced parantheses only. the string should be able to contain only parantheses, numbers, and letters. include comments of each portion of your code

balance(“()()”) should return “()()”
balance(“())”) should return “()”
balance(“a(b)c”) should return “a(b)c”
balance(“a(b)c())”) should return “a(b)c()”


Solutions

Expert Solution

function isValidString(s) {
var count = 0;
for (let c of s) { // check for equal number of brackets
if (c === '(') {
count++;
}
else if (c === ')') {
count--;
}
if (count < 0) return false;
}
return count === 0; // return true if no extra brackets
}

function balance(s) {
let levels = [s]; // create array
while (true) { // run till condition is true
let valid = levels.filter(isValidString) ; // return string according to given condition
if (valid.length > 0) { // check length must be greater than 0
return new Set(valid);
}

// check validity of all possible substrings with one character removed.
let nextlevels = [];
for (let s of levels) {
for (let i = 0; i < s.length; i++) {
nextlevels.push(s.substring(0, i) + s.substring(i + 1));
}
}
levels = nextlevels;
}
}
console.log(balance('()()'));
console.log(balance('())'));
console.log(balance('a(b)c'));
console.log(balance('a(b)c())'));


Related Solutions

JavaScript Write a function called "first" that takes in two arguments - the first is an...
JavaScript Write a function called "first" that takes in two arguments - the first is an argument called arr that is an array of numbers, the second is an optional number argument called num(hint: you'll need a default value - look back at the slides). If "num" was not passed in (since it's optional), the "first" function will return an array containing the first item of the array. If a value for "num" was given, the "first" function will return...
Create a function called time2Greeting. It takes a time (in military time) and returns a string...
Create a function called time2Greeting. It takes a time (in military time) and returns a string with the right greeting. Good Morning 4AM to before noon. Good Afternoon Noon to before 5PM Good Evening from 5PM to 11PM What are you doing up at this hour? between 11 and 4AM For illegal values, say: That is not a valid time. Example: What is your name?   John What time is it? 1315 Good afternoon, John. C++ programming
Write a function (in Javascript) called longestMorseCodeWords which takes in an array of strings. The output...
Write a function (in Javascript) called longestMorseCodeWords which takes in an array of strings. The output of longestMorseCodeWords should be an array of the strings that were passed in, but ordered by the length of their Morse Code equivalent in descending order. If the length of Morse Code is equal, order the words alphabetically.For convenience, the full table for the 26 letters of the English alphabet is given below: [".-","-...","-.-.","-..",".","..-.","--.","....","..",".---","-.-",".-..","--","-.","---",".--.","--.-",".-.","...","-","..-","...-",".--","-..-","-.--","--.."] let words = ["gin", "zen", "gig", "msg"] longestMorseCodeWords(words) The Morse...
Write a function named "characters" that takes a string as a parameter and returns the number...
Write a function named "characters" that takes a string as a parameter and returns the number of characters in the input string
Write a function that takes a string, returns void and has the effect of shifting the...
Write a function that takes a string, returns void and has the effect of shifting the vowels of the string to the left. For example, if the input string was “hello class”, then after calling this function the string should contain “holla cless” (note that the first vowel is moved to the location of the last vowel). You can use an auxillary string if you want, but a nicer solution would work “in-place”
Write a function named "replacement" that takes a string as a parameter and returns an identical...
Write a function named "replacement" that takes a string as a parameter and returns an identical string except with every instance of the character "w" replaced with the character "v" My code: function replacement(word){ var str=word; var n=str.replace("w","v"); return n; } Syntax Error: function replacement incorrect on input Not sure how to fix? Can't use a loop for answer
C programming Write a function called string in() that takes two string pointers as arguments. If...
C programming Write a function called string in() that takes two string pointers as arguments. If the second string is contained in the first string, have the function return the address at which the contained string begins. For instance, string in(“hats”, “at”) would return the address of the a in hats. Otherwise, have the function return the null pointer. Test the function in a complete program that uses a loop to provide input values for feeding to the function.
In C++ Write a function called findBestSimScore that takes a genome and a sequence and returns...
In C++ Write a function called findBestSimScore that takes a genome and a sequence and returns the highest similarity score found in the genome as a double. Note: the term genome refers to the string that represents the complete set of genes in an organism, and sequence to refer to some substring or sub-sequence in the genome. Your function MUST be named findBestSimScore Your function should take two parameters in this order: a string parameter for the genome (complete set...
Use Python Write a function that takes a mobile phone number as a string and returns...
Use Python Write a function that takes a mobile phone number as a string and returns a Boolean value to indicate if it is a valid number or not according to the following rules of a provider: * all numbers must be 9 or 10 digits in length; * all numbers must contain at least 4 different digits; * the sum of all the digits must be equal to the last two digits of the number. For example '045502226' is...
JAVA Arrays 4 Write a method called isPalindrome that takes a String as input and returns...
JAVA Arrays 4 Write a method called isPalindrome that takes a String as input and returns true if the String is a palindrome.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT