• Nov 19, 2022 •CodeCatch
0 likes • 4 views
# Python program for implementation of Bubble Sort def bubbleSort(arr): n = len(arr) # Traverse through all array elements for i in range(n-1): # range(n) also work but outer loop will repeat one time more than needed. # Last i elements are already in place for j in range(0, n-i-1): # traverse the array from 0 to n-i-1 # Swap if the element found is greater # than the next element if arr[j] > arr[j+1] : arr[j], arr[j+1] = arr[j+1], arr[j] # Driver code to test above arr = [64, 34, 25, 12, 22, 11, 90] bubbleSort(arr) print ("Sorted array is:") for i in range(len(arr)): print ("%d" %arr[i]),
0 likes • 2 views
# Deleting all even numbers from a list a = [1,2,3,4,5] del a[1::2] print(a)
• May 31, 2023 •CodeCatch
0 likes • 1 view
# Function to multiply two matrices def multiply_matrices(matrix1, matrix2): # Check if the matrices can be multiplied if len(matrix1[0]) != len(matrix2): print("Error: The number of columns in the first matrix must be equal to the number of rows in the second matrix.") return None # Create the result matrix filled with zeros result = [[0 for _ in range(len(matrix2[0]))] for _ in range(len(matrix1))] # Perform matrix multiplication for i in range(len(matrix1)): for j in range(len(matrix2[0])): for k in range(len(matrix2)): result[i][j] += matrix1[i][k] * matrix2[k][j] return result # Example matrices matrix1 = [[1, 2, 3], [4, 5, 6], [7, 8, 9]] matrix2 = [[10, 11], [12, 13], [14, 15]] # Multiply the matrices result_matrix = multiply_matrices(matrix1, matrix2) # Display the result if result_matrix is not None: print("Result:") for row in result_matrix: print(row)
• Oct 4, 2023 •AustinLeath
0 likes • 10 views
weigh = lambda a,b: sum(b)-sum(a) FindCoin = lambda A: 0 if (n := len(A)) == 1 else (m := n//3) * (w := 1 + weigh(A[:m], A[2*m:])) + FindCoin(A[m*w:m*(w+1)]) print(FindCoin([1,1,1,1,1,1,1,2,1]))
• Feb 26, 2023 •wabdelh
#84 48 13 20 61 20 33 97 34 45 6 63 71 66 24 57 92 74 6 25 51 86 48 15 64 55 77 30 56 53 37 99 9 59 57 61 30 97 50 63 59 62 39 32 34 4 96 51 8 86 10 62 16 55 81 88 71 25 27 78 79 88 92 50 16 8 67 82 67 37 84 3 33 4 78 98 39 64 98 94 24 82 45 3 53 74 96 9 10 94 13 79 15 27 56 66 32 81 77 # xor a list of integers to find the lonely integer res = a[0] for i in range(1,len(a)): res = res ^ a[i]
""" Calculator ---------------------------------------- """ def addition (): print("Addition") n = float(input("Enter the number: ")) t = 0 //Total number enter ans = 0 while n != 0: ans = ans + n t+=1 n = 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 enter sum = 0 while n != 0: ans = ans - n t+=1 n = 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 enter ans = 1 while n != 0: ans = ans * n t+=1 n = float(input("Enter another number (0 to calculate): ")) return [ans,t] def average(): an = [] an = addition() t = an[1] a = an[0] ans = a / t return [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