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,32 @@
|
||||
# :computer: Assignment 01
|
||||
|
||||
## Requirements
|
||||
- Solve one problem statement from each set
|
||||
- Write the solution to each problem statement in its corresponding Python module (`p1.py`, `p2.py` and `p3.py` respectively).
|
||||
- Use functions, input parameters and return values
|
||||
- Each function only does one thing!
|
||||
- Do not use global variables
|
||||
- Provide the user relevant messages regarding expected input and the meaning of the program’s output.
|
||||
- Assignment should be **completed by week 2, hard deadline is week 3**.
|
||||
|
||||
## Problem Statements
|
||||
### First Set
|
||||
1. Generate the first prime number larger than a given natural number `n`.
|
||||
2. Given natural number `n`, determine the prime numbers `p1` and `p2` such that `n = p1 + p2` (check the Goldbach hypothesis).
|
||||
3. For a given natural number `n` find the minimal natural number `m` formed with the same digits. (e.g. `n=3658, m=3568`).
|
||||
4. For a given natural number `n` find the largest natural number written with the same digits. (e.g. `n=3658, m=8653`).
|
||||
5. Generate the largest prime number smaller than a given natural number `n`. If such a number does not exist, a message should be displayed.
|
||||
|
||||
### Second Set
|
||||
6. Determine a calendar date (as year, month, day) starting from two integer numbers representing the year and the day number inside that year (e.g. day number 32 is February 1st). Take into account leap years. Do not use Python's inbuilt date/time functions.
|
||||
7. Determine the twin prime numbers `p1` and `p2` immediately larger than the given non-null natural number `n`. Two prime numbers `p` and `q` are called twin if `q - p = 2`.
|
||||
8. Find the smallest number `m` from the Fibonacci sequence, defined by `f[0]=f[1]=1`, `f[n]=f[n-1] + f[n-2]`, for `n > 2`, larger than the given natural number `n`. (e.g. `for n = 6, m = 8`).
|
||||
9. Consider a given natural number `n`. Determine the product `p` of all the proper factors of `n`.
|
||||
10. The palindrome of a number is the number obtained by reversing the order of its digits (e.g. the `palindrome of 237 is 732`). For a given natural number `n`, determine its palindrome.
|
||||
11. The numbers `n1` and `n2` have the property `P` if their writing in base 10 uses the same digits (e.g. `2113 and 323121`). Determine whether two given natural numbers have property `P`.
|
||||
|
||||
### Third Set
|
||||
12. Determine the age of a person, in number of days. Take into account leap years, as well as the date of birth and current date `(year, month, day)`. Do not use Python's inbuilt date/time functions.
|
||||
13. Determine the `n-th` element of the sequence `1,2,3,2,5,2,3,7,2,3,2,5,...` obtained from the sequence of natural numbers by replacing composed numbers with their prime divisors, without memorizing the elements of the sequence.
|
||||
14. Determine the `n-th` element of the sequence `1,2,3,2,2,5,2,2,3,3,3,7,2,2,3,3,3,...` obtained from the sequence of natural numbers by replacing composed numbers with their prime divisors, each divisor `d` being written `d` times, without memorizing the elements of the sequence.
|
||||
15. Generate the largest perfect number smaller than a given natural number `n`. If such a number does not exist, a message should be displayed. A number is perfect if it is equal to the sum of its divisors, except itself. (e.g. `6 is a perfect number, as 6=1+2+3`).
|
||||
@@ -0,0 +1,22 @@
|
||||
# Solve the problem from the first set here
|
||||
|
||||
def is_prime(n):
|
||||
if n<2:
|
||||
return False
|
||||
for i in range(2,round(n**0.5)+1):
|
||||
if n%i==0:
|
||||
return False
|
||||
return True
|
||||
|
||||
def largest_prime_number_smaller_than_n(n):
|
||||
if n < 3:
|
||||
print("No such number exists")
|
||||
return None
|
||||
for i in range(n-1,0,-1):
|
||||
if is_prime(i):
|
||||
print("The smallest prime number smaller than {} is {}".format(n,i))
|
||||
return i
|
||||
|
||||
if __name__=="__main__":
|
||||
n=int(input("Please enter an positive integer number: "))
|
||||
largest_prime_number_smaller_than_n(n)
|
||||
@@ -0,0 +1,15 @@
|
||||
# Solve the problem from the second set here
|
||||
|
||||
def fibonacci(n):
|
||||
f1=1
|
||||
f2=1
|
||||
while n>=f2:
|
||||
aux=f2
|
||||
f2+=f1
|
||||
f1=aux
|
||||
print(f2)
|
||||
return f2
|
||||
|
||||
if __name__=="__main__":
|
||||
n=int(input("Please enter an positive integer number: "))
|
||||
fibonacci(n)
|
||||
@@ -0,0 +1,42 @@
|
||||
# Solve the problem from the third set here
|
||||
|
||||
def is_prime(n):
|
||||
if n<2:
|
||||
return False
|
||||
for i in range(2,round(n**0.5)+1):
|
||||
if n%i==0:
|
||||
return False
|
||||
return True
|
||||
|
||||
def the_nth_element_of_sequence(n):
|
||||
if n<=0:
|
||||
print("Number should be an positive integer")
|
||||
return None
|
||||
if n==1:
|
||||
print("The nth number in the sequence is: 1")
|
||||
return 1
|
||||
i=1
|
||||
n-=1
|
||||
while n>0:
|
||||
i+=1
|
||||
if is_prime(i):
|
||||
n-=1
|
||||
continue
|
||||
d=2
|
||||
j=i
|
||||
while j>1:
|
||||
if j%d==0:
|
||||
n-=d
|
||||
while j%d==0:
|
||||
j//=d
|
||||
if n<=0:
|
||||
i=d
|
||||
break
|
||||
d+=1
|
||||
if n<=0:
|
||||
print("The nth number in the sequence is: {}".format(i))
|
||||
return i
|
||||
|
||||
if __name__=="__main__":
|
||||
n=int(input("Please enter an positive integer number: "))
|
||||
the_nth_element_of_sequence(n)
|
||||
Reference in New Issue
Block a user