def fibonacci(n):
if n <= 1:
return n
else:
return fibonacci(n-1) + fibonacci(n-2)
def find_max(lst):
if not lst:
return None
max_value = lst[0]
for num in lst:
if num > max_value:
max_value = num
return max_value
def reverse_string(s):
return s[::-1]
def count_characters(s):
count = {}
for char in s:
count[char] = count.get(char, 0) + 1
return count
def common_elements(lst1, lst2):
return list(set(lst1) & set(lst2))
import re
def is_valid_email(email):
pattern = r"^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$"
return re.match(pattern, email) is not None
from datetime import datetime
def days_between_dates(date1, date2):
d1 = datetime.strptime(date1, "%Y-%m-%d")
d2 = datetime.strptime(date2, "%Y-%m-%d")
return abs((d2 - d1).days)
def chunk_list(lst, size):
return [lst[i:i + size] for i in range(0, len(lst), size)]
from urllib.parse import urlparse
def get_domain_name(url):
return urlparse(url).netloc
def filter_even_numbers(lst):
return [num for num in lst if num % 2 == 0]
import json
def read_json(file_path):
with open(file_path, 'r') as file:
return json.load(file)
from urllib.parse import urlparse, parse_qs
def extract_query_params(url):
parsed_url = urlparse(url)
return parse_qs(parsed_url.query)
from urllib.parse import urlparse
def extract_domain(url):
return urlparse(url).netloc
import os
def get_file_extension(file_path):
_, file_extension = os.path.splitext(file_path)
return file_extension
import os
def list_files(directory):
return [file for file in os.listdir(directory) if os.path.isfile(os.path.join(directory, file))]
import os
def join_path(directory, filename):
return os.path.join(directory, filename)
import os
def file_exists(file_path):
return os.path.isfile(file_path)
import os
def directory_exists(directory_path):
return os.path.isdir(directory_path)
def find_median(dataset):
sorted_list = sorted(dataset)
length = len(sorted_list)
if length % 2 == 0:
return (sorted_list[length // 2 - 1] + sorted_list[length // 2]) / 2
else:
return sorted_list[length // 2]
def calculate_variance(data):
mean = sum(data) / len(data)
return sum((x - mean) ** 2 for x in data) / len(data)
def calculate_std_deviation(data):
variance = calculate_variance(data)
return variance ** 0.5
def calculate_correlation(x, y):
mean_x = sum(x) / len(x)
mean_y = sum(y) / len(y)
numerator = sum((xi - mean_x) * (yi - mean_y) for xi, yi in zip(x, y))
denominator = (sum((xi - mean_x) ** 2 for xi in x) * sum((yi - mean_y) ** 2 for yi in y)) ** 0.5
return numerator / denominator if denominator else 0
import cv2
from PIL import Image, ImageDraw, ImageFont
import numpy as np
def pil2cv(image):
''' PIL型 -> OpenCV型 '''
new_image = np.array(image, dtype=np.uint8)
if new_image.ndim == 2: # モノクロ
pass
elif new_image.shape[2] == 3: # カラー
new_image = cv2.cvtColor(new_image, cv2.COLOR_RGB2BGR)
elif new_image.shape[2] == 4: # 透過
new_image = cv2.cvtColor(new_image, cv2.COLOR_RGBA2BGRA)
return new_image
import cv2
from PIL import Image, ImageDraw, ImageFont
import numpy as np
def cv2pil(image):
''' OpenCV型 -> PIL型 '''
new_image = image.copy()
if new_image.ndim == 2: # モノクロ
pass
elif new_image.shape[2] == 3: # カラー
new_image = cv2.cvtColor(new_image, cv2.COLOR_BGR2RGB)
elif new_image.shape[2] == 4: # 透過
new_image = cv2.cvtColor(new_image, cv2.COLOR_BGRA2RGBA)
new_image = Image.fromarray(new_image)
return new_image
import os
def get_current_directory():
return os.getcwd()
def create_directory(path):
if not os.path.exists(path):
os.makedirs(path)
def exists(path):
return os.path.exists(path)
def list_directory_contents(directory):
return os.listdir(directory)
def rename_or_move_file(current_path, new_path):
os.rename(current_path, new_path)
import os
def list_directory_recursively(directory):
paths = []
for root, dirs, files in os.walk(directory):
for name in files:
paths.append(os.path.join(root, name))
for name in dirs:
paths.append(os.path.join(root, name))
return paths
def get_file_size(file_path):
return os.path.getsize(file_path)
def get_last_modification_time(path):
return os.path.getmtime(path)
import os
import time
def change_file_metadata(path, access_time, modification_time):
os.utime(path, (access_time, modification_time))
def find_executable_path(filename):
for path in os.environ["PATH"].split(os.pathsep):
full_path = os.path.join(path, filename)
if os.path.isfile(full_path) and os.access(full_path, os.X_OK):
return full_path
return None
def remove_empty_directory(directory_path):
try:
os.rmdir(directory_path)
return True
except OSError:
return False
import shutil
def copy_directory(source_directory, target_directory):
shutil.copytree(source_directory, target_directory)
def change_permissions(path, mode):
os.chmod(path, mode)
def change_owner(path, uid, gid):
os.chown(path, uid, gid)
def create_symbolic_link(source, target):
os.symlink(source, target)
def get_absolute_path(path):
return os.path.abspath(path)
def get_directory_size(directory):
total_size = 0
for dirpath, dirnames, filenames in os.walk(directory):
for f in filenames:
fp = os.path.join(dirpath, f)
if os.path.exists(fp):
total_size += os.path.getsize(fp)
return total_size