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,38 @@
# :computer: Assignment 04 - Problem Solving Methods
## Requirements
- You will have to solve two problem statements from the list below, one using the **backtracking** programming method and one using the **dynamic programming** method.
- For the backtracking problem, implement both an iterative as well as a recursive algorithm (**deadline is week 5**).
- For the dynamic programming problem, implement both the naive, non-optimized version as well as the dynamic programming version (**deadline is week 6**).
- For the dynamic programming implementation, display the data structure used to memorize the intermediate results and be able to explain how it works.
- For all implementations understand and be able to explain the computational complexity with regards to runtime.
## Problem Statements
### Backtracking
1. A number of `n` coins are given, with values of a<sub>1</sub>, ..., a<sub>n</sub> and a value `s`. Display all payment modalities for the sum `s`. If no payment modality exists print a message.
2. Consider a positive number `n`. Determine all its decompositions as sums of prime numbers.
3. The sequence a = a<sub>1</sub>, ..., a<sub>n</sub> with integer elements is given. Determine all strictly increasing subsequences of sequence `a` (conserve the order of elements in the original sequence).
4. A player at `PRONOSPORT` wants to choose score options for four games. The options may be `1`, `X`, `2`. Generate all possible alternatives, knowing that:
- The last score option may not be `X`
- There should be no more than two score options of `1`
5. The sequence a = a<sub>1</sub>, ..., a<sub>n</sub> with distinct integer numbers is given. Determine all subsets of elements having the sum divisible by a given `n`.
6. Generate all sequences of `n` parentheses that close correctly. Example: for `n=4` there are two solutions: `(())` and `()()`
7. Generate all subsequences of length `2n+1`, formed only by `0`, `-1` or `1`, such that a<sub>1</sub> = 0, ..., a<sub>2n+1</sub>= 0 and |a<sub>i+1</sub> - a<sub>i</sub>| = 1 or 2, for any 1 ≤ i ≤ 2n.
8. Consider `n` points in a plane, given by their coordinates. Determine all subsets with at least three elements formed by collinear points. If the problem has no solution, give a message.
9. The sequence a = a<sub>1</sub>, ..., a<sub>n</sub> with distinct integer elements is given. Determine all subsets of at least two elements with the property:
- The elements in the subset are in increasing order
- Any two consecutive elements in the subsequence have at least one common digit
10. A group of `n` (n<=10) persons, numbered from `1` to `n` are placed on a row of chairs, but between every two neighbor persons (e.g. persons 3 and 4, or persons 7 and 8) some conflicts appeared. Display all the possible modalities to replace the persons, such that between any two persons in conflict stay one or at most two other persons.
11. Two natural numbers `m` and `n` are given. Display in all possible modalities the numbers from `1` to `n`, such that between any two numbers on consecutive positions, the difference in absolute value is at least `m`. If there is no solution, display a message.
12. Consider the natural number `n` (n<=10) and the natural numbers a<sub>1</sub>, ..., a<sub>n</sub>. Determine all the possibilities to insert between all numbers a<sub>1</sub>, ..., a<sub>n</sub> the operators `+` and `` such that by evaluating the expression the result is positive.
13. The sequence a<sub>1</sub>, ..., a<sub>n</sub> of distinct integer numbers is given. Display all subsets with a mountain aspect. A set has a mountain aspect if the elements increase up to a point and then they decrease. E.g. `10, 16, 27, 18, 14, 7`.
14. Generate all numbers of `n` digits with the property that no number has two identical neighboring subsequences. For example, for `n=6`, `121312` is correct, and `121313` and `132132` are not correct.
### Dynamic Programming
1. Determine the longest common subsequence of two given sequences. Subsequence elements are not required to occupy consecutive positions. For example, if `X = "MNPNQMN"` and `Y = "NQPMNM"`, the longest common subsequence has length `4`, and can be one of `"NQMN"`, `"NPMN"` or `"NPNM"`. Determine and display both the length of the longest common subsequence as well as at least one such subsequence.
2. Given the set of positive integers `S` and the natural number `k`, display one of the subsets of `S` which sum to `k`. For example, if `S = { 2, 3, 5, 7, 8 }` and `k = 14`, subset `{ 2, 5, 7 }` sums to `14`.
3. Given the set of positive integers `S`, partition this set into two subsets `S1` and `S2` so that the difference between the sum of the elements in `S1` and `S2` is minimal. For example, for set `S = { 1, 2, 3, 4, 5 }`, the two subsets could be `S1 = { 1, 2, 4 }` and `S2 = { 3, 5 }`. Display at least one of the solutions.
4. Given an `n * n` square matrix with integer values, find the maximum length of a snake sequence. A snake sequence begins on the matrix's top row (coordinate `(0, i), 0 <= i < n`). Each element of the sequence, except the first one, must have a value `±1` from the previous one and be located directly below, or directly to the right of the previous element. For example, element `(i, j)` can be succeded by one of the `(i, j + 1)` or `(i + 1, j)` elements. Display the length as well as the sequence of coordinates for one sequence of maximum length.
5. Maximize the profit when selling a rod of length `n`. The rod can be cut into pieces of integer lengths and pieces can be sold individually. The prices are known for each possible length. For example, if rod length `n = 7`, and the price array is `price = [1, 5, 8, 9, 10, 17, 17]` (the price of a piece of length `3` is `8`), the maximum profit is `18`, and is obtained by cutting the rod into 3 pieces, two of length two and one of length 3. Display the profit and the length of rod sections sold to obtain it.
6. Given an array of integers `A`, maximize the value of the expression `A[m] - A[n] + A[p] - A[q]`, where `m, n, p, q` are array indices with `m > n > p > q`. For `A = [30, 5, 15, 18, 30, 40]`, the maximum value is `32`, obtained as `40 - 18 + 15 - 5`. Display both the maximum value as well as the expression used to calculate it.
7. Given a set of integers `A`, determine if it can be partitioned into two subsets with equal sum. For example, set `A = { 1, 1, 1, 1, 2, 3, 5 }` can be partitioned into sets `A1 = { 1, 1, 2, 3 }` and `A2 = { 1, 1, 5 }`, each of them having sum `7`. Display one such possibility.
@@ -0,0 +1,99 @@
# 2.Consider a positive number n. Determine all its decompositions as sums of prime numbers.
import math
def is_prime(n:int)->bool:
"""
params: n - Integer
return: Boolean
Returns True if n is prime, False otherwise.
"""
if n<2:
return False
for i in range(2,math.floor(n**(1/2))+1):
if n%i==0:
return False
return True
def primes_smaller_or_equal_to_n(n:int)->list:
"""
params: n - Integer
return: List
Returns the list of all prime numbers smaller or equal to n.
"""
primes=[]
for i in range(2,n+1):
if is_prime(i):
primes.append(i)
return primes
def iterative(n):
"""
params: n - Positive Integer
return: List
Returns every decompositions of the number n in a list of lists, each set containing the primes.
If no such decomposition exists, returns an empty list.
The computation is done in an iterative way.
"""
list_of_primes=primes_smaller_or_equal_to_n(n)
count_stack=[]
count=len(list_of_primes)
solution=[]
current_solution=[]
while count:
number=list_of_primes[count-1]
count-=1
if sum(current_solution)+number < n:
current_solution.append(number)
count_stack.append(count)
count=len(list_of_primes)
elif sum(current_solution)+number == n:
current_solution.append(number)
c=sorted(current_solution)
if c not in solution:
solution.append(c)
current_solution.pop()
while count_stack and count==0:
current_solution.pop()
count=count_stack.pop()
return solution
def recursive(n,current_solution,list_of_primes):
"""
params: n - Positive Integer
return: List
Returns every decompositions of the number n in a list of lists, each set containing the primes.
If no such decomposition exists, returns an empty list.
The computation is done in a recursive way
"""
solution=[]
if sum(current_solution)<n:
for i in list_of_primes:
c=current_solution+[i]
s=recursive(n,c,list_of_primes)
if s!=[]:
if isinstance(s[0],list):
for e in s:
if e not in solution:
solution.append(e)
else:
if s not in solution:
solution.append(s)
return solution
elif sum(current_solution)==n:
return sorted(current_solution)
else:
return []
def recursive_imp(n):
return recursive(n,[],list_of_primes=primes_smaller_or_equal_to_n(n))
if __name__ == "__main__":
n=int(input("Please enter a number: "))
print("Recursive: ")
print(recursive_imp(n))
print("Iterative:")
print(iterative(n))
@@ -0,0 +1,35 @@
# 3.Given the set of positive integers S, partition this set into two subsets S1 and S2 so that the difference between
# the sum of the elements in S1 and S2 is minimal. For example, for set S = { 1, 2, 3, 4, 5 },
# the two subsets could be S1 = { 1, 2, 4 } and S2 = { 3, 5 }. Display at least one of the solutions.
def naive(s,s1,s2):
if len(s)==0:
return [s1,s2]
x=s.pop()
x1=naive(s.copy(),s1+[x],s2)
x2=naive(s.copy(),s1,s2+[x])
if abs(sum(x1[0])-sum(x1[1]))<abs(sum(x2[0])-sum(x2[1])):
return x1
else:
return x2
def naive_imp(s):
return naive(s.copy(),[],[])
def dynamic(s):
half_sum=sum(s)//2+1
data=[[False,[]] for _ in range(half_sum)]
data[0]=[True,[]]
for i in s:
for j in range(half_sum):
if data[j][0] and j+i<half_sum and (i not in data[j][1]):
data[j+i]=[True,data[j][1]+[i]]
for i in range(half_sum-1,0,-1):
if data[i][0]:
return [data[i][1],[j for j in s if j not in data[i][1]]]
if __name__ == "__main__":
s=[1,2,3,4,5]
print("Naive: {}".format(naive_imp(s)))
print("Dynamic: {}".format(dynamic(s)))