Skip to main content

Propositional logic with itertools

Nov 18, 2022AustinLeath
Loading...

More Python Posts

return maximum

Nov 19, 2022CodeCatch

0 likes • 0 views

def max_n(lst, n = 1):
return sorted(lst, reverse = True)[:n]
max_n([1, 2, 3]) # [3]
max_n([1, 2, 3], 2) # [3, 2]

when predicate lambda

Nov 19, 2022CodeCatch

0 likes • 0 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

key of minimum value

Nov 19, 2022CodeCatch

0 likes • 0 views

def key_of_min(d):
return min(d, key = d.get)
key_of_min({'a':4, 'b':0, 'c':13}) # b

Check Armstrong Number

May 31, 2023CodeCatch

0 likes • 0 views

# Function to check Armstrong number
def is_armstrong_number(number):
# Convert number to string to iterate over its digits
num_str = str(number)
# Calculate the sum of the cubes of each digit
digit_sum = sum(int(digit) ** len(num_str) for digit in num_str)
# Compare the sum with the original number
if digit_sum == number:
return True
else:
return False
# Prompt user for a number
number = int(input("Enter a number: "))
# Check if the number is an Armstrong number
if is_armstrong_number(number):
print(number, "is an Armstrong number.")
else:
print(number, "is not an Armstrong number.")

LeetCode Flood Fill

Oct 15, 2022CodeCatch

0 likes • 0 views

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

Remove i'th character

Nov 19, 2022CodeCatch

0 likes • 0 views

# Python code to demonstrate
# method to remove i'th character
# Naive Method
# Initializing String
test_str = "CodeCatch"
# Printing original string
print ("The original string is : " + test_str)
# Removing char at pos 3
# using loop
new_str = ""
for i in range(len(test_str)):
if i != 2:
new_str = new_str + test_str[i]
# Printing string after removal
print ("The string after removal of i'th character : " + new_str)