Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Desafio3 #1020

Merged
merged 6 commits into from
Jan 11, 2024
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions desafio-03/erickofs/python/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Calculadora de Palíndromos

## Visão Geral
Este script foi projetado para calcular e recuperar números palíndromos com base em limites e intervalos definidos pelo usuário.

## Requisitos

Para executar este módulo, basta ter uma versão do Python 3.10+ e executar a seguinte linha de comando no terminal:
```
python.exe __main__.py
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Este comentário só vai funcionar para quem usar Windows

```

## 2. Implementação

### Uso
1. O script solicita ao usuário que insira o limite de cálculo (até 1000000).
2. O script solicita ao usuário a quantidade de números palíndromos a serem exibidos.
3. Se nenhum valor for fornecido para o limite ou intervalo, valores padrão são definidos.

### Observação
- O limite e o intervalo são validados para garantir que atendam aos critérios especificados.
7 changes: 7 additions & 0 deletions desafio-03/erickofs/python/__main__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
from palcalc import PalCalc

if __name__ == "__main__":

pn = PalCalc()
for i in pn.get_pal():
print(i)
70 changes: 70 additions & 0 deletions desafio-03/erickofs/python/palcalc.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
# Create class for calculating the palindromes
class PalCalc():
"""
Unique class to store the calculation logic
"""
def get_pal(self):
"""
Calculates the palindromes and returns a list of them based on the limit and range.
"""

# Get the limit and range
pallimit = self.get_valid_limit()
palrange = self.get_valid_range()
pallist = {i for i in range(1, pallimit + 1) if str(i) == str(i)[::-1]}
return list(pallist)[:palrange] if palrange != 0 else list(pallist)

def get_valid_limit(self):
"""
Validates and returns the calculation limit.
"""
while True:
pallimit = input("Enter the calculation limit: ")
try:
pallimit = float(pallimit)
if pallimit < 0:
print("Please enter a positive number.")
elif pallimit > 1000000:
print("The limit is too high. Please enter a number less than 1000000.")
else:
break
except ValueError:
pallimit = pallimit.strip()
if pallimit == "":
pallimit = 100.0
print("No given limit. It has been automatically set to 100.")
break
print("Invalid input. Please enter a valid number.")
if round(pallimit) != pallimit:
print(f"The limit has been rounded to the nearest integer: {round(pallimit)}")
pallimit = int(round(pallimit))
if pallimit == 0:
print("The limit has been automatically set to 100.")
pallimit = 100
return pallimit

def get_valid_range(self):
"""
Validates and returns the number of prime numbers to be displayed.
"""
while True:
palrange = input("Enter the number of prime numbers to be displayed (0 = all): ")
try:
palrange = float(palrange)
if palrange < 0:
print("Please enter a positive number.")
elif palrange > 1000000:
print("The limit is too high. Please enter a number less than 1000000.")
else:
break
except ValueError:
palrange = palrange.strip()
if palrange == "":
palrange = 0
print("No given limit. All numbers will be printed.")
break
print("Invalid input. Please enter a valid number.")
if round(palrange) != palrange:
print(f"The limit has been rounded to the nearest integer: {round(palrange)}")
palrange = int(round(palrange))
return palrange
Loading