My QA Projects

QA Projects I was involded.

View on GitHub

Bubble Sort w/ Dictionaries

def bubble_sort_dict_by_value(dictionary):
    items = list(dictionary.items())
    # convert dictionary into a list of tuples
    n = len(items)

    # Traverse through all dictionary elements
    for i in range(n):
        # Last i elements are already in place
        for j in range(0, n-i-1):
            # Traverse the dictionary from 0 to n-i-1
            # Swap if the value of the current element is greater than the next element
            if items[j][1] > items[j+1][1]:
                items[j], items[j+1] = items[j+1], items[j]
    
    # Convert sorted items back to dictionary
    sorted_dict = dict(items)
    return sorted_dict

# Example
dictionary = {'apple': 5, 'banana': 3, 'cherry': 7, 'date': 10, 'fig': 2}
print("Original dictionary:", dictionary)

sorted_dict = bubble_sort_dict_by_value(dictionary)

print("Sorted dictionary by value:", sorted_dict)

# OUTPUT
# Original dictionary: {'apple': 5, 'banana': 3, 'cherry': 7, 'date': 10, 'fig': 2}
# Sorted dictionary by value: {'fig': 2, 'banana': 3, 'apple': 5, 'cherry': 7, 'date': 10}

items = list(dictionary.items()) - TypeError: ‘list’ object is not callable

bubble_sort_dict_by_value(dictionary):

Bubble Sort based on keys.

def bubble_sort_dict_by_keys(dictionary):
  """
  Args:
    dictionary: The dictionary to be sorted.
  Returns:
    A new dictionary with keys sorted in ascending order.
  """

  keys = list(dictionary.keys())
  #  Converts dict keys into a list (easier manipulation).
  n = len(keys)
  # of keys in the dict.

  # Traverse through all keys
  for i in range(n):
    # Outer loop iterates through each key in the list
    for j in range(0, n - i - 1):
      # Compare the keys within the unsorted portion.
      if keys[j] > keys[j + 1]:
        # Swap keys
        # Swaps the positions of the keys if the current key is greater than the next key. 
        keys[j], keys[j + 1] = keys[j + 1], keys[j]

  # Create a new dictionary with sorted keys
  sorted_dict = {}
  for key in keys:
    sorted_dict[key] = dictionary[key]

  return sorted_dict

# Example :
data = {'c': 3, 'a': 1, 'b': 2}

# Sort the dictionary by keys
sorted_data = bubble_sort_dict_by_keys(data)

# Print the sorted dictionary
print(sorted_data)

Bubble Sort based on keys #2

def bubble_sort_by_key(data, key):
  """
  Sorts a list of dictionaries based on a specific key using Bubble Sort.

  Args:
    data: A list of dictionaries to be sorted.
    key: The key in the dictionaries to sort by.

  Returns:
    The sorted list of dictionaries.
  """

  n = len(data)

  # Traverse through all array elements
  for i in range(n):

    # Last i elements are already in place
    for j in range(0, n - i - 1):

      # Compare the values of the dictionaries based on the specified key
      if data[j][key] > data[j + 1][key]:
        # Swap if the current element is greater than the next element
        data[j], data[j + 1] = data[j + 1], data[j]

  return data
  # Returns the sorted list of dictionaries

# Example usage:
data = [
  {'name': 'Alice', 'age': 25},
  {'name': 'Bob', 'age': 30},
  {'name': 'Charlie', 'age': 20},
]

# Sort by age
sorted_data = bubble_sort_by_key(data, 'age')

# Print the sorted data
print(sorted_data)