School Commit Init

This commit is contained in:
2024-08-31 12:07:21 +03:00
commit 0b130ee18c
2801 changed files with 4720552 additions and 0 deletions
@@ -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,42 @@
# :computer: Assignment 05
## Requirements
- Use functions to: `read a complex number` from the console, `write a complex number` to the console, implement `each required functionality`.
- Functions communicate using input parameter(s) and the return statement (**DO NOT use** global variables, nested functions, the `global` or `nonlocal` keywords)
- Have two separate representations for each complex number, one using a `list` and another using a `dictionary`. Write methods to create a new complex number, to get and set each number's real and imaginary parts as well as to transform a number into its `str` representation. The program must work with both implementations, by either commenting out one of them or changing the order in which the corresponding functions are defined.
- Separate input/output functions (those using `print` and `input` statements) from those performing the calculations (see **program.py**)
- 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 (do not display an empty prompt).
- Deadline is **week 7** for maximum grade, week 9 is a hard deadline.
## Problem Statement
Implement a menu-driven console application that provides the following functionalities:
1. Read a list of complex numbers (in `z = a + bi` form) from the console.
2. Display the entire list of numbers on the console.
3. Display on the console the sequence, subarray or numbers required by the properties that were assigned to you. Each student will receive one property from **Set A** and another one from **Set B**.
4. Exit the application.
**The source code will include:**
- Specifications for the functions related to point 3 above.
- 10 complex numbers already available at program startup.
### Set A (naive implementation)
1. Length and elements of a longest subarray of distinct numbers.
2. Length and elements of a longest subarray of numbers having the same modulus.
3. Length and elements of a longest subarray of numbers having increasing modulus.
4. Length and elements of a longest subarray of numbers that contain at most 3 distinct values.
5. Length and elements of a longest subarray of numbers where each number's modulus is in the `[0, 10]` range.
6. Length and elements of a longest subarray of numbers where the difference between the modulus of consecutive numbers is a prime number.
7. Length and elements of a longest subarray of numbers where their real part is in the form of a mountain (first the values increase, then they decrease). (e.g. `1-i, 2+6i, 4-67i, 90+3i, 80-7i, 76+i, 43-12i, 3`)
8. Length and elements of a longest subarray of numbers where both their real and imaginary parts can be written using the same base 10 digits (e.g. `1+3i, 31i, 33+i, 111, 11-313i`)
## Set B (dynamic programming implementation required)
9. The length and elements of a longest increasing subsequence, when considering each number's modulus
10. The length and elements of a longest increasing subsequence, when considering each number's real part
11. The length and elements of a maximum subarray sum, when considering each number's real part
12. The length and elements of a longest alternating subsequence, when considering each number's real part (e.g., given sequence [1, 3, 2, 4, 10, 6, 1], [1, 3, 2, 10] is an alternating subsequence, because 1 < 3 > 2 < 10)
13. The length of a longest alternating subsequence, when considering each number's modulus (e.g., given sequence [1, 3, 2, 4, 10, 6, 1], [1, 3, 2, 10] is an alternating subsequence, because 1 < 3 > 2 < 10)
## Observations
- Elements of a **subarray** are in consecutive order of their appearance in the array, while in a **subsequence**, this isn't necessarily true.
- The longest subarray/subsequence might not be unique, so determining a single one is sufficient.
- Understand and be able to explain the time and extra-space computational complexity of your implementation.
@@ -0,0 +1,71 @@
# UI section
# Write all functions that have input or print statements here
# Ideally, this section should not contain any calculations relevant to program functionalities
import os
import re
# from list_repr import create_number,stringify
from dict_repr import create_number,stringify
from backend import *
def clear_terminal()->None:
os.system('cls' if os.name=='nt' else 'clear')
def read_numbers(numbers:list)->list:
clear_terminal()
print("Please enter a complex number in the format a+bi or type: \"exit\" to exit.")
while True:
inp=input("> ")
if inp =="exit":
return numbers
elif re.match("[-]?\d+[+-]\d+i",inp)!=None:
parsed_list=re.findall("[-]?\d+",inp)
number=create_number(int(parsed_list[0]),int(parsed_list[1]))
numbers.append(number)
else:
print("Invalid Number")
def display_numbers(numbers:list)->None:
if numbers == []:
print("The list is empty")
return
for number in numbers:
print(stringify(number),end="\t")
def display_longest_subarray_of_distinct_numbers(numbers:list)->None:
lenght,array=get_longest_subarray_of_distinct_numbers(numbers)
print("The longest subarray of distinct numbers is of lenght {} and its elements are: ".format(lenght))
display_numbers(array)
def display_lenght_of_longest_alternatig_subsequence(numbers:list)->None:
print("The length of the longest alternating subsequence is: {}".format(get_lenght_of_longest_alternatig_subsequence(numbers)))
def UI()->None:
numbers=generate_numbers(10)
while True:
print("""
Please select a number:
1. Read a list of number
2. Display the list
3. Display the length and elements of a longest subarray of distinct numbers
4. Display the length of a longest alternating subsequence, when considering each number's modulus
9. Clear the terminal
0. Exit
""")
selected_command=input("> ")
match selected_command:
case "1":
numbers=read_numbers(numbers)
case "2":
display_numbers(numbers)
case "3":
display_longest_subarray_of_distinct_numbers(numbers)
case "4":
display_lenght_of_longest_alternatig_subsequence(numbers)
case "9":
clear_terminal()
case "0":
return
case _:
print("Invalid Command")
@@ -0,0 +1,66 @@
# Functions that deal with subarray/subsequence properties
# -> There should be no print or input statements in this section
# -> Each function should do one thing only
# -> Functions communicate using input parameters and their return values
# Naive:1 Dynamic:13
import random
# from list_repr import get_modulus,create_number
from dict_repr import get_modulus,create_number
def generate_numbers(n:int)->list:
list_of_numbers=[]
while n:
real_part=random.randint(-10,10)
img_part=random.randint(-10,10)
number=create_number(real_part,img_part)
list_of_numbers.append(number)
n-=1
return list_of_numbers
def get_longest_subarray_of_distinct_numbers(numbers:list)->tuple[int,list]:
"""
:params:
numbers - list
:return:
tuple[int,list]
The function takes in a list of complex numbers and returns the length of
a longest subarray of distinct numbers as well as the elements of such subarray
"""
max_lenght=0
solution=[]
length_of_list=len(numbers)
for i in range(length_of_list-1):
current_solution=[numbers[i]]
for j in range(i+1,length_of_list):
if numbers[j] not in current_solution:
current_solution.append(numbers[j])
else:
break
if max_lenght<len(current_solution):
max_lenght=len(current_solution)
solution=current_solution
return max_lenght,solution
def get_lenght_of_longest_alternatig_subsequence(numbers:list)->int:
"""
:params:
numbers - list
:return:
int
The function takes in a list of complex numbers and returns the length of
the longest alternating subsequence of numbers, when considering each number's modulus
"""
lenght=len(numbers)
inc=1
dec=1
for i in range(1,lenght):
if get_modulus(numbers[i])>get_modulus(numbers[i-1]):
inc=dec+1
elif get_modulus(numbers[i])<get_modulus(numbers[i-1]):
dec=inc+1
return max(inc,dec)
@@ -0,0 +1,27 @@
# Functions to deal with complex numbers -- dict representation
# -> There should be no print or input statements in this section
# -> Each function should do one thing only
# -> Functions communicate using input parameters and their return values
def create_number(a:int,b:int)->list:
return {"real":a, "img":b}
def get_real(z:dict)->int:
return z["real"]
def get_img(z:dict)->int:
return z["img"]
def set_real(z:dict,a:int)->list:
z["real"]=a
return z
def set_img(z:dict,b:int)->list:
z["img"]=b
return z
def stringify(z:dict)->str:
return "{}{}{}i".format(get_real(z),"+" if get_img(z)>=0 else "",get_img(z))
def get_modulus(z:dict)->float:
return (get_real(z)**2+get_img(z)**2)**(1/2)
@@ -0,0 +1,27 @@
# Functions to deal with complex numbers -- list representation
# -> There should be no print or input statements in this section
# -> Each function should do one thing only
# -> Functions communicate using input parameters and their return values
def create_number(a:int,b:int)->list:
return [a,b]
def get_real(z:list)->int:
return z[0]
def get_img(z:list)->int:
return z[1]
def set_real(z:list,a:int)->list:
z[0]=a
return z
def set_img(z:list,b:int)->list:
z[1]=b
return z
def stringify(z:list)->str:
return "{}{}{}i".format(get_real(z),"+" if get_img(z)>=0 else "",get_img(z))
def get_modulus(z:list)->float:
return (get_real(z)**2+get_img(z)**2)**(1/2)
@@ -0,0 +1,4 @@
from UI import UI
if __name__ == "__main__":
UI()