FP - Extra3 - Exercices 1 through 6 added
This commit is contained in:
parent
f6e0176208
commit
70c4a24e5a
|
@ -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")
|
|
@ -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
|
|
@ -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
|
|
@ -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
|
|
@ -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}%")
|
|
@ -0,0 +1,2 @@
|
|||
# Complete...
|
||||
stocks2 = sorted(sorted(stocks, key=lambda stock: stock[4], reverse=True), key=lambda stock: stock[0])
|
Loading…
Reference in New Issue