Skip to main content

Bubble sort

Nov 19, 2022CodeCatch
Loading...

More Python Posts

Multiply Two Matrices

May 31, 2023CodeCatch

0 likes • 0 views

# 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)

Copy file to destination

Nov 18, 2022AustinLeath

0 likes • 1 view

# importing the modules
import os
import shutil
# getting the current working directory
src_dir = os.getcwd()
# printing current directory
print(src_dir)
# copying the files
shutil.copyfile('test.txt', 'test.txt.copy2') #copy src to dst
# printing the list of new files
print(os.listdir())

convert bytes to a string

Jun 1, 2023CodeCatch

0 likes • 1 view

bytes_data = b'Hello, World!'
string_data = bytes_data.decode('utf-8')
print("String:", string_data)

Calculate Square Root

Nov 18, 2022AustinLeath

0 likes • 0 views

# Python Program to calculate the square root
num = float(input('Enter a number: '))
num_sqrt = num ** 0.5
print('The square root of %0.3f is %0.3f'%(num ,num_sqrt))

Compute all the Permutation of a String

May 31, 2023CodeCatch

0 likes • 0 views

import itertools
def compute_permutations(string):
# Generate all permutations of the string
permutations = itertools.permutations(string)
# Convert each permutation tuple to a string
permutations = [''.join(permutation) for permutation in permutations]
return permutations
# Prompt the user for a string
string = input("Enter a string: ")
# Compute permutations
permutations = compute_permutations(string)
# Display the permutations
print("Permutations:")
for permutation in permutations:
print(permutation)

Print pyramid pattern

Nov 19, 2022CodeCatch

0 likes • 0 views

def print_pyramid_pattern(n):
# outer loop to handle number of rows
# n in this case
for i in range(0, n):
# inner loop to handle number of columns
# values changing acc. to outer loop
for j in range(0, i+1):
# printing stars
print("* ",end="")
# ending line after each row
print("\r")
print_pyramid_pattern(10)