School Commit Init
This commit is contained in:
@@ -0,0 +1,129 @@
|
||||
# Byte-compiled / optimized / DLL files
|
||||
__pycache__/
|
||||
*.py[cod]
|
||||
*$py.class
|
||||
|
||||
# C extensions
|
||||
*.so
|
||||
|
||||
# Distribution / packaging
|
||||
.Python
|
||||
build/
|
||||
develop-eggs/
|
||||
dist/
|
||||
downloads/
|
||||
eggs/
|
||||
.eggs/
|
||||
lib/
|
||||
lib64/
|
||||
parts/
|
||||
sdist/
|
||||
var/
|
||||
wheels/
|
||||
pip-wheel-metadata/
|
||||
share/python-wheels/
|
||||
*.egg-info/
|
||||
.installed.cfg
|
||||
*.egg
|
||||
MANIFEST
|
||||
|
||||
# PyInstaller
|
||||
# Usually these files are written by a python script from a template
|
||||
# before PyInstaller builds the exe, so as to inject date/other infos into it.
|
||||
*.manifest
|
||||
*.spec
|
||||
|
||||
# Installer logs
|
||||
pip-log.txt
|
||||
pip-delete-this-directory.txt
|
||||
|
||||
# Unit test / coverage reports
|
||||
htmlcov/
|
||||
.tox/
|
||||
.nox/
|
||||
.coverage
|
||||
.coverage.*
|
||||
.cache
|
||||
nosetests.xml
|
||||
coverage.xml
|
||||
*.cover
|
||||
*.py,cover
|
||||
.hypothesis/
|
||||
.pytest_cache/
|
||||
|
||||
# Translations
|
||||
*.mo
|
||||
*.pot
|
||||
|
||||
# Django stuff:
|
||||
*.log
|
||||
local_settings.py
|
||||
db.sqlite3
|
||||
db.sqlite3-journal
|
||||
|
||||
# Flask stuff:
|
||||
instance/
|
||||
.webassets-cache
|
||||
|
||||
# Scrapy stuff:
|
||||
.scrapy
|
||||
|
||||
# Sphinx documentation
|
||||
docs/_build/
|
||||
|
||||
# PyBuilder
|
||||
target/
|
||||
|
||||
# Jupyter Notebook
|
||||
.ipynb_checkpoints
|
||||
|
||||
# IPython
|
||||
profile_default/
|
||||
ipython_config.py
|
||||
|
||||
# pyenv
|
||||
.python-version
|
||||
|
||||
# pipenv
|
||||
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
|
||||
# However, in case of collaboration, if having platform-specific dependencies or dependencies
|
||||
# having no cross-platform support, pipenv may install dependencies that don't work, or not
|
||||
# install all needed dependencies.
|
||||
#Pipfile.lock
|
||||
|
||||
# PEP 582; used by e.g. github.com/David-OConnor/pyflow
|
||||
__pypackages__/
|
||||
|
||||
# Celery stuff
|
||||
celerybeat-schedule
|
||||
celerybeat.pid
|
||||
|
||||
# SageMath parsed files
|
||||
*.sage.py
|
||||
|
||||
# Environments
|
||||
.env
|
||||
.venv
|
||||
env/
|
||||
venv/
|
||||
ENV/
|
||||
env.bak/
|
||||
venv.bak/
|
||||
|
||||
# Spyder project settings
|
||||
.spyderproject
|
||||
.spyproject
|
||||
|
||||
# Rope project settings
|
||||
.ropeproject
|
||||
|
||||
# mkdocs documentation
|
||||
/site
|
||||
|
||||
# mypy
|
||||
.mypy_cache/
|
||||
.dmypy.json
|
||||
dmypy.json
|
||||
|
||||
# Pyre type checker
|
||||
.pyre/
|
||||
@@ -0,0 +1,33 @@
|
||||
# Assignment 02
|
||||
|
||||
## Requirements
|
||||
Implement a menu-driven console application to help visualize the way sorting algorithms work. You will be given two of the algorithms from the list below to implement (one from each set). When started, the program will print a menu with the following options:
|
||||
- Generate a list of `n` random natural numbers. Generated numbers must be between `0` and `100`.
|
||||
- Sort the list using the first algorithm. (see **NB!** below)
|
||||
- Sort the list using the second algorithm. (see **NB!** below)
|
||||
- Exit the program
|
||||
|
||||
## NB!
|
||||
Before starting each sort, the program will ask for the value of parameter `step`. During sorting, the program will display the partially sorted list on the console each time it makes `step` operations or passes, depending on the algorithm (e.g., if `step=2`, display the partially sorted list after each 2 element swaps in bubble sort, after each 2 element insertions in insert sort, after every 2nd generation of a permutation in permutation sort etc.).
|
||||
|
||||
## Implementation requirements
|
||||
- Write a function for each sorting algorithm; each function should take as parameter the list to be sorted and the value of parameter `step` that was read from the console.
|
||||
- Functions communicate using input parameter(s) and return values (**DO NOT use global, or module-level variables**)
|
||||
- Provide the user with a menu-driven console-based user interface. Input data should be read from the console and the results printed to the console. At each step, the program must provide the user the context of the operation (never display an empty prompt).
|
||||
- You may use Internet resources to research the sorting algorithm, but you must be able to explain **how** and **why** they work in detail
|
||||
|
||||
## Sorting algorithms
|
||||
### Basic set
|
||||
- Bubble Sort
|
||||
- Cocktail Sort
|
||||
- Insert Sort
|
||||
- Permutation Sort
|
||||
- Selection Sort
|
||||
|
||||
### Advanced set
|
||||
- Shell Sort
|
||||
- Comb Sort
|
||||
- Gnome Sort
|
||||
- Stooge Sort
|
||||
- Strand Sort
|
||||
|
||||
@@ -0,0 +1,91 @@
|
||||
import random
|
||||
|
||||
def GUI():
|
||||
current_list=[]
|
||||
while True:
|
||||
print("1 - Generate a List")
|
||||
print("2 - Show Current List")
|
||||
print("3 - Bubble Sort")
|
||||
print("4 - Strand Sort")
|
||||
print("0 - Exit")
|
||||
value = int(input())
|
||||
match value:
|
||||
case 1:
|
||||
n=int(input("Select lenght of the list: "))
|
||||
current_list = generate_list(n)
|
||||
print("Generated list: {}\n".format(current_list))
|
||||
case 2:
|
||||
show_list(current_list)
|
||||
case 3:
|
||||
steps=int(input("Please select the number of steps: "))
|
||||
current_list=bubble_sort(current_list,steps)
|
||||
case 4:
|
||||
steps=int(input("Please select the number of steps: "))
|
||||
current_list=strand_sort(current_list,steps)
|
||||
case 0:
|
||||
exit()
|
||||
case _:
|
||||
print("Invalid command")
|
||||
|
||||
def bubble_sort(l,step):
|
||||
print("Starting list: {}".format(l))
|
||||
current_step=0
|
||||
for i in range(len(l)-1):
|
||||
for j in range(len(l)-1-i):
|
||||
if l[j]>l[j+1]:
|
||||
l[j],l[j+1]=l[j+1],l[j]
|
||||
current_step+=1
|
||||
if current_step % step == 0:
|
||||
print("List after {} steps: {}".format(current_step,l))
|
||||
print("Sorted list: {}\n".format(l))
|
||||
return l
|
||||
|
||||
def strand_sort(l,step):
|
||||
print("Starting list: {}\n".format(l))
|
||||
current_step=0
|
||||
aux_list=[]
|
||||
final_list=[]
|
||||
while l:
|
||||
aux_list=[]
|
||||
aux_list.append(l.pop(0))
|
||||
current_step+=1
|
||||
strand_sort_message(current_step,l,aux_list,final_list,step)
|
||||
for i in l.copy():
|
||||
if aux_list[-1] <= i:
|
||||
aux_list.append(l.pop(l.index(i)))
|
||||
current_step+=1
|
||||
strand_sort_message(current_step,l,aux_list,final_list,step)
|
||||
if final_list:
|
||||
i=0
|
||||
while aux_list:
|
||||
if aux_list[-1]>final_list[i]:
|
||||
i+=1
|
||||
else:
|
||||
final_list.insert(i,aux_list[-1])
|
||||
aux_list.pop()
|
||||
current_step+=1
|
||||
strand_sort_message(current_step,l,aux_list,final_list,step)
|
||||
i=0
|
||||
else:
|
||||
final_list=aux_list.copy()
|
||||
aux_list.clear()
|
||||
current_step+=1
|
||||
strand_sort_message(current_step,l,aux_list,final_list,step)
|
||||
print("Sorted list: {}\n".format(final_list))
|
||||
return final_list
|
||||
|
||||
def strand_sort_message(current_step,l,aux_list,final_list,step):
|
||||
if current_step%step==0:
|
||||
print("Lists after {} steps:".format(current_step))
|
||||
print("Starting list: {}".format(l))
|
||||
print("Working list: {}".format(aux_list))
|
||||
print("Final list: {}\n".format(final_list))
|
||||
|
||||
def generate_list(n):
|
||||
return [random.randint(0,100) for _ in range(n)]
|
||||
|
||||
def show_list(l):
|
||||
print("Current list: {}\n".format(l))
|
||||
|
||||
if __name__=="__main__":
|
||||
GUI()
|
||||
Reference in New Issue
Block a user