Computer Science
Write a program to input a formula with some brackets and checks, and prints out if the formula has the same number of opening and closing parentheses.
Answer
str = input("Enter a formula: ")
count = 0
for ch in str :
if ch == '(' :
count += 1
elif ch == ')' :
count -= 1
if count == 0 :
print("Formula has same number of opening and closing parentheses")
else :
print("Formula has unequal number of opening and closing parentheses")
Output
Enter a formula: s(s-a)(s-b)(s-c)
Formula has same number of opening and closing parentheses
=====================================
Enter a formula: s((s-a)(s-b)(s-c)
Formula has unequal number of opening and closing parentheses
Related Questions
Write a program to convert a given number into equivalent Roman number (store its value as a string). You can use following guidelines to develop solution for it:
- From the given number, pick successive digits, using %10 and /10 to gather the digits from right to left.
- The rules for Roman Numerals involve using four pairs of symbols for ones and five, tens and fifties, hundreds and five hundreds. An additional symbol for thousands covers all the relevant bases.
- When a number is followed by the same or smaller number, it means addition. "II" is two 1's = 2. "VI" is 5 + 1 = 6.
- When one number is followed by a larger number, it means subtraction. "IX" is 1 before 10 = 9. "IIX isn't allowed, this would be "VIII". For numbers from 1 to 9, the symbols are "I" and "V", and the coding works like this. "I" , "II", "III", "IV", "V", "VI", "VII", "VIII", "IX".
- The same rules work for numbers from 10 to 90, using "X" and "L". For numbers from 100 to 900, using the symbols "C" and "D". For numbers between 1000 and 4000, using "M".
Here are some examples. 1994 = MCMXCIV, 1956 = MCMLVI, 3888= MMMDCCCLXXXVIII
Write a program that asks the user for a string (only single space between words) and returns an estimate of how many words are in the string. (Hint. Count number of spaces)
Write a program that inputs a line of text and prints out the count of vowels in it.
Write a program to input a line of text and print the biggest word (length wise) from it.