From 70c4a24e5a3cfbf106926402e310d7e6b095e65d Mon Sep 17 00:00:00 2001 From: TiagoRG <35657250+TiagoRG@users.noreply.github.com> Date: Sun, 8 Jan 2023 22:40:13 +0000 Subject: [PATCH] FP - Extra3 - Exercices 1 through 6 added --- 1ano/fp/extra3/ex01.py | 15 +++++++++++++++ 1ano/fp/extra3/ex02.py | 16 ++++++++++++++++ 1ano/fp/extra3/ex03.py | 16 ++++++++++++++++ 1ano/fp/extra3/ex04.py | 14 ++++++++++++++ 1ano/fp/extra3/ex05.py | 3 +++ 1ano/fp/extra3/ex06.py | 2 ++ 6 files changed, 66 insertions(+) create mode 100644 1ano/fp/extra3/ex01.py create mode 100644 1ano/fp/extra3/ex02.py create mode 100644 1ano/fp/extra3/ex03.py create mode 100644 1ano/fp/extra3/ex04.py create mode 100644 1ano/fp/extra3/ex05.py create mode 100644 1ano/fp/extra3/ex06.py diff --git a/1ano/fp/extra3/ex01.py b/1ano/fp/extra3/ex01.py new file mode 100644 index 0000000..53fa5ef --- /dev/null +++ b/1ano/fp/extra3/ex01.py @@ -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") diff --git a/1ano/fp/extra3/ex02.py b/1ano/fp/extra3/ex02.py new file mode 100644 index 0000000..06af1d6 --- /dev/null +++ b/1ano/fp/extra3/ex02.py @@ -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 diff --git a/1ano/fp/extra3/ex03.py b/1ano/fp/extra3/ex03.py new file mode 100644 index 0000000..1d04ea9 --- /dev/null +++ b/1ano/fp/extra3/ex03.py @@ -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 diff --git a/1ano/fp/extra3/ex04.py b/1ano/fp/extra3/ex04.py new file mode 100644 index 0000000..7658cad --- /dev/null +++ b/1ano/fp/extra3/ex04.py @@ -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 \ No newline at end of file diff --git a/1ano/fp/extra3/ex05.py b/1ano/fp/extra3/ex05.py new file mode 100644 index 0000000..b32256b --- /dev/null +++ b/1ano/fp/extra3/ex05.py @@ -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}%") diff --git a/1ano/fp/extra3/ex06.py b/1ano/fp/extra3/ex06.py new file mode 100644 index 0000000..c6a43d0 --- /dev/null +++ b/1ano/fp/extra3/ex06.py @@ -0,0 +1,2 @@ +# Complete... +stocks2 = sorted(sorted(stocks, key=lambda stock: stock[4], reverse=True), key=lambda stock: stock[0]) \ No newline at end of file