diff --git a/1ano/fp/aula09/README.md b/1ano/fp/aula09/README.md index af70314..82fb0c1 100644 --- a/1ano/fp/aula09/README.md +++ b/1ano/fp/aula09/README.md @@ -1,7 +1,7 @@ # Fundamentos de Programação ## Aula 09 - [Slides](https://github.com/TiagoRG/uaveiro-leci/blob/master/1ano/fp/slides/tp09-searching+sorting.pdf) ### Tópicos principais da aula: Searching, Sorting, Lambda Expressions -#### Exercícios feitos: 1, 2, 3, 6, 7 +#### Exercícios em falta: 9 --- *Pode conter erros, caso encontre algum, crie um* [*ticket*](https://github.com/TiagoRG/uaveiro-leci/issues/new) diff --git a/1ano/fp/aula09/binarySearch.py b/1ano/fp/aula09/binarySearch.py new file mode 100644 index 0000000..22e2499 --- /dev/null +++ b/1ano/fp/aula09/binarySearch.py @@ -0,0 +1,10 @@ +import bisect + + +with open("wordlist.txt", "r") as f: + word_list: list[str] = f.read().split() + + begin_index: int = bisect.bisect_left(word_list, "ea") + end_index: int = bisect.bisect_left(word_list, "eb") + + print(f"Words started with 'ea': {end_index - begin_index}") diff --git a/1ano/fp/aula09/findZero.py b/1ano/fp/aula09/findZero.py new file mode 100644 index 0000000..e4d45c6 --- /dev/null +++ b/1ano/fp/aula09/findZero.py @@ -0,0 +1,23 @@ +import math + + +def main(): + print(findZero(lambda x: x + math.sin(10*x), 0.2, 0.4, 0.001)) + + +def findZero(func, a, b, tol): + if func(a) * func(b) > 0: + return None + + while b - a > tol: + m = (a + b) / 2 + if func(a) * func(m) <= 0: + b = m + else: + a = m + + return (a + b) / 2 + + +if __name__ == '__main__': + main() diff --git a/1ano/fp/aula09/prefixSearch.py b/1ano/fp/aula09/prefixSearch.py new file mode 100644 index 0000000..3554165 --- /dev/null +++ b/1ano/fp/aula09/prefixSearch.py @@ -0,0 +1,22 @@ +import bisect + + +def main() -> None: + with open("wordlist.txt", "r") as f: + word_list: list[str] = f.read().split() + + prefix = "" + while True: + prefix += input(f"Current prefix: {'None' if prefix == '' else prefix}\nAdd to prefix: ") + print(prefixSearch(word_list, prefix)) + + +def prefixSearch(lst: list[str], prefix: str) -> list[str]: + begin_index = bisect.bisect_left(lst, prefix) + end_index = bisect.bisect_left(lst, prefix + chr(127)) + + return lst[begin_index:end_index] + + +if __name__ == "__main__": + main()