Loading...
More Python Posts
""" Calculator----------------------------------------"""def addition ():print("Addition")n = float(input("Enter the number: "))t = 0 //Total number enterans = 0while n != 0:ans = ans + nt+=1n = float(input("Enter another number (0 to calculate): "))return [ans,t]def subtraction ():print("Subtraction");n = float(input("Enter the number: "))t = 0 //Total number entersum = 0while n != 0:ans = ans - nt+=1n = float(input("Enter another number (0 to calculate): "))return [ans,t]def multiplication ():print("Multiplication")n = float(input("Enter the number: "))t = 0 //Total number enterans = 1while n != 0:ans = ans * nt+=1n = float(input("Enter another number (0 to calculate): "))return [ans,t]def average():an = []an = addition()t = an[1]a = an[0]ans = a / treturn [ans,t]// main...while True:list = []print(" My first python program!")print(" Simple Calculator in python by Malik Umer Farooq")print(" Enter 'a' for addition")print(" Enter 's' for substraction")print(" Enter 'm' for multiplication")print(" Enter 'v' for average")print(" Enter 'q' for quit")c = input(" ")if c != 'q':if c == 'a':list = addition()print("Ans = ", list[0], " total inputs ",list[1])elif c == 's':list = subtraction()print("Ans = ", list[0], " total inputs ",list[1])elif c == 'm':list = multiplication()print("Ans = ", list[0], " total inputs ",list[1])elif c == 'v':list = average()print("Ans = ", list[0], " total inputs ",list[1])else:print ("Sorry, invilid character")else:break
#SetsU = {0,1,2,3,4,5,6,7,8,9}P = {1,2,3,4}Q = {4,5,6}R = {3,4,6,8,9}def set2bits(xs,us) :bs=[]for x in us :if x in xs :bs.append(1)else:bs.append(0)assert len(us) == len(bs)return bsdef union(set1,set2) :finalSet = set()bitList1 = set2bits(set1, U)bitList2 = set2bits(set2, U)for i in range(len(U)) :if(bitList1[i] or bitList2[i]) :finalSet.add(i)return finalSetdef intersection(set1,set2) :finalSet = set()bitList1 = set2bits(set1, U)bitList2 = set2bits(set2, U)for i in range(len(U)) :if(bitList1[i] and bitList2[i]) :finalSet.add(i)return finalSetdef compliment(set1) :finalSet = set()bitList = set2bits(set1, U)for i in range(len(U)) :if(not bitList[i]) :finalSet.add(i)return finalSetdef implication(a,b):return union(compliment(a), b)################################################################################################################# Problems 1-6 ###################################################################################################################################p \/ (q /\ r) = (p \/ q) /\ (p \/ r)def prob1():return union(P, intersection(Q,R)) == intersection(union(P,Q), union(P,R))#p /\ (q \/ r) = (p /\ q) \/ (p /\ r)def prob2():return intersection(P, union(Q,R)) == union(intersection(P,Q), intersection(P,R))#~(p /\ q) = ~p \/ ~qdef prob3():return compliment(intersection(P,R)) == union(compliment(P), compliment(R))#~(p \/ q) = ~p /\ ~qdef prob4():return compliment(union(P,Q)) == intersection(compliment(P), compliment(Q))#(p=>q) = (~q => ~p)def prob5():return implication(P,Q) == implication(compliment(Q), compliment(P))#(p => q) /\ (q => r) => (p => r)def prob6():return implication(intersection(implication(P,Q), implication(Q,R)), implication(P,R))print("Problem 1: ", prob1())print("Problem 2: ", prob2())print("Problem 3: ", prob3())print("Problem 4: ", prob4())print("Problem 5: ", prob5())print("Problem 6: ", prob6())'''Problem 1: TrueProblem 2: TrueProblem 3: TrueProblem 4: TrueProblem 5: TrueProblem 6: {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}'''
# Python binary search functiondef binary_search(arr, target):left = 0right = len(arr) - 1while left <= right:mid = (left + right) // 2if arr[mid] == target:return midelif arr[mid] < target:left = mid + 1else:right = mid - 1return -1# Usagearr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]target = 7result = binary_search(arr, target)if result != -1:print(f"Element is present at index {result}")else:print("Element is not present in array")
def clamp_number(num, a, b):return max(min(num, max(a, b)), min(a, b))clamp_number(2, 3, 5) # 3clamp_number(1, -1, -5) # -1
list_1 = [1,2,3,4,5,6,7,8,9]cubed = map(lambda x: pow(x,3), list_1)print(list(cubed))#Results#[1, 8, 27, 64, 125, 216, 343, 512, 729]
# Function to check Armstrong numberdef is_armstrong_number(number):# Convert number to string to iterate over its digitsnum_str = str(number)# Calculate the sum of the cubes of each digitdigit_sum = sum(int(digit) ** len(num_str) for digit in num_str)# Compare the sum with the original numberif digit_sum == number:return Trueelse:return False# Prompt user for a numbernumber = int(input("Enter a number: "))# Check if the number is an Armstrong numberif is_armstrong_number(number):print(number, "is an Armstrong number.")else:print(number, "is not an Armstrong number.")