• Nov 19, 2022 •CodeCatch
0 likes • 4 views
from collections import Counter def find_parity_outliers(nums): return [ x for x in nums if x % 2 != Counter([n % 2 for n in nums]).most_common()[0][0] ] find_parity_outliers([1, 2, 3, 4, 6]) # [1, 3]
• Oct 15, 2022 •CodeCatch
1 like • 2 views
my_list = ["blue", "red", "green"] #1- Using sort or srted directly or with specifc keys my_list.sort() #sorts alphabetically or in an ascending order for numeric data my_list = sorted(my_list, key=len) #sorts the list based on the length of the strings from shortest to longest. # You can use reverse=True to flip the order #2- Using locale and functools import locale from functools import cmp_to_key my_list = sorted(my_list, key=cmp_to_key(locale.strcoll))
0 likes • 0 views
def check_prop(fn, prop): return lambda obj: fn(obj[prop]) check_age = check_prop(lambda x: x >= 18, 'age') user = {'name': 'Mark', 'age': 18} check_age(user) # True
1 like • 3 views
def hex_to_rgb(hex): return tuple(int(hex[i:i+2], 16) for i in (0, 2, 4)) hex_to_rgb('FFA501') # (255, 165, 1)
• May 31, 2023 •CodeCatch
0 likes • 2 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)
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