FP - Extra3 - Exercices 1 through 6 added

This commit is contained in:
TiagoRG 2023-01-08 22:40:13 +00:00
parent 0ce3cb4ab2
commit 73646c8b6b
6 changed files with 66 additions and 0 deletions

15
1ano/fp/extra3/ex01.py Normal file
View File

@ -0,0 +1,15 @@
# On a chessboard, positions are marked with letters between a and h for the column and a
# number between 1 and 8 for the row. The first place on the board, a1, is black. The next
# is white, alternating across a row. Odd rows start with black, even rows start with white.
# Give a 2 character input string with a letter (a-h) and a number (1-8), print "Black" or
# "White" indicating if the square is black or white.
inputStr = 'b3'
print("Black" if (
(inputStr[0] in ['a', 'c', 'e', 'g'] and int(inputStr[1]) % 2 == 1)
or (inputStr[0] in ['b', 'd', 'f', 'h'] and int(inputStr[1]) % 2 == 0)
)
else "White")

16
1ano/fp/extra3/ex02.py Normal file
View File

@ -0,0 +1,16 @@
# Given a sequence lst, return the longest n so that
# the first n elements equal the last n elements (with no overlapping).
# Dada uma sequência lst, devolva o maior n tal que
# os primeiros n elementos igualam os últimos n elementos (sem sobreposição).
def firstEqualLast(lst):
if len(lst) <= 1:
return 0
n = 0
for i in range(1, len(lst)//2+1):
if lst[:i] == lst[len(lst)-i:]:
n = i
return n

16
1ano/fp/extra3/ex03.py Normal file
View File

@ -0,0 +1,16 @@
# Given a string s and a string t, return a string in which all the characters
# of s that occur in t have been replaced by a _ sign. The comparisons are
# case sensitive.
def replaceCharactersWithUnderscores(s, t):
# Your code here...
new_str = ""
for c in s:
if c in t:
new_str += '_'
else:
new_str += c
return new_str

14
1ano/fp/extra3/ex04.py Normal file
View File

@ -0,0 +1,14 @@
# Given a string s, return the longest prefix that is repeated somewhere else in the string.
# For example, "abcdabejf" would return "ab" as "ab" starts at the beginning of the string
# and is repeated again later. Do not use the find method.
def longestPrefixRepeated(s):
# Your code here...
longest = ""
for i in range(1, len(s)//2+1):
if s[:i] in s[i:]:
longest = s[:i]
return longest

3
1ano/fp/extra3/ex05.py Normal file
View File

@ -0,0 +1,3 @@
def printStocks(stocks):
for stock in stocks:
print(f"{stock[0]:<10}{stock[1]:<19}{stock[2]:>6.2f}{stock[3]:>10.2f}{stock[4]:>10}{(stock[3]/stock[2]-1)*100:>7.1f}%")

2
1ano/fp/extra3/ex06.py Normal file
View File

@ -0,0 +1,2 @@
# Complete...
stocks2 = sorted(sorted(stocks, key=lambda stock: stock[4], reverse=True), key=lambda stock: stock[0])