• Nov 19, 2022 •CodeCatch
0 likes • 3 views
# Input for row and column R = int(input()) C = int(input()) # Using list comprehension for input matrix = [[int(input()) for x in range (C)] for y in range(R)]
0 likes • 0 views
# Python program for implementation of Selection # Sort import sys A = [64, 25, 12, 22, 11] # Traverse through all array elements for i in range(len(A)): # Find the minimum element in remaining # unsorted array min_idx = i for j in range(i+1, len(A)): if A[min_idx] > A[j]: min_idx = j # Swap the found minimum element with # the first element A[i], A[min_idx] = A[min_idx], A[i] # Driver code to test above print ("Sorted array") for i in range(len(A)): print("%d" %A[i]),
• Nov 18, 2022 •AustinLeath
0 likes • 5 views
mydict = {'carl':40, 'alan':2, 'bob':1, 'danny':0} # How to sort a dict by value Python 3> sort = {key:value for key, value in sorted(mydict.items(), key=lambda kv: (kv[1], kv[0]))} print(sort) # How to sort a dict by key Python 3> sort = {key:mydict[key] for key in sorted(mydict.keys())} print(sort)
0 likes • 1 view
# Python code to demonstrate # method to remove i'th character # Naive Method # Initializing String test_str = "CodeCatch" # Printing original string print ("The original string is : " + test_str) # Removing char at pos 3 # using loop new_str = "" for i in range(len(test_str)): if i != 2: new_str = new_str + test_str[i] # Printing string after removal print ("The string after removal of i'th character : " + new_str)
• May 31, 2023 •CodeCatch
def generate_pascals_triangle(num_rows): triangle = [] for row in range(num_rows): # Initialize the row with 1 current_row = [1] # Calculate the values for the current row if row > 0: previous_row = triangle[row - 1] for i in range(len(previous_row) - 1): current_row.append(previous_row[i] + previous_row[i + 1]) # Append 1 at the end of the row current_row.append(1) # Add the current row to the triangle triangle.append(current_row) return triangle def display_pascals_triangle(triangle): for row in triangle: for number in row: print(number, end=" ") print() # Prompt the user for the number of rows num_rows = int(input("Enter the number of rows for Pascal's Triangle: ")) # Generate Pascal's Triangle pascals_triangle = generate_pascals_triangle(num_rows) # Display Pascal's Triangle display_pascals_triangle(pascals_triangle)
• Sep 3, 2025 •AustinLeath
import subprocess class CommandRunner: def run_command(self, command): command_process = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE, text=True) output = command_process.communicate()[0].strip() return_code = command_process.returncode return output, return_code def main(): # Create instance of CommandRunner runner = CommandRunner() # Define the command command = 'ping -c 4 localhost' try: # Run the command and get output and return code output, return_code = runner.run_command(command) # Print the output and return code print(f"Command output:\n{output}") print(f"Return code: {return_code}") except Exception as e: print(f"An error occurred: {e}") if __name__ == "__main__": main()