• Nov 19, 2022 •CodeCatch
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))
• May 31, 2023 •CodeCatch
0 likes • 0 views
class Rectangle: pass class Square(Rectangle): pass rectangle = Rectangle() square = Square() print(isinstance(rectangle, Rectangle)) # True print(isinstance(square, Rectangle)) # True print(isinstance(square, Square)) # True print(isinstance(rectangle, Square)) # False
# Prompt user for base and height base = float(input("Enter the base of the triangle: ")) height = float(input("Enter the height of the triangle: ")) # Calculate the area area = (base * height) / 2 # Display the result print("The area of the triangle is:", area)
# function which return reverse of a string def isPalindrome(s): return s == s[::-1] # Driver code s = "malayalam" ans = isPalindrome(s) if ans: print("Yes") else: print("No")
0 likes • 1 view
from functools import partial def curry(fn, *args): return partial(fn, *args) add = lambda x, y: x + y add10 = curry(add, 10) add10(20) # 30