Tech Talk Hacks
Hacks!!
This Code is using Merge Sort:
Merge sort of reminds me of a binary search because this breaks the list in halves and then sorts the those smaller parts before putting itself back together in the right sorted order. Binary search also cuts the list in half but instead of sorting, it is looking for the number in one of the halves and gets rid of the half that doesn't have the number it wants (although the list has to be sorted already).
import nltk
import random
from nltk.corpus import words
nltk.download('words') # Download the word list (only required once)
english_words = words.words()
def new_words():
# You can now use the 'english_words' list in your code
random_words = []
for i in range(10):
random_words.append(english_words[random.randint(0,len(english_words))])
return random_words
print("Random List")
print(new_words())
def merge_sort(word_list):
if len(word_list) <= 1:
return word_list
mid = len(word_list) // 2
left_half = word_list[:mid]
right_half = word_list[mid:]
left_half = merge_sort(left_half)
right_half = merge_sort(right_half)
return merge(left_half, right_half)
def merge(left_half, right_half):
result = []
left_index = 0
right_index = 0
while left_index < len(left_half) and right_index < len(right_half):
if left_half[left_index] <= right_half[right_index]:
result.append(left_half[left_index])
left_index += 1
else:
result.append(right_half[right_index])
right_index += 1
while left_index < len(left_half):
result.append(left_half[left_index])
left_index += 1
while right_index < len(right_half):
result.append(right_half[right_index])
right_index += 1
return result
def new_words():
random_words = []
for _ in range(10):
random_words.append(english_words[random.randint(0, len(english_words) - 1)])
random_words = merge_sort(random_words)
return random_words
print("Random List:")
random_list = new_words()
print(random_list)
Is a list and/or dictionary in python considered a primitive or collection type? Why?
- Lists and Dictionaries are collecton types because they can store multiple variables whereas primitive types are things such as strings and integers, where they can only store one variable.
Is the list passed into bubble sort "pass-by-value" or "pass-by-reference? Describe why in relation to output.
- I think that it might be a pass-by-reference because everything in python is there to describe an object (at least when you put in a class) so since it is that it can't just be referencing a single value so I don't think it is pass-by-value.
Research Analysis:
-
Comparisons: This compares the elements in the list in order to figure out what the order of the list is supposed to be. It then swaps elements after comparing and then that is how the list gets ordered.
-
Swaps: The Bubble sort relies heavily on swapping the variables in order to order the list.
-
Time: Sorting algorithms have a time complexity, meaning the amount of time it takes for the program to execute. One example could be the code down below which uses time to count how long it takes for the duck to get to the end.
Animation: Instead with a Duck :)
"""
* Creator: Nighthawk Coding Society
Sailing Ship Animation (programatic method)
"""
import time # used for delay
from IPython.display import clear_output # jupyter specific clear
# ANSI Color Codes
OCEAN_COLOR = u"\u001B[34m\u001B[2D"
SHIP_COLOR = u"\u001B[35m\u001B[2D"
RESET_COLOR = u"\u001B[0m\u001B[2D"
def ship_print(position): # print ship with colors and leading spaces according to position
clear_output(wait=True)
print(RESET_COLOR)
sp = " " * position
print(sp + " _ ")
print(sp + " __(.)< ")
print(sp + " \\_)_)")
print(OCEAN_COLOR + "--"*32 + RESET_COLOR)
def ship(): # ship function, loop/controller for animation speed and times
# loop control variables
start = 0 # start at zero
distance = 70 # how many times to repeat
step = 2 # count by 2
# loop purpose is to animate ship sailing
for position in range(start, distance, step):
ship_print(position) # call to function with parameter
time.sleep(.2)
ship() # activate/call ship function