Skip to content
Snippets Groups Projects
Commit daf96e3f authored by Lowri Williams's avatar Lowri Williams
Browse files

Add new file

parent ebc5a7d3
No related branches found
No related tags found
No related merge requests found
# sample_code.py
def add(a, b):
"""Add two numbers."""
return a + b
def subtract(a, b):
"""Subtract the second number from the first."""
return a - b
def multiply(a, b):
"""Multiply two numbers."""
return a * b
def divide(a, b):
"""Divide the first number by the second, handling division by zero."""
if b == 0:
return "Cannot divide by zero"
return a / b
def power(base, exponent):
"""Raise a base number to an exponent."""
return base ** exponent
def is_palindrome(s):
"""Check if a string is a palindrome (ignores case and spaces)."""
s = s.replace(" ", "").lower()
return s == s[::-1]
def count_vowels(s):
"""Count the number of vowels in a string."""
vowels = "aeiouAEIOU"
return sum(1 for char in s if char in vowels)
def factorial(n):
"""Calculate the factorial of a non-negative integer."""
if n < 0:
return "Invalid input - must be a non-negative integer"
elif n == 0 or n == 1:
return 1
else:
result = 1
for i in range(2, n + 1):
result *= i
return result
def fibonacci(n):
"""Generate the nth Fibonacci number."""
if n < 0:
return "Invalid input - must be a non-negative integer"
elif n == 0:
return 0
elif n == 1:
return 1
else:
a, b = 0, 1
for _ in range(2, n + 1):
a, b = b, a + b
return b
def is_prime(n):
"""Check if a number is a prime number."""
if n <= 1:
return False
for i in range(2, int(n ** 0.5) + 1):
if n % i == 0:
return False
return True
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment