veda-programming / programming.txt
vedaco's picture
Create programming.txt
4b2c97c verified
def hello_world():
print("Hello, World!")
return True
def fibonacci(n):
if n <= 1:
return n
return fibonacci(n-1) + fibonacci(n-2)
def factorial(n):
if n == 0:
return 1
return n * factorial(n-1)
class Calculator:
def __init__(self):
self.result = 0
def add(self, a, b):
return a + b
def subtract(self, a, b):
return a - b
def bubble_sort(arr):
n = len(arr)
for i in range(n):
for j in range(0, n-i-1):
if arr[j] > arr[j+1]:
arr[j], arr[j+1] = arr[j+1], arr[j]
return arr
def binary_search(arr, target):
left, right = 0, 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
def is_palindrome(s):
s = s.lower()
return s == s[::-1]
def sum_list(numbers):
total = 0
for num in numbers:
total += num
return total