Skip to content

Commit

Permalink
algoritmo de verificação de palíndromos
Browse files Browse the repository at this point in the history
  • Loading branch information
eduardomps committed Mar 14, 2020
1 parent 8191ca0 commit b6f6ef0
Show file tree
Hide file tree
Showing 4 changed files with 125 additions and 2 deletions.
19 changes: 19 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,22 @@ duplicados/obj/Debug/netcoreapp3.1/duplicados.csproj.FileListAbsolute.txt
duplicados/obj/Debug/netcoreapp3.1/duplicados.csprojAssemblyReference.cache
duplicados/obj/Debug/netcoreapp3.1/duplicados.dll
duplicados/obj/Debug/netcoreapp3.1/duplicados.pdb
palindromos/bin/Debug/netcoreapp3.1/palindromos
palindromos/bin/Debug/netcoreapp3.1/palindromos.deps.json
palindromos/bin/Debug/netcoreapp3.1/palindromos.dll
palindromos/bin/Debug/netcoreapp3.1/palindromos.pdb
palindromos/bin/Debug/netcoreapp3.1/palindromos.runtimeconfig.dev.json
palindromos/bin/Debug/netcoreapp3.1/palindromos.runtimeconfig.json
palindromos/obj/palindromos.csproj.nuget.cache
palindromos/obj/palindromos.csproj.nuget.dgspec.json
palindromos/obj/palindromos.csproj.nuget.g.props
palindromos/obj/palindromos.csproj.nuget.g.targets
palindromos/obj/project.assets.json
palindromos/obj/Debug/netcoreapp3.1/palindromos
palindromos/obj/Debug/netcoreapp3.1/palindromos.AssemblyInfo.cs
palindromos/obj/Debug/netcoreapp3.1/palindromos.AssemblyInfoInputs.cache
palindromos/obj/Debug/netcoreapp3.1/palindromos.assets.cache
palindromos/obj/Debug/netcoreapp3.1/palindromos.csproj.FileListAbsolute.txt
palindromos/obj/Debug/netcoreapp3.1/palindromos.csprojAssemblyReference.cache
palindromos/obj/Debug/netcoreapp3.1/palindromos.dll
palindromos/obj/Debug/netcoreapp3.1/palindromos.pdb
18 changes: 16 additions & 2 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
// Use hover for the description of the existing attributes
// For further information visit https://github.com/OmniSharp/omnisharp-vscode/blob/master/debugger-launchjson.md
"version": "0.2.0",
"configurations": [
"configurations": [
{
"name": ".NET Core Launch (console)",
"name": "Duplicados",
"type": "coreclr",
"request": "launch",
"preLaunchTask": "build",
Expand All @@ -17,11 +17,25 @@
"console": "internalConsole",
"stopAtEntry": false
},
{
"name": "Palindromos",
"type": "coreclr",
"request": "launch",
"preLaunchTask": "build",
// If you have changed target frameworks, make sure to update the program path.
"program": "${workspaceFolder}/palindromos/bin/Debug/netcoreapp3.1/palindromos.dll",
"args": ["A Rita, sobre vovô, verbos atira"],
"cwd": "${workspaceFolder}/palindromos",
// For more information about the 'console' field, see https://aka.ms/VSCode-CS-LaunchJson-Console
"console": "internalConsole",
"stopAtEntry": false
},
{
"name": ".NET Core Attach",
"type": "coreclr",
"request": "attach",
"processId": "${command:pickProcess}"
}

]
}
82 changes: 82 additions & 0 deletions palindromos/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
using System;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace palindromos
{
class Program
{
static void Main(string[] args)
{
if(args.Length <= 0)
{
Console.WriteLine("USO DO PROGRAMA: palindromos <sequencia para testar>!");
return;
}
string input = args[0];

Console.WriteLine("CONSIDERANDO espaços, especiais e diacríticos: ");
string result = IsPalindrome(input)
? $"'{input}' É palíndromo!"
: $"'{input}' NÃO É palíndromo.";
Console.WriteLine(result);

Console.WriteLine("IGNORANDO espaços, especiais e diacríticos: ");
result = IsPalindrome(input, false)
? $"'{input}' É palíndromo!"
: $"'{input}' NÃO É palíndromo.";
Console.WriteLine(result);
}

// Valida se a string de entrada é palíndroma
private static bool IsPalindrome(string input, bool isSimpleWord = true)
{
bool isPalindrome = true;
// parâmetro isSimpleWord indica se validará apenas
// uma palavra ex.: "arara" ou se é frase e portanto
// deve usar regras de espaços e diacríticos
if(!isSimpleWord)
{
// remove os espaços e especiais
// e os diacríticos
input = Regex.Replace(input, @"\W+", string.Empty);
input = RemoveDiacritics(input);
}
// garantindo o ignoreCase
input = input.ToUpper();
int halfLengthIndex = input.Length / 2 - 1;
int lastIndex = input.Length - 1;
// vai até a metade da string
for(int index=0; index< halfLengthIndex; index++)
{
// compara o caractrer no primeiro indice com o ultimo
// o segundo com o penultimo, etc
// se encontrar alguma coisa diferente, já pode retornar falso
if(input[index] != input[lastIndex-index])
{
isPalindrome = false;
break;
}

}
return isPalindrome;
}

// método para "limpar" a string de diacríticos
// ps.: já havia utilizado anteriormente em produção
// -- fonte: https://stackoverflow.com/a/13155469
private static string RemoveDiacritics(string str)
{
if (null == str) return null;
var chars = str
.Normalize(NormalizationForm.FormD)
.ToCharArray()
.Where(c=> CharUnicodeInfo.GetUnicodeCategory(c) != UnicodeCategory.NonSpacingMark)
.ToArray();

return new string(chars).Normalize(NormalizationForm.FormC);
}
}
}
8 changes: 8 additions & 0 deletions 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>netcoreapp3.1</TargetFramework>
</PropertyGroup>

</Project>

0 comments on commit b6f6ef0

Please sign in to comment.