Algumas simplificações
This commit is contained in:
parent
8cf9127399
commit
96a28133dd
|
@ -1,15 +1,14 @@
|
||||||
def isPrime(n):
|
def isPrime(n):
|
||||||
div_counter = 0
|
for x in range(2, n):
|
||||||
for x in range(1, n):
|
|
||||||
if n % x == 0:
|
if n % x == 0:
|
||||||
div_counter += 1
|
return False
|
||||||
if div_counter > 1 or n == 1:
|
return True
|
||||||
return False
|
|
||||||
else:
|
|
||||||
return True
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
for x in range(1, 100):
|
for x in range(1, 100):
|
||||||
print('Is {} prime? {}'.format(x, isPrime(x)))
|
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
|
# 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):
|
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.)
|
# 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.
|
# Also, add a column to show 2**n. Adjust the formatting.
|
||||||
|
|
|
@ -1,15 +1,14 @@
|
||||||
import re
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
print(countDigits("far4214faewf13")) # Tem de dar 6
|
print(countDigits("far4214faewf13")) # Tem de dar 6
|
||||||
|
|
||||||
|
|
||||||
def countDigits(string):
|
def countDigits(string):
|
||||||
nums = r"[0-9]$"
|
|
||||||
count = 0
|
count = 0
|
||||||
for char in string:
|
for char in string:
|
||||||
if re.match(nums, char):
|
if char.isdigit():
|
||||||
count += 1
|
count += 1
|
||||||
return count
|
return count
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
main()
|
main()
|
||||||
|
|
|
@ -18,25 +18,23 @@ def evenThenOdd(string):
|
||||||
|
|
||||||
|
|
||||||
def removeAdjacentDuplicates(s):
|
def removeAdjacentDuplicates(s):
|
||||||
s = str(s)
|
new = ''
|
||||||
newS = ''
|
for i in range(len(s)):
|
||||||
lastChar = 'a' if s[0] != 'a' else 'b'
|
if i == 0:
|
||||||
for char in s:
|
new += s[i]
|
||||||
if char != lastChar:
|
elif s[i] != s[i-1]:
|
||||||
newS += char
|
new += s[i]
|
||||||
lastChar = char
|
return new
|
||||||
return newS
|
|
||||||
|
|
||||||
|
|
||||||
def reapeatNumTimes(n):
|
def reapeatNumTimes(n):
|
||||||
lst = []
|
lst = []
|
||||||
for i in range(1,n+1):
|
for i in range(1, n+1):
|
||||||
for j in range(i):
|
for j in range(i):
|
||||||
lst.append(i)
|
lst.append(i)
|
||||||
return lst
|
return lst
|
||||||
|
|
||||||
|
|
||||||
# <!-- -->
|
|
||||||
def positionOfFirstLargest(arr):
|
def positionOfFirstLargest(arr):
|
||||||
mx = maxArray(arr)
|
mx = maxArray(arr)
|
||||||
index = 0
|
index = 0
|
||||||
|
@ -51,8 +49,7 @@ def maxArray(arr):
|
||||||
if a > mx:
|
if a > mx:
|
||||||
mx = a
|
mx = a
|
||||||
return mx
|
return mx
|
||||||
# <!-- -->
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
main()
|
main()
|
||||||
|
|
|
@ -1,13 +1,12 @@
|
||||||
def main():
|
def main():
|
||||||
s = input('Introduza uma string >> ')
|
s = input('Introduza uma string >> ')
|
||||||
print(ispalindrome(s))
|
print(ispalindrome(s))
|
||||||
|
|
||||||
|
|
||||||
def ispalindrome(s):
|
def ispalindrome(s):
|
||||||
pal_s = s[::-1]
|
pal_s = s[::-1]
|
||||||
if pal_s == s:
|
return pal_s == s
|
||||||
return True
|
|
||||||
else:
|
|
||||||
return False
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
main()
|
main()
|
|
@ -1,15 +1,12 @@
|
||||||
import re
|
|
||||||
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
print(shorten("Universidade de Aveiro"))
|
print(shorten("Universidade de Aveiro"))
|
||||||
print(shorten("United Nations Organization"))
|
print(shorten("United Nations Organization"))
|
||||||
|
|
||||||
|
|
||||||
def shorten(string):
|
def shorten(string):
|
||||||
abv = ''
|
abv = ''
|
||||||
upper = r"[A-Z]$"
|
|
||||||
for char in string:
|
for char in string:
|
||||||
if re.match(upper, char):
|
if char.isupper():
|
||||||
abv += char
|
abv += char
|
||||||
return abv
|
return abv
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue