Skip to main content

integer to roman numeral

Nov 19, 2022CodeCatch
Loading...

More Python Posts

Binary search

Sep 22, 2023AustinLeath

0 likes • 13 views

# Python binary search function
def binary_search(arr, target):
left = 0
right = len(arr) - 1
while left <= right:
mid = (left + right) // 2
if arr[mid] == target:
return mid
elif arr[mid] < target:
left = mid + 1
else:
right = mid - 1
return -1
# Usage
arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
target = 7
result = binary_search(arr, target)
if result != -1:
print(f"Element is present at index {result}")
else:
print("Element is not present in array")

delay time lambda

Nov 19, 2022CodeCatch

0 likes • 0 views

from time import sleep
def delay(fn, ms, *args):
sleep(ms / 1000)
return fn(*args)
delay(lambda x: print(x), 1000, 'later') # prints 'later' after one second

convert bytes to a string

Jun 1, 2023CodeCatch

0 likes • 1 view

bytes_data = b'Hello, World!'
string_data = bytes_data.decode('utf-8')
print("String:", string_data)

Palindrome checker

Nov 19, 2022CodeCatch

0 likes • 3 views

# 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")

Fibonacci Series

Nov 18, 2022AustinLeath

0 likes • 5 views

#Python 3: Fibonacci series up to n
def fib(n):
a, b = 0, 1
while a < n:
print(a, end=' ')
a, b = b, a+b
print()
fib(1000)

return multiple values from a function

Jun 1, 2023CodeCatch

0 likes • 0 views

def calculate_values():
value1 = 10
value2 = 20
return value1, value2
result1, result2 = calculate_values()
print("Result 1:", result1)
print("Result 2:", result2)