def two_sum(nums: list, target: int) -> list:
num_map = {}
for i, num in enumerate(nums):
complement = target - num
if complement in num_map:
return [num_map[complement], i]
num_map[num] = i
print(two_sum([2, 7, 11, 15], 9)) # Output: [0, 1]
def remove_duplicates(nums: list) -> list:
return list(set(nums))
print(remove_duplicates([1, 2, 2, 3, 4, 4, 5])) # Output: [1, 2, 3, 4, 5]
def two_sum(arr, target):
for i in range(len(arr)):
for j in range(i+1, len(arr)):
if arr[i] + arr[j] == target:
return [i, j]
return [-1, -1]
def merge_sorted_lists(list1: list, list2: list) -> list:
merged = []
i = j = 0
while i < len(list1) and j < len(list2):
if list1[i] < list2[j]:
merged.append(list1[i])
i += 1
else:
merged.append(list2[j])
j += 1
merged.extend(list1[i:])
merged.extend(list2[j:])
return merged
print(merge_sorted_lists([1, 3, 5], [2, 4, 6])) # Output: [1, 2, 3, 4, 5, 6]
def fibonacci(n):
if n <= 1:
return n
else:
return fibonacci(n-1) + fibonacci(n-2)
print(fibonacci(5)) # 5
def reverse_string(s: str) -> str:
return s[::-1]
print(reverse_string("hello")) # Output: "olleh"
def count_vowels(s: str) -> int:
vowels = 'aeiouAEIOU'
return sum(1 for char in s if char in vowels)
print(count_vowels("hello world")) # Output: 3
def first_non_repeated_char(s: str) -> str:
counts = {}
for char in s:
counts[char] = counts.get(char, 0) + 1
for char in s:
if counts[char] == 1:
return char
return None
print(first_non_repeated_char("swiss")) # Output: "w"
def longest_common_subsequence(str1, str2):
dp = [[0] * (len(str2) + 1) for _ in range(len(str1) + 1)]
# Fill the dp array
for i in range(1, len(str1) + 1):
for j in range(1, len(str2) + 1):
if str1[i - 1] == str2[j - 1]:
dp[i][j] = dp[i - 1][j - 1] + 1
else:
dp[i][j] = max(dp[i - 1][j], dp[i][j - 1])
# Reconstruct the LCS from the dp table
i, j = len(str1), len(str2)
lcs = []
while i > 0 and j > 0:
if str1[i - 1] == str2[j - 1]:
lcs.append(str1[i - 1])
i -= 1
j -= 1
elif dp[i - 1][j] > dp[i][j - 1]:
i -= 1
else:
j -= 1
# The lcs list will be in reverse order, so reverse it
lcs.reverse()
return dp[-1][-1], ''.join(lcs)
# Example usage
str1 = "ABCBDAB"
str2 = "BDCAB"
lcs_length, lcs_sequence = longest_common_subsequence(str1, str2)
print(f"The length of the longest common subsequence is: {lcs_length}")
print(f"The longest common subsequence is: {lcs_sequence}")
# OUTPUT
# The length of the longest common subsequence is: 4
T# he longest common subsequence is: BDAB
def is_palindrome(str):
return str == str[::-1]
print(is_palindrome("121")) # True
print(is_palindrome("122")) # False
def anagram(str1, str2):
# Sort both strings and compare them
return sorted(str1) == sorted(str2)
str1a = "listen"
str1b = "silent"
result = anagram(str1a, str1b)
print(f"Are '{str1a}' and '{str1b}' anagrams? {result}")
# Another example with non-anagrams
str2a = "hello"
str2b = "world"
result2 = anagram(str2a, str2b)
print(f"Are '{str2a}' and '{str2b}' anagrams? {result2}")
# OUTPUT
# Are 'listen' and 'silent' anagrams? True
# Are 'hello' and 'world' anagrams? False
def fizzbuzz(n):
if n % 3 == 0:
return "Fizz"
elif n % 5 == 0:
return "Buzz"
else:
return str(n)
def longest_common_subsequence(str1, str2):
dp = [[0] * (len(str2) + 1) for _ in range(len(str1) + 1)]
for i in range(1, len(str1) + 1):
for j in range(1, len(str2) + 1):
if str1[i - 1] == str2[j - 1]:
dp[i][j] = dp[i - 1][j - 1] + 1
else:
dp[i][j] = max(dp[i - 1][j], dp[i][j - 1])
return dp[-1][-1]
def hamming_distance(str1, str2):
"""Calculate the Hamming distance between two strings of equal length.
Args:
str1 (string): The first string for comparison.
str2 (string): The second string for comparison.
Returns:
int: The Hamming distance between the two strings.
"""
# Check if strings are of equal length
if len(str1) != len(str2):
raise ValueError("Strings must be of the same length")
# Calculate the Hamming distance by comparing each character in the two strings
distance = sum(c1 != c2 for c1, c2 in zip(str1, str2))
return distance
# --------------------------------------------------------------------------------- #
str1 = "karolin"
str2 = "kathrin"
distance = hamming_distance(str1, str2)
print(f"The Hamming distance between '{str1}' and '{str2}' is: {distance}")
# OUTPUT
# The Hamming distance between 'karolin' and 'kathrin' is: 3
import numpy as np
def levenshtein_distance(str1, str2):
"""Calculate the Levenshtein distance between two strings.
Args:
str1 (string): The first string for comparison.
str2 (string): The second string for comparison.
Returns:
int: The Levenshtein distance between the two strings.
"""
m = len(str1)
n = len(str2)
# Create a 2D array to store the distances
dp = np.zeros((m + 1, n + 1))
# Initialize the base cases
for i in range(m + 1):
dp[i][0] = i # Cost of deleting all characters from str1
for j in range(n + 1):
dp[0][j] = j # Cost of inserting all characters into str1
# Fill the dp array
for i in range(1, m + 1):
for j in range(1, n + 1):
if str1[i - 1] == str2[j - 1]:
dp[i][j] = dp[i - 1][j - 1] # No operation needed
else:
dp[i][j] = min(dp[i - 1][j - 1] + 1, # Substitution
dp[i - 1][j] + 1, # Deletion
dp[i][j - 1] + 1) # Insertion
return int(dp[m][n])
# ---------------------------------------------------------------------------- #
str1 = "kitten"
str2 = "sitting"
distance = levenshtein_distance(str1, str2)
print(f"The Levenshtein distance between '{str1}' and '{str2}' is: {distance}")
# ---------------------------------------------------------------------------- #
# OUTPUT
# The Levenshtein distance between 'kitten' and 'sitting' is: 3
# ---------------------------------------------------------------------------- #
str1 = "flaw"
str2 = "lawn"
distance = levenshtein_distance(str1, str2)
print(f"The Levenshtein distance between '{str1}' and '{str2}' is: {distance}")
# ---------------------------------------------------------------------------- #
# OUTPUT
# The Levenshtein distance between 'flaw' and 'lawn' is: 2
# ---------------------------------------------------------------------------- #
def list_intersection(list1: list, list2: list) -> list:
return list(set(list1) & set(list2))
print(list_intersection([1, 2, 3, 4], [3, 4, 5, 6])) # Output: [3, 4]
def binary_search(arr, target):
low = 0
high = len(arr) - 1
while low <= high:
mid = (low + high) // 2
if arr[mid] == target:
return mid
elif arr[mid] < target:
low = mid + 1
else:
high = mid - 1
return -1
def quick_sort(arr):
if len(arr) <= 1:
return arr
pivot = arr[0]
left = [x for x in arr[1:] if x <= pivot]
right = [x for x in arr[1:] if x > pivot]
return quick_sort(left) + [pivot] + quick_sort(right)
def merge_sort(arr):
if len(arr) <= 1:
return arr
mid = len(arr) // 2
left = merge_sort(arr[:mid])
right = merge_sort(arr[mid:])
return merge(left, right)
def merge(left, right):
result = []
while left and right:
if left[0] <= right[0]:
result.append(left.pop(0))
else:
result.append(right.pop(0))
if left:
result += left
if right:
result += right
return result
def fibonacci_generator():
a, b = 0, 1
while True:
yield a
a, b = b, a + b
fib_gen = fibonacci_generator()
# Generate and print the first 10 Fibonacci numbers
for _ in range(10):
print(next(fib_gen))
This Python code defines a generator function that produces Fibonacci numbers indefinitely:
fibonacci_generator() is defined to yield an infinite sequence of Fibonacci numbers.yield statement produces the current value of a and pauses the function's execution, preserving its state for the next iteration.a and b are updated to the next two values in the Fibonacci sequence: a takes the value of b, and b takes the sum of the old a and b.for loop.