Aula05 adicionada
This commit is contained in:
parent
34241b443b
commit
6cd24ca86c
|
@ -0,0 +1,16 @@
|
|||
def main():
|
||||
matches = allMatches(['SLB', 'FCP', 'SCP', 'SB'])
|
||||
print(matches)
|
||||
print(len(matches))
|
||||
|
||||
def allMatches(teamList):
|
||||
matchList = []
|
||||
for team1 in teamList:
|
||||
for team2 in teamList:
|
||||
if team1 == team2:
|
||||
continue
|
||||
matchList.append((team1, team2))
|
||||
return matchList
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
Binary file not shown.
|
@ -0,0 +1,15 @@
|
|||
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):
|
||||
count += 1
|
||||
return count
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
|
@ -0,0 +1,50 @@
|
|||
def main():
|
||||
floatList = inputFloatList()
|
||||
print(floatList)
|
||||
print()
|
||||
|
||||
# Tem de dar print a '4'
|
||||
print(countLower([1321, 143, 1432, 512, 43, 153, 143613], 1000))
|
||||
print()
|
||||
|
||||
# Tem de dar print a 43, 143613
|
||||
print(minmax([1321, 143, 1432, 512, 43, 153, 143613]))
|
||||
print()
|
||||
|
||||
#
|
||||
mix()
|
||||
|
||||
def inputFloatList(inputMsg = '>>> '):
|
||||
returnList = []
|
||||
while True:
|
||||
inpt = input(inputMsg)
|
||||
if inpt == '': return returnList
|
||||
try:
|
||||
returnList.append(float(inpt))
|
||||
except:
|
||||
continue
|
||||
|
||||
def countLower(lst, v):
|
||||
returnList = [lst[i] for i in range(len(lst)) if lst[i] < v]
|
||||
return len(returnList)
|
||||
|
||||
def minmax(lst):
|
||||
mx = 0
|
||||
mn = 0
|
||||
for n in lst:
|
||||
if n > mx:
|
||||
mx = n
|
||||
if n < mn:
|
||||
mn = n
|
||||
return (mn, mx)
|
||||
|
||||
# Alinea d)
|
||||
def mix():
|
||||
lst = inputFloatList()
|
||||
mn_mx = minmax(lst)
|
||||
med = (mn_mx[0] + mn_mx[1]) / 2
|
||||
count = countLower(lst, med)
|
||||
print(count)
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
|
@ -0,0 +1,58 @@
|
|||
def main():
|
||||
print(evenThenOdd('abcd'))
|
||||
print(removeAdjacentDuplicates('mississipi'))
|
||||
print(reapeatNumTimes(4))
|
||||
print(positionOfFirstLargest([1, 624, 123, 34, 12]))
|
||||
|
||||
def evenThenOdd(string):
|
||||
even = ''
|
||||
odd = ''
|
||||
index = 0
|
||||
for char in string:
|
||||
if index % 2 == 0:
|
||||
even += char
|
||||
else:
|
||||
odd += char
|
||||
index += 1
|
||||
return even + odd
|
||||
|
||||
|
||||
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
|
||||
|
||||
|
||||
def reapeatNumTimes(n):
|
||||
lst = []
|
||||
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
|
||||
for a in arr:
|
||||
if a == mx:
|
||||
return index
|
||||
index += 1
|
||||
|
||||
def maxArray(arr):
|
||||
mx = 0
|
||||
for a in arr:
|
||||
if a > mx:
|
||||
mx = a
|
||||
return mx
|
||||
# <!-- -->
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
|
@ -0,0 +1,13 @@
|
|||
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
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
|
@ -0,0 +1,17 @@
|
|||
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):
|
||||
abv += char
|
||||
return abv
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
|
@ -0,0 +1,50 @@
|
|||
from operator import contains
|
||||
|
||||
# Convert a telephone number into corresponding name, if on list.
|
||||
# (If not on list, just return the number itself.)
|
||||
|
||||
|
||||
def telToName(tel, telList, nameList):
|
||||
# your code here
|
||||
index = 0
|
||||
try:
|
||||
for t in telList:
|
||||
if t == tel: break
|
||||
index += 1
|
||||
name = nameList[index]
|
||||
except:
|
||||
name = tel
|
||||
return name
|
||||
|
||||
|
||||
# Return list of telephone numbers corresponding to names containing partName.
|
||||
def nameToTels(partName, telList, nameList):
|
||||
# your code here
|
||||
tels = []
|
||||
index = 0
|
||||
for name in nameList:
|
||||
if contains(name, partName):
|
||||
tels.append(telList[index])
|
||||
index += 1
|
||||
return tels
|
||||
|
||||
def main():
|
||||
# Lists of telephone numbers and names
|
||||
telList = ['975318642', '234000111', '777888333', '911911911']
|
||||
nameList = ['Angelina', 'Brad', 'Claudia', 'Bruna']
|
||||
|
||||
# Test telToName:
|
||||
tel = input("Tel number? ")
|
||||
print( telToName(tel, telList, nameList) )
|
||||
print( telToName('234000111', telList, nameList) == "Brad" )
|
||||
print( telToName('222333444', telList, nameList) == "222333444" )
|
||||
|
||||
# Test nameToTels:
|
||||
name = input("Name? ")
|
||||
print( nameToTels(name, telList, nameList) )
|
||||
print( nameToTels('Clau', telList, nameList) == ['777888333'] )
|
||||
print( nameToTels('Br', telList, nameList) == ['234000111', '911911911'] )
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
Loading…
Reference in New Issue