My QA Projects

QA Projects I was involded.

View on GitHub

Searching Algorithms:

1.1 Linear Search with list

Function defintion takes two arguments

List Characteristics:

Why Use a List for Linear Search:

Step 1: Function Definition: The linear_search function takes two parameters: arr (the list to be searched) and target (the element to search for).

Step 2: Iteration: The function iterates over each element in the list using a for loop. The range(len(arr)) generates indices from 0 to len(arr) - 1.

Step 3: Comparison: Inside the loop, the function compares the current element (arr[index]) with the target. If they are equal, the function returns the current index.

Step 4: Return Statement: If the loop completes without finding the target, the function returns -1, indicating that the target is not in the list.

"""
    Perform linear search to find the index of target in arr.
    Returns the index if found, otherwise returns -1.
"""
def linear_search(arr, target):
    # Iterate through each element in the list
    for i in range(len(arr)):
        # Check if the current element is equal to the target
        if arr[i] == target:
            # Return the index of the element if found
            return i
    # Return -1 if the target is not found in the list
    return -1

# Example
arr = [10, 20, 30, 40, 50]
target = 30
result = linear_search(arr, target)
if result != -1:
    print(f"Element {target} found at index {result}")
else:
    print(f"Element {target} not found in the list")

1.2 Linear Search with a Dictionary

def linear_search_dict(d, target):
    # Iterate through each key-value pair in the dictionary
    for key, value in d.items():
        # Check if the current value is equal to the target
        if value == target:
            # Return the key of the element if found
            return key
    # Return None if the target is not found in the dictionary
    return None

# Example usage:
d = {'a': 10, 'b': 20, 'c': 30, 'd': 40, 'e': 50}
target = 30
result = linear_search_dict(d, target)
if result is not None:
    print(f"Value {target} found with key '{result}'")
else:
    print(f"Value {target} not found in the dictionary")

Step 1: Function Definition: The linear_search_dict function takes two parameters: d (the dictionary to be searched) and target (the value to search for).

Step 2: Iteration: The function iterates over each key-value pair in the dictionary using a for loop. The d.items() method returns a view object that displays a list of dictionary’s key-value tuple pairs.

Step 3: Comparison: Inside the loop, the function compares the current value (value) with the target. If they are equal, the function returns the corresponding key.

Step 4: Return Statement: If the loop completes without finding the target, the function returns None, indicating that the target is not in the dictionary.