CSCE 2100 Question 3

0 likes • Nov 18, 2022 • 3 views
Python

More Python Posts

Shuffle Deck of Cards

0 likes • May 31, 2023 • 1 view
Python
```import random
# Define the ranks, suits, and create a deckranks = ['Ace', '2', '3', '4', '5', '6', '7', '8', '9', '10', 'Jack', 'Queen', 'King']suits = ['Hearts', 'Diamonds', 'Clubs', 'Spades']deck = [(rank, suit) for rank in ranks for suit in suits]
# Shuffle the deckrandom.shuffle(deck)
# Display the shuffled deckfor card in deck:    print(card[0], "of", card[1])```

Python Fibonacci

CHS
0 likes • Sep 6, 2020 • 0 views
Python
`def Fibonacci(n):     if n<0:         print("Incorrect input")     # First Fibonacci number is 0     elif n==1:         return 0    # Second Fibonacci number is 1     elif n==2:         return 1    else:         return Fibonacci(n-1)+Fibonacci(n-2)   # Driver Program   print(Fibonacci(9))`

Find URL in string

0 likes • Nov 19, 2022 • 0 views
Python
```# Python code to find the URL from an input string# Using the regular expressionimport re
def Find(string):
# findall() has been used	# with valid conditions for urls in string	regex = r"(?i)\b((?:https?://|www\d{0,3}[.]|[a-z0-9.\-]+[.][a-z]{2,4}/)(?:[^\s()<>]+|\(([^\s()<>]+|(\([^\s()<>]+\)))*\))+(?:\(([^\s()<>]+|(\([^\s()<>]+\)))*\)|[^\s`!()\[\]{};:'\".,<>?«»“”‘’]))"	url = re.findall(regex,string)		return [x[0] for x in url]	# Driver Codestring = 'My Profile: https://codecatch.net'print("Urls: ", Find(string))```

delay time lambda

0 likes • Nov 19, 2022 • 0 views
Python
```from time import sleep
def delay(fn, ms, *args):  sleep(ms / 1000)  return fn(*args)
delay(lambda x: print(x), 1000, 'later') # prints 'later' after one second```

Compute all the Permutation of a String

0 likes • May 31, 2023 • 0 views
Python
```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 stringstring = input("Enter a string: ")
# Compute permutationspermutations = compute_permutations(string)
# Display the permutationsprint("Permutations:")for permutation in permutations:    print(permutation)```

Multiply Two Matrices

0 likes • May 31, 2023 • 0 views
Python
```# Function to multiply two matricesdef 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 matricesmatrix1 = [[1, 2, 3],           [4, 5, 6],           [7, 8, 9]]
matrix2 = [[10, 11],           [12, 13],           [14, 15]]
# Multiply the matricesresult_matrix = multiply_matrices(matrix1, matrix2)
# Display the resultif result_matrix is not None:    print("Result:")    for row in result_matrix:        print(row)```