Underrated step for logic building in programming.

Image
Logic building is a crucial and complex skill in programming. In essence, it is ability to come-up with solution of coding problem and write precise instructions ( or code) that a computer can execute autonomously. This skill requires aligning your thought process with computer and its capabilities. And running through code some-what abstractly to know and predict the behavior of code before it is executed. To be able to do this, one essential step that many beginner programmers overlook is performing dry runs. Understanding Dry Runs The concept of a dry run in programming is straightforward: can you mentally execute your code and predict its output without actually running it on a computer? While this seems simple, it is a challenging task. Typically, we are taught to write code, run it, and observe the output. This cycle is essential because code needs to run to be validated. However, if you rely solely on running your code to understand its behavior, you may struggle with building

Python Code # 12

 def binary_search(arr, target):

    """
Perform binary search on the given sorted list to find the target value.

Parameters:
- arr (list): The sorted list to be searched.
- target: The value to be searched for.

Returns:
- int: Index of the target value if found, otherwise -1.
"""
low, high = 0, len(arr) - 1

while low <= high:
mid = (low + high) // 2 # Calculate mid index

if arr[mid] == target:
return mid # Target found, return its index
elif arr[mid] < target:
low = mid + 1 # Adjust the search range to the right half
else:
high = mid - 1 # Adjust the search range to the left half

return -1 # Target not found

# Example usage:
my_sorted_list = [2, 5, 8, 12, 16, 23, 38, 45, 50]
target_value = 16

result = binary_search(my_sorted_list, target_value)

if result != -1:
print(f'Target {target_value} found at index {result}.')
else:
print(f'Target {target_value} not found in the list.')
---------------------------------------------------------------------------------------------
def binary_search(arr, target,low, high):

while low <= high:
mid = (low + high) // 2 # Calculate mid index

if arr[mid] == target:
return mid # Target found, return its index
elif arr[mid] < target:
low = mid + 1 # Adjust the search range to the right half
else:
high = mid - 1 # Adjust the search range to the left half

return -1 # Target not found

# Example usage:
my_sorted_list = [2, 5, 8, 12, 16, 23, 38, 45, 50]
target_value = 16

result = binary_search(my_sorted_list, target_value,0,8)

if result != -1:
print(f'Target {target_value} found at index {result}.')
else:
print(f'Target {target_value} not found in the list.')
---------------------------------------------------------------------------------------------
def binary_search_recursive(arr, target, start, end):
"""
Perform binary search on the given sorted list to find the target value.

Parameters:
- arr (list): The sorted list to be searched.
- target: The value to be searched for.
- start: The starting index for the search.
- end: The ending index for the search.

Returns:
- int: Index of the target value if found, otherwise -1.
"""
if start <= end:
mid = start + (end - start) // 2

# Check if the target is present at the middle
if arr[mid] == target:
return mid

# If the target is smaller, ignore the right half
elif arr[mid] > target:
return binary_search_recursive(arr, target, start, mid - 1)

# If the target is larger, ignore the left half
else:
return binary_search_recursive(arr, target, mid + 1, end)

else:
# Target is not present in the array
return -1

# Example usage:
sorted_list = [2, 5, 8, 12, 16, 23, 38, 45, 50]
target_value = 16

result = binary_search_recursive(sorted_list, target_value, 0, len(sorted_list) - 1)

if result != -1:
print(f'Target {target_value} found at index {result}.')
else:
print(f'Target {target_value} not found in the list.')
---------------------------------------------------------------------------------------------
def efficient_bubble_sort(names, dobs, phone_numbers):
n = len(names)
swapped = True

while swapped:
swapped = False

for i in range(0, n - 1):
if names[i] > names[i + 1]:
# Swap names
names[i], names[i + 1] = names[i + 1], names[i]
# Swap date of birth
dobs[i], dobs[i + 1] = dobs[i + 1], dobs[i]
# Swap phone numbers
phone_numbers[i], phone_numbers[i + 1] = phone_numbers[i + 1], phone_numbers[i]

swapped = True

return names, dobs, phone_numbers

# Example usage:
# Assuming you have three lists 'names', 'dob', and 'phone_numbers' with 200 elements each

# Sample data initialization (replace it with your actual data)
names = ["John", "Alice", "Bob", ...] # Replace with actual names
dob = ["1990-01-01", "1985-05-12", "1998-09-20", ...] # Replace with actual dates of birth
phone_numbers = ["123-456-7890", "987-654-3210", "555-123-4567", ...] # Replace with actual phone numbers

# Sorting the arrays using efficient bubble sort
names, dob, phone_numbers = efficient_bubble_sort(names, dob, phone_numbers)

# Now, 'names', 'dob', and 'phone_numbers' are sorted based on the 'names' array in ascending order.

Comments

Popular posts from this blog

Building JavaScript Array Methods from Scratch in 2024 - Easy tutorial for beginners # 1

Build eisenhower matrix with React, firebase and neumorphism ui . (Part one)

Creating a Dynamic Search Bar in React: A Step-by-Step Tutorial