Exercicios da aula04 adicionados

This commit is contained in:
tiagorg 2022-10-14 23:03:25 +01:00
parent 46b5f5c6c4
commit 5680b2c4b0
Signed by untrusted user who does not match committer: TiagoRG
GPG Key ID: DFCD48E3F420DB42
14 changed files with 176 additions and 1 deletions

5
1ano/fp/aula01/README.md Normal file
View File

@ -0,0 +1,5 @@
# Fundamentos de Programação
## Aula 01
### Tópico principal da aula: Introduction, Basics
---
*Pode conter erros, caso encontre algum, crie um* [*ticket*](https://github.com/TiagoRG/uaveiro-leci/issues/new)

5
1ano/fp/aula02/README.md Normal file
View File

@ -0,0 +1,5 @@
# Fundamentos de Programação
## Aula 02
### Tópico principal da aula: Conditionals, Boolean expressions
---
*Pode conter erros, caso encontre algum, crie um* [*ticket*](https://github.com/TiagoRG/uaveiro-leci/issues/new)

5
1ano/fp/aula03/README.md Normal file
View File

@ -0,0 +1,5 @@
# Fundamentos de Programação
## Aula 03
### Tópico principal da aula: Functions, Lambda expressions
---
*Pode conter erros, caso encontre algum, crie um* [*ticket*](https://github.com/TiagoRG/uaveiro-leci/issues/new)

5
1ano/fp/aula04/README.md Normal file
View File

@ -0,0 +1,5 @@
# Fundamentos de Programação
## Aula 04
### Tópico principal da aula: Iteration, Loops
---
*Pode conter erros, caso encontre algum, crie um* [*ticket*](https://github.com/TiagoRG/uaveiro-leci/issues/new)

View File

@ -0,0 +1,39 @@
def divList(n):
divs = []
for x in range(1, n):
if n % x == 0:
divs.append(x)
return divs
def category(n):
total = 0
for div in divList(n):
total += div
if total < n: return 'deficiente'
if total == n: return 'perfeito'
if total > n: return 'abundante'
def main():
n = int(input('Introduza um número: '))
div_list = ""
div_list_array = divList(n)
for div in div_list_array:
div_list += str(div)
if div != div_list_array[len(div_list_array)-1]:
div_list += ', '
print("""
--------------------
Número introduzido: {}
Lista de divisores:
{}
Este é um número {}.
--------------------
""".format(n, div_list, category(n)))
main()

View File

@ -0,0 +1,11 @@
def factorial(n):
total = 1
for x in range(1, n + 1):
total *= x
return total
def main():
n = int(input('Introduza um número: '))
print('O fatorial de {} é: {}'.format(n, factorial(n)))
main()

View File

@ -0,0 +1,10 @@
def fibonacci(n):
if n == 0: return 0
if n == 1: return 1
return fibonacci(n-1) + fibonacci(n-2)
def main():
n = int(input('Introduza um número: '))
print('O {}º número de Fibonacci é: {}'.format(n, fibonacci(n)))
main()

21
1ano/fp/aula04/hilo.py Normal file
View File

@ -0,0 +1,21 @@
# Complete the code to make the HiLo game...
import random
def main():
# Pick a random number between 1 and 100, inclusive
secret = random.randrange(1, 101)
print("Can you guess my secret?")
# put your code here
c = 0
num = -1
while secret != num:
num = int(input('>>> '))
if num > secret:
print('High')
if num < secret:
print('Low')
c += 1
print('Well done! The secret number was {}. It took you {} tries to get it right.'.format(secret, c))
main()

15
1ano/fp/aula04/isprime.py Normal file
View File

@ -0,0 +1,15 @@
def isPrime(n):
div_counter = 0
for x in range(1, n):
if n % x == 0:
div_counter += 1
if div_counter > 1 or n == 1:
return False
else:
return True
def main():
for x in range(1, 100):
print('Is {} prime? {}'.format(x, isPrime(x)))
main()

View File

@ -0,0 +1,20 @@
import math
def leibnizPi4(n):
total = 0
for x in range(1, n+1):
if x % 2 == 0:
total -= 1/(x*2-1)
else:
total += 1/(x*2-1)
return total
def main():
num = int(input('Introduza o número de termos: '))
print("""
Resultado da série de Leibniz: {}
Valor do PI/4: {}
""".format(leibnizPi4(num), math.pi/4))
main()

21
1ano/fp/aula04/media.py Normal file
View File

@ -0,0 +1,21 @@
values = []
def GetValues():
c = 1
while True:
n = input('n{}: '.format(c))
if n == "": break
values.append(float(n))
c += 1
def GetMedia(val):
total = 0
for v in val:
total += v
return total / len(val)
def main():
GetValues()
print('Média dos valores introduzidos: ', GetMedia(values))
main()

View File

@ -0,0 +1,10 @@
# This program generates 20 terms of a sequence by a recurrence relation.
Un = 100 # Un = each term of the sequence. Initially = U0
c = 0
while Un > 0:
print(round(Un, 4))
Un = 1.01*Un - 1.01 # Set Un to the next term of the sequence
c += 1
print('O programa mostrou ', c, ' termos')

7
1ano/fp/aula04/table.py Normal file
View File

@ -0,0 +1,7 @@
# Show a table of the squares of the first four numbers
print(" {:2s}| {:2s}| {:2s}".format("n", "", "2**n"))
for n in range(1, 21):
print("{:2d} | {:3d} |{:8d}".format(n, n**2, 2**n))
# Modify the program to show the squares of 1..20. (Use the range function.)
# Also, add a column to show 2**n. Adjust the formatting.

View File

@ -1,5 +1,6 @@
# UAveiro - LECI
### Todo o material de Licenciatura em Engenharia de Computatores e Informática [Free for Use]
### Todo o material de Licenciatura em Engenharia de Computatores e Informática
### [Free for Use]
#### 1º Ano em 2022/2023
---
*Pode conter erros, caso encontre algum, crie um* [*ticket*](https://github.com/TiagoRG/uaveiro-leci/issues/new)