Skip to content
This repository has been archived by the owner on Nov 12, 2024. It is now read-only.

Marcel Schmidt solution for 'Palindrom' and 'Rekursion' #103

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
31 changes: 31 additions & 0 deletions katas/Palindrom/solutions/MarcelSchmidt/Palindrom/Palindrom.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 16
VisualStudioVersion = 16.0.31112.23
MinimumVisualStudioVersion = 10.0.40219.1
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Palindrom", "Palindrom\Palindrom.csproj", "{87FAE52E-8A5E-4E2C-80FC-1B82110F86BA}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "PalindromTest", "PalindromTest\PalindromTest.csproj", "{7FD235E0-F63F-4653-BC54-CC002E15BA19}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{87FAE52E-8A5E-4E2C-80FC-1B82110F86BA}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{87FAE52E-8A5E-4E2C-80FC-1B82110F86BA}.Debug|Any CPU.Build.0 = Debug|Any CPU
{87FAE52E-8A5E-4E2C-80FC-1B82110F86BA}.Release|Any CPU.ActiveCfg = Release|Any CPU
{87FAE52E-8A5E-4E2C-80FC-1B82110F86BA}.Release|Any CPU.Build.0 = Release|Any CPU
{7FD235E0-F63F-4653-BC54-CC002E15BA19}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{7FD235E0-F63F-4653-BC54-CC002E15BA19}.Debug|Any CPU.Build.0 = Debug|Any CPU
{7FD235E0-F63F-4653-BC54-CC002E15BA19}.Release|Any CPU.ActiveCfg = Release|Any CPU
{7FD235E0-F63F-4653-BC54-CC002E15BA19}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {D30751F1-D037-4D14-8F26-2D5CBFA3FC47}
EndGlobalSection
EndGlobal
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>
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
using System;
using System.Linq;

namespace Palindrom
{
public static class Palindrome
{
public static bool IsPalindrome(string input, bool ignoreCaseSensitivity = false)
{
if (string.IsNullOrWhiteSpace(input))
return false;

if (input.Any(char.IsPunctuation) || input.Any(char.IsWhiteSpace))
{
input = new string(input.Where(x => !(char.IsPunctuation(x) || char.IsWhiteSpace(x))).ToArray());
}

var firstHalfLength = (int)Math.Floor(input.Length / 2d);
var secondHalfStartIndex = (int)Math.Ceiling(input.Length / 2d);

var firstHalf = input[0..firstHalfLength];
var secondHalf = ReverseString(input[secondHalfStartIndex..]);

return ignoreCaseSensitivity
? firstHalf.ToLower() == secondHalf.ToLower()
: firstHalf == secondHalf;
}

private static string ReverseString(string s)
{
var array = s.ToCharArray();
Array.Reverse(array);
return new string(array);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
using System.Collections.Generic;

namespace Palindrom
{
public static class PalindromeExamples
{
public static List<string> PalindromesCaseInsensitive { get; } = new List<string>
{
"Abba",
"Lagerregal",
"Reliefpfeiler",
"Rentner",
"Dienstmannamtsneid",
};

public static List<string> PalindromesCaseSensitive { get; } = new List<string>
{
"abba",
"lagerregal",
"reliefpfeiler",
"rentner",
"dienstmannamtsneid",
};

public static List<string> NoPalindromes { get; } = new List<string>
{
"Aufgabe",
"Palindromsätze",
"Satzzeichen",
"vernachlässigen",
"Zeichenkette",
};

public static List<string> PalindromeSentencesCaseInsensitive { get; } = new List<string>
{
"Tarne nie deinen Rat!",
"Eine güldne, gute Tugend: Lüge nie!",
"Ein agiler Hit reizt sie. Geist?! Biertrunk nur treibt sie. Geist ziert ihre Liga nie!"
};

public static List<string> PalindromeSentencesCaseSensitive { get; } = new List<string>
{
"tarne nie deinen rat!",
"eine güldne, gute tugend: lüge nie!",
"ein agiler hit reizt sie. geist?! biertrunk nur treibt sie. geist ziert ihre liga nie!"
};

public static List<string> NoPalindromeSentences { get; } = new List<string>
{
"lorem ipsum dolor sit amet consectetur adipisicing elit.",
"Künstliche neuronale Netze haben, ebenso wie künstliche Neuronen, ein biologisches Vorbild.",
"An engine or motor is a machine designed to convert one or more forms of energy into mechanical energy."
};
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
using System;
using System.Linq;

namespace Palindrom
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("'Palindrom'");
Process();
}

private static void Process()
{
Console.WriteLine("Would you like to ignore case sensitivity for the palindrome checks? (y)es / (n)o");

var keyPressed = Console.ReadKey();

Console.WriteLine();

var ignoreCaseSensitivity = false;
if (keyPressed.Key == ConsoleKey.Y)
{
ignoreCaseSensitivity = true;
}
else if (keyPressed.Key != ConsoleKey.N)
{
Console.WriteLine("Error: Invalid input detected!");
Process();
return;
}

var testInput = PalindromeExamples.PalindromesCaseInsensitive
.Concat(PalindromeExamples.PalindromesCaseSensitive)
.Concat(PalindromeExamples.NoPalindromes)
.Concat(PalindromeExamples.PalindromeSentencesCaseInsensitive)
.Concat(PalindromeExamples.PalindromeSentencesCaseSensitive)
.Concat(PalindromeExamples.NoPalindromeSentences);

foreach (var input in testInput)
{
var result = Palindrome.IsPalindrome(input, ignoreCaseSensitivity) ? "IS" : "Is NOT";

Console.WriteLine("{0} >> {1} a palindrome", input, result);
}
}

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>

<IsPackable>false</IsPackable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.5.0" />
<PackageReference Include="MSTest.TestAdapter" Version="2.1.0" />
<PackageReference Include="MSTest.TestFramework" Version="2.1.0" />
<PackageReference Include="coverlet.collector" Version="1.2.0" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\Palindrom\Palindrom.csproj" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Palindrom;
using System.Collections.Generic;

namespace PalindromTest
{
[TestClass]
public class PalindromeTest
{
[TestMethod]
public void IsPalindrome_CaseInsensitiveCheck_WordCaseInsensitive_ReturnsTrue()
{
CaseInsensitiveCheckSucceed(PalindromeExamples.PalindromesCaseInsensitive);
}

[TestMethod]
public void IsPalindrome_CaseSensitiveCheck_WordCaseInsensitive_ReturnsFalse()
{
CaseSensitiveCheckFail(PalindromeExamples.PalindromesCaseInsensitive);
}

[TestMethod]
public void IsPalindrome_CaseInsensitiveCheck_WordCaseSensitive_ReturnsTrue()
{
CaseInsensitiveCheckSucceed(PalindromeExamples.PalindromesCaseSensitive);
}

[TestMethod]
public void IsPalindrome_CaseSensitiveCheck_WordCaseSensitive_ReturnsTrue()
{
CaseSensitiveCheckSucceed(PalindromeExamples.PalindromesCaseSensitive);
}

[TestMethod]
public void IsPalindrome_CaseSensitiveCheck_WordNoPalindrome_ReturnsFalse()
{
CaseSensitiveCheckFail(PalindromeExamples.NoPalindromes);
}

[TestMethod]
public void IsPalindrome_CaseInsensitiveCheck_WordNoPalindrome_ReturnsFalse()
{
CaseInsensitiveCheckFail(PalindromeExamples.NoPalindromes);
}

[TestMethod]
public void IsPalindrome_CaseInsensitiveCheck_SentenceCaseInsensitive_ReturnsTrue()
{
CaseInsensitiveCheckSucceed(PalindromeExamples.PalindromeSentencesCaseInsensitive);
}

[TestMethod]
public void IsPalindrome_CaseSensitiveCheck_SentenceCaseInsensitive_ReturnsFalse()
{
CaseSensitiveCheckFail(PalindromeExamples.PalindromeSentencesCaseInsensitive);
}

[TestMethod]
public void IsPalindrome_CaseInsensitiveCheck_SentenceCaseSensitive_ReturnsTrue()
{
CaseInsensitiveCheckSucceed(PalindromeExamples.PalindromeSentencesCaseSensitive);
}

[TestMethod]
public void IsPalindrome_CaseSensitiveCheck_SentenceCaseSensitive_ReturnsTrue()
{
CaseSensitiveCheckSucceed(PalindromeExamples.PalindromeSentencesCaseSensitive);
}

[TestMethod]
public void IsPalindrome_CaseSensitiveCheck_SentenceNoPalindrome_ReturnsFalse()
{
CaseSensitiveCheckFail(PalindromeExamples.NoPalindromeSentences);
}

[TestMethod]
public void IsPalindrome_CaseInsensitiveCheck_SentenceNoPalindrome_ReturnsFalse()
{
CaseInsensitiveCheckFail(PalindromeExamples.NoPalindromeSentences);
}

private static void CaseInsensitiveCheckFail(IEnumerable<string> palindromes)
{
foreach (var palindrome in palindromes)
{
if (Palindrome.IsPalindrome(palindrome, true))
{
Assert.Fail("{0} is for some reason classified as a palindrome", palindrome);
}
}
}

private static void CaseInsensitiveCheckSucceed(IEnumerable<string> palindromes)
{
foreach (var palindrome in palindromes)
{
if (!Palindrome.IsPalindrome(palindrome, true))
{
Assert.Fail("{0} is no palindrome", palindrome);
}
}
}

private static void CaseSensitiveCheckFail(IEnumerable<string> palindromes)
{
foreach (var palindrome in palindromes)
{
if (Palindrome.IsPalindrome(palindrome))
{
Assert.Fail("{0} is for some reason classified as a palindrome", palindrome);
}
}
}

private static void CaseSensitiveCheckSucceed(IEnumerable<string> palindromes)
{
foreach (var palindrome in palindromes)
{
if (!Palindrome.IsPalindrome(palindrome))
{
Assert.Fail("{0} is no palindrome", palindrome);
}
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\Infrastructure\Infrastructure.csproj" />
</ItemGroup>

</Project>
Loading