diff --git a/calculator.py b/calculator.py index 6f0f018c..0d773866 100644 --- a/calculator.py +++ b/calculator.py @@ -1,59 +1,52 @@ -num1=int(input("eneter a digit")) -num2=int(input("eneter a another digit")) -# defination for operators - -#addition -def add(num1, num2): - return num1+num2 -#substraction -def subtract(num1, num2): - return num1-num2 -#multiply -def multiply(num1, num2): - return num1*num2 -#division -def divide(num1, num2): - return num1/num2 - -#command for operation -print("choose operation") -print("press 1 for add") -print("press 2 for subs") -print("press 3 for multiply") -print("press 4 for devision") - - - - - -while True: - # take input from the user - choice = input("Enter choice(1/2/3/4): ") - - if choice in ('1', '2', '3', '4'): - - if choice == '1': - print(num1, "+", num2, "=", add(num1, num2)) - - - - elif choice == '2': - print(num1, "-", num2, "=", subtract(num1, num2)) - - elif choice == '3': - print(num1, "*", num2, "=", multiply(num1, num2)) - - - - - - elif choice == '4': - print(num1, "/", num2, "=", divide(num1, num2)) - # check if user wants another calculation - # break the while loop if answer is no - next_calculation = input("Let's do next calculation? (yes/no): ") - if next_calculation == "no": - break - - else: - print("Invalid Input") +# This function adds two numbers +def add(x, y): + return x + y + +# This function subtracts two numbers +def subtract(x, y): + return x - y + +# This function multiplies two numbers +def multiply(x, y): + return x * y + +# This function divides two numbers +def divide(x, y): + return x / y + + +print("Select operation.") +print("1.Add") +print("2.Subtract") +print("3.Multiply") +print("4.Divide") + +while True: + # take input from the user + choice = input("Enter choice(1/2/3/4): ") + + # check if choice is one of the four options + if choice in ('1', '2', '3', '4'): + num1 = float(input("Enter first number: ")) + num2 = float(input("Enter second number: ")) + + if choice == '1': + print(num1, "+", num2, "=", add(num1, num2)) + + elif choice == '2': + print(num1, "-", num2, "=", subtract(num1, num2)) + + elif choice == '3': + print(num1, "*", num2, "=", multiply(num1, num2)) + + elif choice == '4': + print(num1, "/", num2, "=", divide(num1, num2)) + + # check if user wants another calculation + # break the while loop if answer is no + next_calculation = input("Let's do next calculation? (y/n): ") + if next_calculation == "n": + break + + else: + print("Invalid Input") \ No newline at end of file diff --git a/longestcommonprefix.py b/longestcommonprefix.py new file mode 100644 index 00000000..cff15269 --- /dev/null +++ b/longestcommonprefix.py @@ -0,0 +1,33 @@ +def longestCommonPrefix(a): + + size = len(a) + + # if size is 0, return empty string + if (size == 0): + return "" + + if (size == 1): + return a[0] + + # sort the given strings + a.sort() + + # find the minimum length string + end = min(len(a[0]), len(a[size - 1])) + + # find the common prefix + i = 0 + while (i < end and a[0][i] == a[size - 1][i]): + i += 1 + + ans = a[0][0: i] + return ans + +inputs = [] + +n = int(input("Enter the number of strings : ")) + +for i in range(n): + inputs.append(input("Enter the string : ")) + +print("The longest Common Prefix is :" , longestCommonPrefix(inputs)) \ No newline at end of file