• 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]
• Aug 12, 2024 •AustinLeath
0 likes • 5 views
magnitude = lambda bits: 1_000_000_000_000_000_000 % (2 ** bits) sign = lambda bits: -1 ** (1_000_000_000_000_000_000 // (2 ** bits)) print("64 bit sum:", magnitude(64) * sign(64)) print("32 bit sum:", magnitude(32) * sign(32)) print("16 bit sum:", magnitude(16) * sign(16))
• Nov 18, 2022 •AustinLeath
# 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))
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
0 likes • 6 views
def when(predicate, when_true): return lambda x: when_true(x) if predicate(x) else x double_even_numbers = when(lambda x: x % 2 == 0, lambda x : x * 2) print(double_even_numbers(2)) # 4 print(double_even_numbers(1)) # 1
• 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))