From 96a28133dda1287a44d11af42d820fe1bfde574c Mon Sep 17 00:00:00 2001 From: tiagorg Date: Thu, 10 Nov 2022 23:20:38 +0000 Subject: [PATCH] =?UTF-8?q?Algumas=20simplifica=C3=A7=C3=B5es?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 1ano/fp/aula04/isprime.py | 15 +++++++-------- 1ano/fp/aula04/table.py | 4 ++-- 1ano/fp/aula05/countDigits.py | 13 ++++++------- 1ano/fp/aula05/ex08.py | 21 +++++++++------------ 1ano/fp/aula05/ispalindrome.py | 7 +++---- 1ano/fp/aula05/shorten.py | 9 +++------ 1ano/fp/aula07/{ => datafiles}/nasdaq.csv | 0 7 files changed, 30 insertions(+), 39 deletions(-) rename 1ano/fp/aula07/{ => datafiles}/nasdaq.csv (100%) diff --git a/1ano/fp/aula04/isprime.py b/1ano/fp/aula04/isprime.py index a08ec00..44b73e5 100755 --- a/1ano/fp/aula04/isprime.py +++ b/1ano/fp/aula04/isprime.py @@ -1,15 +1,14 @@ def isPrime(n): - div_counter = 0 - for x in range(1, n): + for x in range(2, n): if n % x == 0: - div_counter += 1 - if div_counter > 1 or n == 1: - return False - else: - return True + return False + return True + def main(): for x in range(1, 100): print('Is {} prime? {}'.format(x, isPrime(x))) -main() \ No newline at end of file + +if __name__ == "__main__": + main() diff --git a/1ano/fp/aula04/table.py b/1ano/fp/aula04/table.py index 19221dc..a92f6e7 100755 --- a/1ano/fp/aula04/table.py +++ b/1ano/fp/aula04/table.py @@ -1,7 +1,7 @@ # Show a table of the squares of the first four numbers -print(" {:2s}| {:2s}| {:2s}".format("n", "n²", "2**n")) +print(" {:1s} | {:>3s} | {:>7s}".format("n", "n²", "2**n")) for n in range(1, 21): - print("{:2d} | {:3d} |{:8d}".format(n, n**2, 2**n)) + print("{:2d} | {:3d} | {:7d}".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. diff --git a/1ano/fp/aula05/countDigits.py b/1ano/fp/aula05/countDigits.py index a74db28..8bd327f 100755 --- a/1ano/fp/aula05/countDigits.py +++ b/1ano/fp/aula05/countDigits.py @@ -1,15 +1,14 @@ -import re - def main(): - print(countDigits("far4214faewf13")) # Tem de dar 6 + print(countDigits("far4214faewf13")) # Tem de dar 6 + def countDigits(string): - nums = r"[0-9]$" count = 0 for char in string: - if re.match(nums, char): + if char.isdigit(): count += 1 return count - + + if __name__ == "__main__": - main() \ No newline at end of file + main() diff --git a/1ano/fp/aula05/ex08.py b/1ano/fp/aula05/ex08.py index 0ba7a4c..dc23ff3 100755 --- a/1ano/fp/aula05/ex08.py +++ b/1ano/fp/aula05/ex08.py @@ -18,25 +18,23 @@ def evenThenOdd(string): def removeAdjacentDuplicates(s): - s = str(s) - newS = '' - lastChar = 'a' if s[0] != 'a' else 'b' - for char in s: - if char != lastChar: - newS += char - lastChar = char - return newS + new = '' + for i in range(len(s)): + if i == 0: + new += s[i] + elif s[i] != s[i-1]: + new += s[i] + return new def reapeatNumTimes(n): lst = [] - for i in range(1,n+1): + for i in range(1, n+1): for j in range(i): lst.append(i) return lst -# def positionOfFirstLargest(arr): mx = maxArray(arr) index = 0 @@ -51,8 +49,7 @@ def maxArray(arr): if a > mx: mx = a return mx -# if __name__ == "__main__": - main() \ No newline at end of file + main() diff --git a/1ano/fp/aula05/ispalindrome.py b/1ano/fp/aula05/ispalindrome.py index bace5c8..f31e668 100755 --- a/1ano/fp/aula05/ispalindrome.py +++ b/1ano/fp/aula05/ispalindrome.py @@ -1,13 +1,12 @@ def main(): s = input('Introduza uma string >> ') print(ispalindrome(s)) + def ispalindrome(s): pal_s = s[::-1] - if pal_s == s: - return True - else: - return False + return pal_s == s + if __name__ == "__main__": main() \ No newline at end of file diff --git a/1ano/fp/aula05/shorten.py b/1ano/fp/aula05/shorten.py index 6ef0666..d976232 100755 --- a/1ano/fp/aula05/shorten.py +++ b/1ano/fp/aula05/shorten.py @@ -1,15 +1,12 @@ -import re - - def main(): print(shorten("Universidade de Aveiro")) print(shorten("United Nations Organization")) - + + def shorten(string): abv = '' - upper = r"[A-Z]$" for char in string: - if re.match(upper, char): + if char.isupper(): abv += char return abv diff --git a/1ano/fp/aula07/nasdaq.csv b/1ano/fp/aula07/datafiles/nasdaq.csv similarity index 100% rename from 1ano/fp/aula07/nasdaq.csv rename to 1ano/fp/aula07/datafiles/nasdaq.csv