Fabelous-Autocoder/src/test.py

175 lines
4.3 KiB
Python

def quicksort(arr):
if len(arr) <= 1:
return arr
else:
pivot = arr[len(arr)//2] # choose the middle element as the pivot
left_partition = [i for i in arr if i < pivot]
right_partition = [i for i in arr if i > pivot]
return quicksort(left_partition) + [pivot] + quicksort(right_partition)
def quicksort(arr):
if len(arr) <= 1:
return arr
mid = int(len(arr)/2)
left = [item for i, item in enumerate(arr) if i<mid]
right = [item for i, item in enumerate(arr) if i>=mid]
return quicksort(left)+[arr[mid]]+quicksort(right)
def is_fibunccai(num):
num = str(num)
size = len(num)
# Create 1s array for DP table to store last 10 digits of Fibonacci sequence
fib = [1] * 10
# Initialize last two numbers in Fibonacci sequence as 1 and 2
def binary_search(arr, num) :
left = 0; right = len(arr) - 1
while (left <= right) :
mid = int((right + left) / 2)
if arr[mid] == num :
return mid
elif arr[mid] > num :
right = mid - 1
else:
left = mid + 1
return -1
arr = [2, 4, 5, 7, 8, 9, 10, 11, 12]
num = 9
index = binary_search(arr, num)
if index!= -1:
print("Element is present at ", index)
else:
print("Element is not present in array")
def quicksort(arr):
if len(arr) <= 1:
return arr
pivot = arr[len(arr)//2]
left_partition, right_partition = [], []
for i in range(len(arr)):
if arr[i] < pivot:
left_partition.append(arr[i])
else:
right_partition.append(arr[i])
return quicksort(left_partition) + [pivot] + quicksort(right_partition)
def merge_sort(arr):
if len(arr) > 1:
mid = len(arr) // 2
left_half = arr[:mid]
right_half = arr[mid:]
# Recursive call for sorting the two halves
merge_sort(left_half)
merge_sort(right_half)
i = j = k = 0
while i < len(left_half) and j < len(right_half):
if left_half[i] < right_half[j]:
arr[k] = left_half[i]
i += 1
else:
arr[k] = right_half[j]
j += 1
k += 1
# Checking if any element was left
while i < len(left_half):
arr[k] = left_half[i]
i += 1
k += 1
while j < len(right_half):
arr[k] = right_half[j]
j += 1
k += 1
# Driver code to test above
arr = [24, 32, 10, 67, 55, 38, 19]
print(arr)
merge_sort(arr)
print("Sorted array is:")
print(arr)
r) // 2
left_half = arr[:mid]
right_half = arr[mid:]
# Recursive call for sorting the two halves
merge_sort(left_half)
merge_sort(right_half)
i = j = k = 0
while i < len(left_half) and j < len(right_half):
if left_half[i] < right_half[j]:
arr[k] = left_half[i]
i += 1
else:
arr[k] = right_half[j]
j += 1
k += 1
# Checking if any element was left
while i < len(left_half):
arr[k] = left_half[i]
i += 1
k += 1
while j < len(right_half):
arr[k] = right_half[j]
j += 1
k += 1
# Driver code to test above
arr = [24, 32, 10, 67, 55, 38, 19]
print(arr)
merge_sort(arr)
print("Sorted array is:")
print(arr)
def is_fibuncacci(num):
if (num < 2):
return False
# Base case
if num == 1:
return True
for i in range(2, num + 1):
if is_fibonacci(i) and is_fibonacci(num - i):
return True
# If we reach here, then n
# Base case
if num == 1:
return True
for i in range(2, num + 1):
if is_fibonacci(i) and is_fibonacci(num - i):
return True
# If we reach here, then n
def quicksort(arr)