FP: Aula09 - Added exercices 4, 5, 8
This commit is contained in:
parent
301f70476b
commit
5bbf8e28ea
|
@ -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)
|
||||
|
|
|
@ -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}")
|
|
@ -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()
|
|
@ -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()
|
Loading…
Reference in New Issue