Skip to content

Commit

Permalink
Desafio03 (#1062)
Browse files Browse the repository at this point in the history
* Proposta de Solução desafio-02.

* Efetuadas correções sugeridas

* Removendo op-desafios.sln

* Proposta de solução do exercício 03 em csharp.

* Corrigidos erros de Trailing Spaces

* Adicionado instruções de como executar o teste.

* Correção do nome do arquivo

* Correção do desafio 03

* Alterado entrada da aplicação para que o usuário possa efetuar testes variados por input de parâmetros.
  • Loading branch information
ronaldofas committed Aug 4, 2024
1 parent fdd4749 commit 17de511
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 0 deletions.
56 changes: 56 additions & 0 deletions desafio-03/ronaldofas/csharp/palindromos/Palindromo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
using System;

namespace palindromos;

public class Palindromo
{
public Palindromo() { }

public bool EhPalindromo(ulong numero){
// Converte o número para uma string.
string numeroStr = numero.ToString();

// Inverte a string.
char[] charArray = numeroStr.ToCharArray();
Array.Reverse(charArray);
string numeroInvertidoStr = new string(charArray);

// Compara a string original com a string invertida.
return numeroStr.Equals(numeroInvertidoStr);
}

public List<ulong> PalindromosEntre(string inicio, string fim)
{
ValidarParametros(inicio);
ValidarParametros(fim);

List<ulong> resultado = new List<ulong>();

for (ulong i = UInt64.Parse(inicio); i <= UInt64.Parse(fim); i++)
{
if (EhPalindromo(i))
{
resultado.Add(i);
}
}

return resultado;
}

private static void ValidarParametros(string numero)
{
try
{
var conversao = UInt64.Parse(numero);
} catch (OverflowException)
{
throw new OverflowException("Valor maior ou menor que o permitido");
} catch (ArgumentNullException)
{
throw new ArgumentNullException("Valor não pode ser nulo");
} catch (FormatException)
{
throw new FormatException("Valor inválido");
}
}
}
23 changes: 23 additions & 0 deletions desafio-03/ronaldofas/csharp/palindromos/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using System;

namespace palindromos;

class Program
{
public static void Main(string[] args)
{
Palindromo palindromo = new Palindromo();

Console.WriteLine("Informe o número inicial para validação dos palíndromos: ");
string inicio = Console.ReadLine();
Console.WriteLine("Informe o número final para validação dos palíndromos: ");
string fim = Console.ReadLine();

Console.WriteLine("\nNumeros palíndromos entre " + inicio + " e " + fim + ": ");
foreach (ulong item in palindromo.PalindromosEntre(inicio, fim))
{
Console.WriteLine(item);
}

}
}
8 changes: 8 additions & 0 deletions desafio-03/ronaldofas/csharp/palindromos/palindromos.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
</Project>

0 comments on commit 17de511

Please sign in to comment.