import random

numbers = []
for i in range(10):
    numbers.append(random.randint(0,100))
print("Random List")
print(numbers)

bah = 0
for n in numbers:
    for n in numbers:
        if bah == 9:
            break
        if n > numbers[bah+1]:
            numbers.insert(bah+2, n)
            numbers.pop(bah)
        bah += 1
    bah = 0

print(numbers)
Random List
[12, 18, 71, 27, 0, 37, 69, 78, 98, 20]
[0, 12, 18, 20, 27, 37, 69, 71, 78, 98]
import nltk
import random

nltk.download('words')  # Download the word list (only required once)

from nltk.corpus import words

english_words = words.words()
#print(len(english_words))  # Prints the number of words in the list

# You can now use the 'english_words' list in your code

words = []
for i in range(10):
    words.append(english_words[random.randint(0,len(english_words))])
print("Random List")
print(words)

fa = 0
for word in words:
    words[fa] = word.lower()
    fa += 1
bah = 0
for n in words:
    for n in words:
        if bah == 9:
            break
        if n > words[bah+1]:
            words.insert(bah+2, n)
            words.pop(bah)
        bah += 1
    bah = 0
print(words)
Random List
['quantivalency', 'microsphere', 'The', 'caeremoniarius', 'chickenweed', 'inimitable', 'darts', 'preaccumulate', 'Silpha', 'reinterest']
['caeremoniarius', 'chickenweed', 'darts', 'inimitable', 'microsphere', 'preaccumulate', 'quantivalency', 'reinterest', 'silpha', 'the']
[nltk_data] Downloading package words to /home/kkcbal/nltk_data...
[nltk_data]   Package words is already up-to-date!
"""
* Creator: Nighthawk Coding Society
Bubble Sort of a List with optimizations
"""

# bubble sorts a list of dictionaries, base off of provided key
def bubbleSort(list, key):
    n = len(list) - 1  # list are indexed 0 to n-1, len is n
    
    # Traverse through list with i index
    for i in range(n):
        swapped = False  # optimize code, so it exits if now swaps on inner loop

        # Inner traversal using j index
        for j in range(n-i):  # n-i as positions on right are in order in bubble
 
            # Swap if the element KeyN is greater KeyN1
            keyN = list[j].get(key)
            keyN1 = list[j+1].get(key)
            if keyN > keyN1:
                swapped = True
                list[j], list[j + 1] = list[j + 1], list[j]  # single line swap
         
        if not swapped:  # if no swaps on inner pass, list is sorted
            return  # exit function
    

if __name__ == "__main__":
    # list/dictionary sample
    list_of_people = [
    {"name": "Risa", "age": 18, "city": "New York"},
    {"name": "John", "age": 63, "city": "Eugene"},
    {"name": "Shekar", "age": 18, "city": "San Francisco"},
    {"name": "Ryan", "age": 21, "city": "Los Angeles"}
    ]
    
    # assuming uniform keys, pick 1st row as source of keys
    key_row = list_of_people[0]

    # print list as defined
    print("Original")
    print(list_of_people)
    
    for key in key_row:  # finds each key in the row
        print(key)
        bubbleSort(list_of_people, key)  # sort list of people
        print(list_of_people)
Original
[{'name': 'Risa', 'age': 18, 'city': 'New York'}, {'name': 'John', 'age': 63, 'city': 'Eugene'}, {'name': 'Shekar', 'age': 18, 'city': 'San Francisco'}, {'name': 'Ryan', 'age': 21, 'city': 'Los Angeles'}]
name
[{'name': 'John', 'age': 63, 'city': 'Eugene'}, {'name': 'Risa', 'age': 18, 'city': 'New York'}, {'name': 'Ryan', 'age': 21, 'city': 'Los Angeles'}, {'name': 'Shekar', 'age': 18, 'city': 'San Francisco'}]
age
[{'name': 'Risa', 'age': 18, 'city': 'New York'}, {'name': 'Shekar', 'age': 18, 'city': 'San Francisco'}, {'name': 'Ryan', 'age': 21, 'city': 'Los Angeles'}, {'name': 'John', 'age': 63, 'city': 'Eugene'}]
city
[{'name': 'John', 'age': 63, 'city': 'Eugene'}, {'name': 'Ryan', 'age': 21, 'city': 'Los Angeles'}, {'name': 'Risa', 'age': 18, 'city': 'New York'}, {'name': 'Shekar', 'age': 18, 'city': 'San Francisco'}]

Is a list and/or dictionary in python considered a primitive or collection type? Why?

  • They're considered a collection type. They store a collection of values.

Is the list passed into bubble sort "pass-by-value" or "pass-by-reference? Describe why in relation to output.

  • It's pass-by-reference. You're passing the reference to the array when you call bubblesort
# Sort
numToKey = {
    0: "name",
    1: "age",
    2: "city"
}
list_of_people = [
    {"name": "Risa", "age": 18, "city": "New York"},
    {"name": "John", "age": 63, "city": "Eugene"},
    {"name": "Shekar", "age": 18, "city": "San Francisco"},
    {"name": "Ryan", "age": 21, "city": "Los Angeles"}
    ]

for A, B in numToKey.items():
    for i in range(len(list_of_people)):
        
        # Find the minimum element in remaining
        # unsorted array
        min_idx = i
        for j in range(i+1, len(list_of_people)):
            if list_of_people[min_idx][B] > list_of_people[j][B]:
                min_idx = j
                
        # Swap the found minimum element with
        # the first element       
        list_of_people[i], list_of_people[min_idx] = list_of_people[min_idx], list_of_people[i]
    print(B)
    for dictionary in list_of_people:
        print(dictionary)
name
{'name': 'John', 'age': 63, 'city': 'Eugene'}
{'name': 'Risa', 'age': 18, 'city': 'New York'}
{'name': 'Ryan', 'age': 21, 'city': 'Los Angeles'}
{'name': 'Shekar', 'age': 18, 'city': 'San Francisco'}
age
{'name': 'Risa', 'age': 18, 'city': 'New York'}
{'name': 'Shekar', 'age': 18, 'city': 'San Francisco'}
{'name': 'Ryan', 'age': 21, 'city': 'Los Angeles'}
{'name': 'John', 'age': 63, 'city': 'Eugene'}
city
{'name': 'John', 'age': 63, 'city': 'Eugene'}
{'name': 'Ryan', 'age': 21, 'city': 'Los Angeles'}
{'name': 'Risa', 'age': 18, 'city': 'New York'}
{'name': 'Shekar', 'age': 18, 'city': 'San Francisco'}