Algumas simplificações
This commit is contained in:
parent
d8fac9474e
commit
58112d2e23
|
@ -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
|
||||
|
||||
|
||||
def main():
|
||||
for x in range(1, 100):
|
||||
print('Is {} prime? {}'.format(x, isPrime(x)))
|
||||
|
||||
main()
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
|
|
|
@ -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.
|
||||
|
|
|
@ -1,15 +1,14 @@
|
|||
import re
|
||||
|
||||
def main():
|
||||
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()
|
|
@ -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,7 +49,6 @@ def maxArray(arr):
|
|||
if a > mx:
|
||||
mx = a
|
||||
return mx
|
||||
# <!-- -->
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
|
|
@ -2,12 +2,11 @@ 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()
|
|
@ -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
|
||||
|
||||
|
|
Loading…
Reference in New Issue