• Nov 18, 2022 •AustinLeath
0 likes • 4 views
mydict = {'carl':40, 'alan':2, 'bob':1, 'danny':0} # How to sort a dict by value Python 3> sort = {key:value for key, value in sorted(mydict.items(), key=lambda kv: (kv[1], kv[0]))} print(sort) # How to sort a dict by key Python 3> sort = {key:mydict[key] for key in sorted(mydict.keys())} print(sort)
• Nov 19, 2022 •CodeCatch
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)
• Sep 6, 2020 •C S
0 likes • 2 views
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))
• May 31, 2023 •CodeCatch
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)
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 • 11 views
# question3.py from itertools import product V='∀' E='∃' def tt(f,n) : xss=product((0,1),repeat=n) print('function:',f.__name__) for xs in xss : print(*xs,':',int(f(*xs))) print('') # this is the logic for part A (p\/q\/r) /\ (p\/q\/~r) /\ (p\/~q\/r) /\ (p\/~q\/~r) /\ (~p\/q\/r) /\ (~p\/q\/~r) /\ (~p\/~q\/r) /\ (~p\/~q\/~r) def parta(p,q,r) : a=(p or q or r) and (p or q or not r) and (p or not q or r)and (p or not q or not r) b=(not p or q or r ) and (not p or q or not r) and (not p or not q or r) and (not p or not q or not r) c= a and b return c def partb(p,q,r) : a=(p or q and r) and (p or not q or not r) and (p or not q or not r)and (p or q or not r) b=(not p or q or r ) and (not p or q or not r) and (not p or not q or r) and (not p or not q or not r) c= a and b return c print("part A:") tt(parta,3) print("part B:") tt(partb,3)