• Nov 19, 2022 •CodeCatch
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]),
• May 31, 2023 •CodeCatch
0 likes • 5 views
my_list = [1, 2, 3, 4, 5] removed_element = my_list.pop(2) # Remove and return element at index 2 print(removed_element) # 3 print(my_list) # [1, 2, 4, 5] last_element = my_list.pop() # Remove and return the last element print(last_element) # 5 print(my_list) # [1, 2, 4]
• Nov 18, 2022 •AustinLeath
import itertools import string import time def guess_password(real): chars = string.ascii_lowercase + string.ascii_uppercase + string.digits + string.punctuation attempts = 0 for password_length in range(1, 9): for guess in itertools.product(chars, repeat=password_length): startTime = time.time() attempts += 1 guess = ''.join(guess) if guess == real: return 'password is {}. found in {} guesses.'.format(guess, attempts) loopTime = (time.time() - startTime); print(guess, attempts, loopTime) print("\nIt will take A REALLY LONG TIME to crack a long password. Try this out with a 3 or 4 letter password and see how this program works.\n") val = input("Enter a password you want to crack that is 9 characters or below: ") print(guess_password(val.lower()))
• Sep 14, 2024 •rgannedo-6205
0 likes • 4 views
# Python binary search function def binary_search(arr, target): left = 0 right = len(arr) - 1 while left <= right: mid = (left + right) // 2 if arr[mid] == target: return mid elif arr[mid] < target: left = mid + 1 else: right = mid - 1 return -1 # Usage arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] target = 7 result = binary_search(arr, target) if result != -1: print(f"Element is present at index {result}") else: print("Element is not present in array")
• Oct 15, 2022 •CodeCatch
0 likes • 1 view
class Solution(object): def floodFill(self, image, sr, sc, newColor): R, C = len(image), len(image[0]) color = image[sr][sc] if color == newColor: return image def dfs(r, c): if image[r][c] == color: image[r][c] = newColor if r >= 1: dfs(r-1, c) if r+1 < R: dfs(r+1, c) if c >= 1: dfs(r, c-1) if c+1 < C: dfs(r, c+1) dfs(sr, sc) return image
0 likes • 2 views
from collections import defaultdict def collect_dictionary(obj): inv_obj = defaultdict(list) for key, value in obj.items(): inv_obj[value].append(key) return dict(inv_obj) ages = { 'Peter': 10, 'Isabel': 10, 'Anna': 9, } collect_dictionary(ages) # { 10: ['Peter', 'Isabel'], 9: ['Anna'] }