Algumas simplificações

This commit is contained in:
tiagorg 2022-11-10 23:20:38 +00:00
parent 8cf9127399
commit 96a28133dd
Signed by untrusted user who does not match committer: TiagoRG
GPG Key ID: DFCD48E3F420DB42
7 changed files with 30 additions and 39 deletions

View File

@ -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()
if __name__ == "__main__":
main()

View File

@ -1,7 +1,7 @@
# Show a table of the squares of the first four numbers
print(" {:2s}| {:2s}| {:2s}".format("n", "", "2**n"))
print(" {:1s} | {:>3s} | {:>7s}".format("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.

View File

@ -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()
main()

View File

@ -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()
main()

View File

@ -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()

View File

@ -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