commit ac9b66a10ebf1f0a9ee4ff3120275dbc459af525 Author: tiagorg Date: Tue Oct 11 20:27:51 2022 +0100 First Commit diff --git a/1ano/fp/aula01/ex02.py b/1ano/fp/aula01/ex02.py new file mode 100644 index 0000000..12d5717 --- /dev/null +++ b/1ano/fp/aula01/ex02.py @@ -0,0 +1,5 @@ +celcius = float(input('°C: ')) + +fahrenheit = 1.8 * celcius + 32 + +print('\n{} °C = {} °F'.format(celcius, fahrenheit)) \ No newline at end of file diff --git a/1ano/fp/aula01/ex03.py b/1ano/fp/aula01/ex03.py new file mode 100644 index 0000000..fa95694 --- /dev/null +++ b/1ano/fp/aula01/ex03.py @@ -0,0 +1,6 @@ +v1 = float(input('Velocidade de ida (v1): ')) +v2 = float(input('Velocidade de regresso (v2): ')) + +vm = (2 * v1 * v2) / (v1 + v2) + +print('\nA velocidade média da viagem completa foi: ', vm) \ No newline at end of file diff --git a/1ano/fp/aula01/ex04.py b/1ano/fp/aula01/ex04.py new file mode 100644 index 0000000..b77d4b5 --- /dev/null +++ b/1ano/fp/aula01/ex04.py @@ -0,0 +1,8 @@ +import calendar +from datetime import datetime; + +nome = input('Como te chamas? ') +anoNascenca = int(input('Em que ano nasceste? ')) +ano = int(input('Ano para verificar: ')) + +print('{}, em {} farás {} anos.'.format(nome, ano, ano - anoNascenca)) \ No newline at end of file diff --git a/1ano/fp/aula01/ex05.py b/1ano/fp/aula01/ex05.py new file mode 100644 index 0000000..9363d64 --- /dev/null +++ b/1ano/fp/aula01/ex05.py @@ -0,0 +1,8 @@ +secs = int(input('Segundos a converter para horas, minutos e segundos? ')) +s = secs % 60 +mins = secs // 60 +m = mins % 60 +h = mins // 60 + + +print("{:02d}:{:02d}:{:02d}".format(h, m, s)) \ No newline at end of file diff --git a/1ano/fp/aula01/ex06.py b/1ano/fp/aula01/ex06.py new file mode 100644 index 0000000..03d73d9 --- /dev/null +++ b/1ano/fp/aula01/ex06.py @@ -0,0 +1,10 @@ +viagensDia = 2 * (1 + 2 + 3) +viagensAno = viagensDia * 365 + +mAno = viagensAno * 3 +kmAno = mAno + +secsAno = mAno +hAno = secsAno / 3600 + +print('O elevador anda {} kilometros por ano, durante {} horas.'.format(kmAno, hAno)) diff --git a/1ano/fp/aula01/ex07.py b/1ano/fp/aula01/ex07.py new file mode 100644 index 0000000..bdd6d24 --- /dev/null +++ b/1ano/fp/aula01/ex07.py @@ -0,0 +1,13 @@ +from math import * + + +A = float(input('Comprimento do cateto A: ')) +B = float(input('Comprimento do cateto B: ')) + +C = sqrt(A**2 + B**2) + +cosseno = A / C +angRad = acos(cosseno) +angDeg = angRad * 180 / pi + +print('O comprimento da hipotenusa é {} e o valor do angulo entre o cateto A e a hipotenusa é {}°'.format(round(C, 2), round(angDeg, 2))) \ No newline at end of file diff --git a/1ano/fp/aula01/ex08.py b/1ano/fp/aula01/ex08.py new file mode 100644 index 0000000..49d9dae --- /dev/null +++ b/1ano/fp/aula01/ex08.py @@ -0,0 +1,14 @@ +from math import sqrt + + +x1, y1 = input("Introduza x1 e y1, separados por uma virgula ',': ").split(',') +x2, y2 = input("Introduza x2 e y2, separados por uma virgula ',': ").split(',') + +x1 = float(x1) +y1 = float(y1) +x2 = float(x2) +y2 = float(y2) + +distancia = sqrt((x2 - x1)**2 + (y2 - y1)**2) + +print('A distancia entre os dois pontos é: ', distancia) \ No newline at end of file diff --git a/1ano/fp/aula02/age.py b/1ano/fp/aula02/age.py new file mode 100644 index 0000000..b5b4375 --- /dev/null +++ b/1ano/fp/aula02/age.py @@ -0,0 +1,25 @@ + +# A teenager is a person between 13 and 19 years old, inclusive. +# A child is under 13. A grown-up is 20 or more. +# This program outputs the age category for a given input age. +# It has a semantic error. Can you find it? +# Which values of age produce the output "grown-up"? +# Correct the error. +# Can you simplify the code to avoid redundant conditions? + +age = int(input("Age? ")) + +if age < 0: + print("ERROR: invalid age!") + exit(1) # this terminates the program + +print("Age:", age) + +if age < 13 : + cat = "child" +elif 13 < age < 20: + cat = "teenager" +else: + cat = "grown-up" + +print("Category: ", cat) \ No newline at end of file diff --git a/1ano/fp/aula02/darts.cs b/1ano/fp/aula02/darts.cs new file mode 100644 index 0000000..2cc588c --- /dev/null +++ b/1ano/fp/aula02/darts.cs @@ -0,0 +1,129 @@ +// .NET core 6.0 + +namespace Darts +{ + internal class Program + { + static void Main(string[] args) + { + Console.WriteLine("Introduza as coordenadas (x, y) do dardo.\nRepresentam as posições horizontal e vertical respetivamente.\nAmbas em milimetros."); + Console.Write("X: "); + int x = Convert.ToInt32(Console.ReadLine()); + Console.Write("Y: "); + int y = Convert.ToInt32(Console.ReadLine()); + + double mod = Math.Sqrt(Math.Pow(x, 2) + Math.Pow(y, 2)); + + if (mod > 170) + { + Console.WriteLine("Fora do alvo."); + Console.ReadKey(); + return; + } + + if (mod < 12.7) + { + Console.WriteLine("Pontuação: 50 points"); + Console.ReadKey(); + return; + } + else if (mod < 32) + { + Console.WriteLine("Pontuação: 25 points"); + Console.ReadKey(); + return; + } + + int basePoint = BasePoint(x, y); + + if (mod > 99 && mod < 107) + { + Console.WriteLine($"Pontuação: {basePoint * 3} points"); + Console.ReadKey(); + return; + } + if (mod > 162) + { + Console.WriteLine($"Pontuação: {basePoint * 2} points"); + Console.ReadKey(); + return; + } + + Console.WriteLine($"Pontuação: {basePoint} points"); + Console.ReadKey(); + return; + } + + public static int BasePoint(int x, int y) + { + if (x > 0) + { + if (Math.Abs(y) < x * Math.Tan(Math.PI / 20)) + return 6; + else + { + if (y > 0) + { + if (y < x * Math.Tan(3 * Math.PI / 20)) + return 13; + if (y < x * Math.Tan(5 * Math.PI / 20)) + return 4; + if (y < x * Math.Tan(7 * Math.PI / 20)) + return 18; + if (y < x * Math.Tan(9 * Math.PI / 20)) + return 1; + else + return 20; + } + else + { + if (y > x * Math.Tan(-3 * Math.PI / 20)) + return 10; + if (y > x * Math.Tan(-5 * Math.PI / 20)) + return 15; + if (y > x * Math.Tan(-7 * Math.PI / 20)) + return 2; + if (y > x * Math.Tan(-9 * Math.PI / 20)) + return 17; + else + return 3; + } + } + } + else + { + if (Math.Abs(y) < x * Math.Tan(Math.PI + Math.PI / 20)) + return 11; + else + { + if (y > 0) + { + if (y < x * Math.Tan(Math.PI + 3 * Math.PI / 20)) + return 14; + if (y < x * Math.Tan(Math.PI + 5 * Math.PI / 20)) + return 9; + if (y < x * Math.Tan(Math.PI + 7 * Math.PI / 20)) + return 12; + if (y < x * Math.Tan(Math.PI + 9 * Math.PI / 20)) + return 5; + else + return 20; + } + else + { + if (y > x * Math.Tan(Math.PI + -3 * Math.PI / 20)) + return 8; + if (y > x * Math.Tan(Math.PI + -5 * Math.PI / 20)) + return 16; + if (y > x * Math.Tan(Math.PI + -7 * Math.PI / 20)) + return 7; + if (y > x * Math.Tan(Math.PI + -9 * Math.PI / 20)) + return 19; + else + return 3; + } + } + } + } + } +} \ No newline at end of file diff --git a/1ano/fp/aula02/darts.py b/1ano/fp/aula02/darts.py new file mode 100644 index 0000000..9423b16 --- /dev/null +++ b/1ano/fp/aula02/darts.py @@ -0,0 +1,88 @@ +import math + +def BasePoint(x, y): + if x > 0: + if abs(y) < x * math.tan(math.pi / 20): + return 6 + else: + if y > 0: + if y < x * math.tan(3 * math.pi / 20): + return 13 + if y < x * math.tan(5 * math.pi / 20): + return 4 + if y < x * math.tan(7 * math.pi / 20): + return 18 + if y < x * math.tan(9 * math.pi / 20): + return 1 + else: + return 20 + else: + if y > x * math.tan(-3 * math.pi / 20): + return 10 + if y > x * math.tan(-5 * math.pi / 20): + return 15 + if y > x * math.tan(-7 * math.pi / 20): + return 2 + if y > x * math.tan(-9 * math.pi / 20): + return 17 + else: + return 3 + else: + if abs(y) < x * math.tan(math.pi + math.pi / 20): + return 6 + else: + if y > 0: + if y < x * math.tan(math.pi + 3 * math.pi / 20): + return 14 + if y < x * math.tan(math.pi + 5 * math.pi / 20): + return 9 + if y < x * math.tan(math.pi + 7 * math.pi / 20): + return 12 + if y < x * math.tan(math.pi + 9 * math.pi / 20): + return 5 + else: + return 20 + else: + if y > x * math.tan(math.pi + -3 * math.pi / 20): + return 8 + if y > x * math.tan(math.pi + -5 * math.pi / 20): + return 16 + if y > x * math.tan(math.pi + -7 * math.pi / 20): + return 7 + if y > x * math.tan(math.pi + -9 * math.pi / 20): + return 19 + else: + return 3 + +print("""Introduza as coordenadas (x, y) do dardo. +Representa as posicoes horizontal e vertical respetivamente. +Ambas em milimetros. +""") + +x = int(input('X: ')) +y = int(input('Y: ')) + +mod = math.sqrt(x ** 2 + y ** 2) + +if mod > 170: + print('Fora do alvo.') + exit(1) + +if mod < 12.7: + print('Pontuacao: 50 pontos.') + exit(1) +elif mod < 32: + print('Pontuacao: 25 pontos.') + exit(1) + +base_point = BasePoint(x, y) + +if mod > 99 and mod < 107: + print('Pontuacao: {}pontos.'.format(base_point * 3)) + exit(1) +if mod > 162: + print('Pontuacao: {}pontos.'.format(base_point * 2)) + exit(1) + +print('Pontuacao: {}pontos.'.format(base_point)) +exit(1) \ No newline at end of file diff --git a/1ano/fp/aula02/ex02_03.py b/1ano/fp/aula02/ex02_03.py new file mode 100644 index 0000000..d1d4c4c --- /dev/null +++ b/1ano/fp/aula02/ex02_03.py @@ -0,0 +1,14 @@ +x1 = float(input("number? ")) +x2 = float(input("number? ")) +x3 = float(input("number? ")) +x4 = float(input("number? ")) +mx = x4 + +if x1 > mx: + mx = x1 +if x2 > mx: + mx = x2 +if x3 > mx: + mx = x3 + +print("Maximum:", mx) \ No newline at end of file diff --git a/1ano/fp/aula02/ex04.py b/1ano/fp/aula02/ex04.py new file mode 100644 index 0000000..52fa284 --- /dev/null +++ b/1ano/fp/aula02/ex04.py @@ -0,0 +1,6 @@ +n = int(input('numero? ')) + +if n % 2 == 0: + print('par') +else: + print('impar') \ No newline at end of file diff --git a/1ano/fp/aula02/ex05.py b/1ano/fp/aula02/ex05.py new file mode 100644 index 0000000..54c934a --- /dev/null +++ b/1ano/fp/aula02/ex05.py @@ -0,0 +1,10 @@ +tarifario_sec = 0.12 / 60 + +duracao = float(input('Introduza a duracao da chamada em segundos: ')) + +if duracao < 60: + custo = 0.12 +else: + custo = duracao * tarifario_sec + +print('Custo: ', custo) \ No newline at end of file diff --git a/1ano/fp/aula02/ex09.py b/1ano/fp/aula02/ex09.py new file mode 100644 index 0000000..ab09dc5 --- /dev/null +++ b/1ano/fp/aula02/ex09.py @@ -0,0 +1,19 @@ +CTP = float(input('Componente Teorica-pratica: ')) +CP = float(input('Componente Pratica: ')) + + +NF = round(0.3 * CTP + 0.7 * CP) + +if (CTP < 6.6) or (CP < 6.6): + print('Reprovado por nota minima!\n') +elif NF >= 10: + print('Aprovado com nota {}'.format(NF)) + exit(1) + +ATPR = float(input('Componente Teorica-pratica de recurso: ')) +ATP = float(input('Componente Pratica de recurso: ')) +NF = round(0.3 * ATPR + 0.7 * ATP) +if NF >= 10: + print('Aprovado com nota {}'.format(NF)) +else: + print('Continua reprovado, nota ', NF) \ No newline at end of file diff --git a/1ano/fp/aula02/imc.py b/1ano/fp/aula02/imc.py new file mode 100644 index 0000000..3a11fd0 --- /dev/null +++ b/1ano/fp/aula02/imc.py @@ -0,0 +1,26 @@ +# -*- coding: utf-8 -*- + +# Modifique o programa para indicar a categoria de IMC de acordo com a tabela: +# IMC: <18.5 [18.5, 25[ [25, 30[ 30 ou mais +# Categoria: Magro Saudável Forte Obeso + +print("Índice de Massa Corporal") + +altura = float(input("Altura (m)? ")) +peso = float(input("Peso (kg)? ")) + +imc = peso / altura**2 + +print("IMC:", imc, "kg/m2") + +# Determinar a categoria de IMC: +if imc < 18.5: + categoria = "Magro" +elif imc < 25: + categoria = "Saudavel" +elif imc < 30: + categoria = "Forte" +else: + categoria = "Obeso" + +print("Categoria:", categoria) \ No newline at end of file diff --git a/1ano/fp/aula02/kryptonite.py b/1ano/fp/aula02/kryptonite.py new file mode 100644 index 0000000..2f54424 --- /dev/null +++ b/1ano/fp/aula02/kryptonite.py @@ -0,0 +1,27 @@ +# This program should find the phase of a fictional substance +# for given temperature and pressure conditions, but it has several bugs. +# +# a) Try to run the program with Python3 and see what happens. +# There's a syntax error near the end. Fix it. +# b) Try again. It'll crash with a runtime error. Find the cause and fix it. +# c) There is also a semantic error: for T=300 and P=100, +# the phase should be SOLID. +# Fix it to agree with the phase diagram. Test in several conditions. +# d) Adjust the format string to output the temperature with 1 decimal place +# and the pressure with 3. + +print("Kryptonite phase classifier") +# Input. +T = float(input("Temperature (K)? ")) +P = float(input("Pressure (kPa)? ")) + +# Determine the phase. +if (T > 400) and (P > 50): + phase = "LIQUID" +elif (P > 0.125 * T): + phase = "SOLID" +else: + phase = "GAS" + +# Output. +print("At {:.1f} K and {:.3f} kPa, Kryptonite is in the {} phase.\n\n".format(T, P, phase)) \ No newline at end of file diff --git a/1ano/fp/aula03/bmi.py b/1ano/fp/aula03/bmi.py new file mode 100644 index 0000000..2326a25 --- /dev/null +++ b/1ano/fp/aula03/bmi.py @@ -0,0 +1,62 @@ +""" Exercicio 03. +O programa bmi.py serve para calcular o índice de massa corporal, mas está incompleto. O +programa inclui três funçoes. Analise o seu funcionamento. + +a. Complete a definiçao da funçao bodyMassIndex para calcular o indice pela razao + bmi = weight / height**2. Complete os argumentos na invocacao da funçao, dentro da + funçao principal. Teste o programa. + +b. Complete a funçao bmiCategory para devolver uma string com a categoria correspondente + ao indice de massa corporal dado. Acrescente uma chamada a esta funçao na funçao + principal, para obter o nome da categoria. Volte a testar +""" + +# This function computes the body mass index (BMI), +# given the height (in meter) and weight (in kg) of a person. +def bodyMassIndex(height, weight): + # Complete the function definition... + bmi = weight / height**2 + return bmi + + +# This function returns the BMI category acording to this table: +# BMI: <18.5 [18.5, 25[ [25, 30[ 30 or greater +# Category: Underweight Normal weight Overweight Obesity +def bmiCategory(bmi): + assert bmi>0 + # Complete the function definition... + if bmi < 18.5: + return 'Underweight' + elif bmi < 25: + return 'Normal weight' + elif bmi < 30: + return 'Overweight' + else: + return 'Obesity' + + +# This is the main function +def main(): + print("Índice de Massa Corporal") + + altura = float(input("Altura (m)? ")) + if altura < 0: + print("ERRO: altura inválida!") + exit(1) + + peso = float(input("Peso (kg)? ")) + if peso < 0: + print("ERRO: peso inválido!") + exit(1) + + # Complete the function calls... + imc = bodyMassIndex(altura, peso) + cat = bmiCategory(imc) + + print("BMI: {:.2f} kg/m2".format(imc)) + print("BMI category:", cat) + + +# Program starts executing here +main() + diff --git a/1ano/fp/aula03/dates.py b/1ano/fp/aula03/dates.py new file mode 100644 index 0000000..5906ff1 --- /dev/null +++ b/1ano/fp/aula03/dates.py @@ -0,0 +1,89 @@ +""" Exercicio 09. +Analise e execute o programa dates.py. Faça as correçoes indicadas abaixo. + +a. A funçao isLeapYear deveria indicar quando um ano é bissexto, mas esta errada. + Corrija-a. Um ano e bissexto se for multiplo de 4, com exceçao dos fins de seculo + (multiplos de 100), que so sao bissextos se forem multiplos de 400. Por exemplo: + 1980, 1984, 2004 foram bissextos; 1800 e 1900 foram anos comuns, mas 2000 foi bissexto. + +b. A funçao monthDays, para determinar o numero de dias de um mes, tambem esta errada. + Quando o mes e fevereiro, invoque a funçao anterior para determinar se o ano e + bissexto e devolva 29 dias nesse caso. + +c. Corrija a funçao nextDay para devolver o dia seguinte corretamente. +""" + +# This function checks if year is a leap year. +# It is wrong: 1900 was a common year! +from operator import contains + + +def isLeapYear(year): + if year % 100 == 0: + return year % 400 == 0 + return year%4 == 0 + +# This function has a semantic error: February in a leap year should return 29! +# Correct it. +def monthDays(year, month): + MONTHDAYS = (0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31) + # This tuple contains the days in each month (on a common year). + # For example: MONTHDAYS[3] is the number of days in March. + days = MONTHDAYS[month] + if isLeapYear(year) and month == 2: + days += 1 + return days + +# This is wrong, too. +def nextDay(year, month, day): + months31 = (1, 3, 5, 7, 8, 10) + months30 = (4, 6, 9, 11) + if (month, day) == (12, 31): + year += 1 + month = 1 + day = 1 + elif contains(months31, month) and day == 31: + month += 1 + day = 1 + elif contains(months30, month) and day == 30: + month += 1 + day = 1 + elif month == 2 and day >= 28: + if isLeapYear(year) and day == 29: + day = 1 + month += 1 + if not isLeapYear(year) and day == 28: + day = 1 + month += 1 + else: + day += 1 + + return year, month, day + + +# This is the main function +def main(): + print("Was", 2017, "a leap year?", isLeapYear(2017)) # False? + print("Was", 2016, "a leap year?", isLeapYear(2016)) # True? + print("Was", 2000, "a leap year?", isLeapYear(2000)) # True? + print("Was", 1900, "a leap year?", isLeapYear(1900)) # False? + + print("January 2017 had", monthDays(2017, 1), "days") # 31? + print("February 2017 had", monthDays(2017, 2), "days") # 28? + print("February 2016 had", monthDays(2016, 2), "days") # 29? + print("February 2000 had", monthDays(2000, 2), "days") # 29? + print("February 1900 had", monthDays(1900, 2), "days") # 28? + + y, m, d = nextDay(2017, 1, 30) + print(y, m, d) # 2017 1 31 ? + y, m, d = nextDay(2017, 1, 31) + print(y, m, d) # 2017 2 1 ? + y, m, d = nextDay(2017, 2, 28) + print(y, m, d) # 2017 3 1 ? + y, m, d = nextDay(2016, 2, 29) + print(y, m, d) # 2016 3 1 ? + y, m, d = nextDay(2017, 12, 31) + print(y, m, d) # 2018 1 1 ? + +# call the main function +main() diff --git a/1ano/fp/aula03/ex02.md b/1ano/fp/aula03/ex02.md new file mode 100644 index 0000000..887bf0f --- /dev/null +++ b/1ano/fp/aula03/ex02.md @@ -0,0 +1,55 @@ +#### Exercicios propostos no [CodeCheck](https://horstmann.com/codecheck/index.html) +___ +## Ex 1. +height = 4.5
+width = 3
+volume1 = balloonVolume(width, height)
+print("Original volume: ", volume1)
+volume2 = balloonVolume(width + 1, height + 1)
+change = volume2 - volume1
+print("Change: ", change)
+change = balloonVolume(width + 2, height + 2) - volume2
+print("Change: ", change) +___ +## Ex 2. +def balloonVolume(width, height):
+ pi = 3.1415926
+ a = height / 2
+ c = width / 2
+ volume = 4 * pi * a * c * c / 3
+ return volume +___ +## Ex 3. +def hideCharacters(string) :
+ return "*" * len(string) +___ +## Ex 4. +def isEven(n):
+ if n % 2 == 0:
+  return True
+ else:
+  return False
+
+def main() :
+ page = int(input("Enter page number: "))
+ if isEven(page) :
+  print(page)
+ else :
+  print("%60d" % page)
+
+main() +___ +## Ex 5. +def countSpaces(string):
+ spaces = 0
+ for char in string:
+  if char == " ":
+   spaces += 1
+ return spaces
+
+def main() :
+ userInput = input("Enter a string: ")
+ spaces = countSpaces(userInput)
+ print(spaces)
+
+main() \ No newline at end of file diff --git a/1ano/fp/aula03/ex05_06.py b/1ano/fp/aula03/ex05_06.py new file mode 100644 index 0000000..ded9188 --- /dev/null +++ b/1ano/fp/aula03/ex05_06.py @@ -0,0 +1,31 @@ +""" Exercicio 05. +Defina uma funçao que devolva o maior dos seus dois argumentos. Por exemplo, max2(4, 3) +deve devolver 4 e max2(-3, -2) deve devolver -2. Nao pode usar a funçao pre-definida max. +Use uma instruçao if ou uma expressao condicional. Teste a funçao com varios conjuntos +de argumentos. +""" +""" Exercicio 06. +No mesmo programa, crie uma funçao max3 que devolva o maior dos seus 3 argumentos. Nao +pode usar a funçao max, nem instruçoes ou expressoes condicionais. Recorra apenas a +funçao max2 que definiu atras. Teste a nova funçao. +""" + +def max2(x, y): + if x > y: + return x + else: + return y + +def max3(x, y, z): + return max2(x, max2(y, z)) + +n1 = float(input('Introduza dois valores.\nN1: ')) +n2 = float(input('N2: ')) + +print('\nO maior valor é: ', max2(n1, n2)) + +n1 = float(input('\n\nIntroduza tres valores.\nN1: ')) +n2 = float(input('N2: ')) +n3 = float(input('N3: ')) + +print('\nO maior valor é: ', max3(n1, n2, n3)) \ No newline at end of file diff --git a/1ano/fp/aula03/ex07.py b/1ano/fp/aula03/ex07.py new file mode 100644 index 0000000..490b311 --- /dev/null +++ b/1ano/fp/aula03/ex07.py @@ -0,0 +1,21 @@ +""" Exercicio 07. +Escreva uma funçao, tax(4), que implemente a seguinte funçao de ramos: +tax(r) = { + 0.1r se r <= 1000 + 0.2r - 100 se 1000 < r <= 2000 + 0.3r - 300 se 2000 < r +} +Use uma instruçao if-elif-else e evite condiçoes redundantes. Teste a funçao para +diversos valores de r e confirme os resultados. Que valores deve testar? +""" + + +def tax(r): + if r <= 1000: + return 0.1 * r + if r <= 2000: + return 0.2 * r - 100 + return 0.3 * r - 300 + +x = float(input('R? ')) +print('O valor de tax(r) é: {:.3f}'.format(tax(x))) \ No newline at end of file diff --git a/1ano/fp/aula03/ex08.py b/1ano/fp/aula03/ex08.py new file mode 100644 index 0000000..bc42752 --- /dev/null +++ b/1ano/fp/aula03/ex08.py @@ -0,0 +1,22 @@ +""" Exercicio 08. +Escreva uma funçao intersects(a1, b1, a2, b2) que devolva True se os intervalos +[a1, b1[ e [a2, b2[ se intersectarem e devolva False, caso contrario. Pode admitir que +a1 < b1 e a2 < b2. +""" + + +def intersects(a1, b1, a2, b2): + assert a1 < b1 + assert a2 < b2 + + if a1 < b2 and a2 < b1: + return True + else: + return False + +a1 = float(input("a1: ")) +b1 = float(input("b1: ")) +a2 = float(input("a2: ")) +b2 = float(input("b2: ")) + +print(intersects(a1, b1, a2, b2)) \ No newline at end of file diff --git a/1ano/fp/aula03/ex10_11.md b/1ano/fp/aula03/ex10_11.md new file mode 100644 index 0000000..8eed5cf --- /dev/null +++ b/1ano/fp/aula03/ex10_11.md @@ -0,0 +1,13 @@ +#### Exercicios propostos no [CodeCheck](https://horstmann.com/codecheck/index.html) +___ +## Ex 10. +def hms2sec(h, m, s):
+ sec = h * 3600 + m * 60 + s + return sec +___ +## Ex 11. +def sec2hms(sec):
+ h = sec // 3600
+ m = (sec // 60) % 60
+ s = sec % 60
+ return h, m, s \ No newline at end of file diff --git a/1ano/fp/aula03/ex12.py b/1ano/fp/aula03/ex12.py new file mode 100644 index 0000000..dfdfd63 --- /dev/null +++ b/1ano/fp/aula03/ex12.py @@ -0,0 +1,17 @@ +""" Exercicio 12. +Escreva uma funçao countdown (N) que imprima uma contagem decrescente a partir de um +numero positivo N. Note que pode imprimir N e depois chamar countdown (N - 1). +Teste a funçao com diversos valores de N. +""" + +def main(): + num = int(input('De onde vai começar o contador? ')) + countdown(num) + +def countdown(n): + assert n > 0 + print(n) + if n > 0: + countdown(n - 1) + +main() \ No newline at end of file diff --git a/1ano/fp/aula03/ex13.py b/1ano/fp/aula03/ex13.py new file mode 100644 index 0000000..db930fa --- /dev/null +++ b/1ano/fp/aula03/ex13.py @@ -0,0 +1,27 @@ +""" Exercicio 13. +O algoritmo de Euclides serve para determinar o maximo divisor comum de dois numeros +naturais. Baseia-se na igualdade seguinte: +mdc(a, b) = { + b se r = 0 + mdc(b, r) se r > 0 +} +onde 'r' e o resto da divisao de 'a' por 'b'. Escreva uma funçao para calcular o +m.d.c. e teste-a com diversos pares de valores. +""" + +def mdc(a, b): + assert a > 0 + assert b > 0 + r = a % b + if r == 0: + return b + else: + return mdc(b, r) + +def main(): + print('Este programa calcula o maximo divisor comum de dois numeros naturais') + n1 = int(input('Numero 1: ')) + n2 = int(input('Numero 2: ')) + print('\nMaximo divisor comum de \'{}\' e \'{}\': {}'.format(n1, n2, mdc(n1, n2))) + +main() \ No newline at end of file diff --git a/1ano/fp/aula03/poly.py b/1ano/fp/aula03/poly.py new file mode 100644 index 0000000..5af107b --- /dev/null +++ b/1ano/fp/aula03/poly.py @@ -0,0 +1,31 @@ +""" Exercicio 04. +Execute e analise o programa poly.py. Acrescente-lhe uma funçao para calcular o polinomio +p(x) = x**2 + 2x + 3 e modifique a funçao main para mostrar os valores de p(1), p(2), +p(10) e g(1 + p(3)). Confira os resultados. +""" + + +# Esta função implementa g(x) = 8 - x**3 +def g(x): + return 8 - x**3 + +# Defina uma função que implemente p(x) = x**2 + 2x + 3 +def p(x): + return x**2 + 2*x + 3 + +def main(): + # Mostra alguns valores da função g: + print("g(1) =", g(1)) + print("g(2) =", g(2)) + print("g(10) =", g(10)) + + # Acrescente instruções para mostrar os valores de + # p(1), p(2), p(10) e g(1 + p(3)). + print("""p(1) = {} +p(2) = {} +p(10) = {} +g(1 + p(3)) = {}""".format(p(1), p(2), p(10), g(1 + p(3)))) + +if __name__ == '__main__': + main() + diff --git a/README.md b/README.md new file mode 100644 index 0000000..161c576 --- /dev/null +++ b/README.md @@ -0,0 +1 @@ +# UAveiro - LECI