Here is the completed code for this problem. Comments are included, go through it, learn how things work and let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please rate the answer. Thanks
//required method
function balance(expression){
//a string variable to store balanced expression
var updated="";
//number of opened parentheses
var opened=0;
//looping through the expression
for (var i = 0; i < expression.length; i++) {
//getting current character
var c=expression.charAt(i);
//checking if it is an opening parentheses
if(c=='('){
//incrementing opened
opened+=1;
//adding c to updated
updated+=c;
}else if(c==')'){
//closing parentheses found, checking if there is an
//opening parentheses before
if(opened>0){
//only appending c to updated if there is an open parentheses before
updated+=c;
//decrementing number of opened parentheses
opened-=1;
}
}else{
//any other character, simply appending
updated+=c;
}
}
//now if there is any unclosed parentheses, closing all
//or in other words, appending 'opened' number of parentheses
while(opened>0){
//appending a ')'
updated+=")";
opened-=1;
}
return updated;
}
write an algorithm function called balance() in javascript that takes in a string and returns a...
Write a function called most_consonants(words) that takes a list of strings called words and returns the string in the list with the most consonants (i.e., the most letters that are not vowels). You may assume that the strings only contain lowercase letters. For example: >>> most_consonants(['python', 'is', 'such', 'fun']) result: 'python' >>> most_consonants(['oooooooh', 'i', 'see', 'now']) result: 'now' The function that you write must use a helper function (either the num_vowels function from lecture, or a similar function that you...
Python 3: Write a function called every_other_character() that takes in a string and returns a new string with every other character removed. That is, the new string will include the first, third, fifth, etc. letters of the given string. Must use a while or for loop in the solution.
Write a Javascript function that accepts a string. The function takes the last digit of the ASCII code and returns a string of digits sorted in ascending order. (without using push, sort or other inbuilt function)
project-8a Write a function named count_letters that takes as a parameter a string and returns a dictionary that tabulates how many of each letter is in that string. The string can contain characters other than letters, but only the letters should be counted. The string could even be the empty string. Lower-case and upper-case versions of a letter should be part of the same count. The keys of the dictionary should be the upper-case letters. If a letter does not...
Write a function called smallestLetter that takes in a string argument and returns a char. The char that is returned by this function is the character in the string with the lowest ASCII integer code. For example: smallestLetter("Hello") returns ‘H’ which is code 72, and smallestLetter("Hello World") returns ‘ ’ (The space character) which is code 32
Language: Python Function name : findwaldo Parameters : string Returns: int Description: Write a recursive function that takes in a string containing some combination of letters, numbers, and spaces, and return the starting index of the first instance of “waldo” contained in that string. If the string does not contain “waldo”, return -1. This function IS case sensitive, so “waldo” is not the same as “WALDO”. Code using string functions such as .index() and .find() that oversimplify the problem will...
Write the method: public static String doubleVowels(String x). This method takes a String x and returns a new String y which is identical to x except wherever there is a vowel in x, there will be two of that same vowel in the returned String y. The original String x will contain only lower case letters. For example, doubleVowels("easy") should return the String "eeaasy". Another example: doubleVowels("abootstrap") should return the String "aabooootstraap". Another example: doubleVowels("gggrrrhh") should return the String "gggrrrhh"....
package week_3; /** Write a method called countUppercase that takes a String array argument. You can assume that every element in the array is a one-letter String, for example String[] test = { "a", "B", "c", "D", "e"}; This method will count the number of uppercase letters from the set A through Z in the array, and return that number. So for the example array above, your method will return 2. You will need to use some Java library methods....
4) Write a function named remove that returns void, takes in a string& called word, and an int called i that, starting from index 0, removes every ith letter from word by modifying it in place. Include a functional main method to make sure the program works. Programming language is C++, thanks in advance
Write a JavaScript function called getHypotenuse that returns the length of the hypotenuse of a right triangle given the length of the other two sides. You can utilize the Pythagorean Theorem. Test your function with the following data. getHypotenuse(3, 4) should return 5 getHypotenuse(6, 8) should return 10 Rewrite the above function as an arrow function.