Skip to content

Commit e3e4b1a

Browse files
author
zzzprojects
committed
Fix anagram test + add IsPalindrome
1 parent 0ce2fef commit e3e4b1a

6 files changed

Lines changed: 67 additions & 10 deletions

File tree

src/Z.Core/System.String/String.IsAnagram.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
// Feedback / Feature Requests / Issues : http://zzzprojects.uservoice.com/forums/283927
55
// All ZZZ Projects products: Entity Framework Extensions / Bulk Operations / Extension Methods /Icon Library
66

7-
using System;
87
using System.Linq;
98

109
public static partial class Extensions
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// Copyright (c) 2015 ZZZ Projects. All rights reserved
2+
// Licensed under MIT License (MIT) (https://github.com/zzzprojects/Z.ExtensionMethods)
3+
// Website: http://www.zzzprojects.com/
4+
// Feedback / Feature Requests / Issues : http://zzzprojects.uservoice.com/forums/283927
5+
// All ZZZ Projects products: Entity Framework Extensions / Bulk Operations / Extension Methods /Icon Library
6+
7+
using System.Linq;
8+
using System.Text.RegularExpressions;
9+
10+
public static partial class Extensions
11+
{
12+
/// <summary>A string extension method that query if '@this' is palindrome.</summary>
13+
/// <param name="this">The @this to act on.</param>
14+
/// <returns>true if palindrome, false if not.</returns>
15+
public static bool IsPalindrome(this string @this)
16+
{
17+
// Keep only alphanumeric characters
18+
19+
var rgx = new Regex("[^a-zA-Z0-9]");
20+
@this = rgx.Replace(@this, "");
21+
return @this.SequenceEqual(@this.Reverse());
22+
}
23+
}

src/Z.Core/Z.Core.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -505,6 +505,7 @@
505505
<Compile Include="System.Single\_CoreObject\Single.InRange.cs" />
506506
<Compile Include="System.Single\_CoreObject\Single.NotIn.cs" />
507507
<Compile Include="System.String\String.Br2Nl.cs" />
508+
<Compile Include="System.String\String.IsPalindrome.cs" />
508509
<Compile Include="System.String\String.IsAnagram.cs" />
509510
<Compile Include="System.String\String.StripHtml.cs" />
510511
<Compile Include="System.String\String.ConcatWith.cs" />

test/Z.Core.Test/String.IsAnagram.cs renamed to test/Z.Core.Test/System.String/String.IsAnagram.cs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,21 +15,21 @@ public class System_String_IsAnagram
1515
public void IsAnagram()
1616
{
1717
// Type
18-
string @this = "abba";
18+
var @this = "abba";
1919

2020
// Examples
21-
bool value1 = @this.IsAnagram("abba"); // return true;
22-
bool value2 = @this.IsAnagram("abab"); // return false;
23-
bool value3 = @this.IsAnagram("aba"); // return false;
24-
bool value4 = @this.IsAnagram(""); // return false;
25-
bool value5 = @this.IsAnagram("aba b"); // return false;
21+
var value1 = @this.IsAnagram("abba"); // return true;
22+
var value2 = @this.IsAnagram("abab"); // return true;
23+
var value3 = @this.IsAnagram("aba"); // return false;
24+
var value4 = @this.IsAnagram(""); // return false;
25+
var value5 = @this.IsAnagram("aba b"); // return false;
2626

2727
// Unit Test
2828
Assert.IsTrue(value1);
29-
Assert.IsFalse(value2);
29+
Assert.IsTrue(value2);
3030
Assert.IsFalse(value3);
3131
Assert.IsFalse(value4);
3232
Assert.IsFalse(value5);
3333
}
3434
}
35-
}
35+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// Copyright (c) 2015 ZZZ Projects. All rights reserved
2+
// Licensed under MIT License (MIT) (https://github.com/zzzprojects/Z.ExtensionMethods)
3+
// Website: http://www.zzzprojects.com/
4+
// Feedback / Feature Requests / Issues : http://zzzprojects.uservoice.com/forums/283927
5+
// All ZZZ Projects products: Entity Framework Extensions / Bulk Operations / Extension Methods /Icon Library
6+
7+
using Microsoft.VisualStudio.TestTools.UnitTesting;
8+
9+
namespace Z.Core.Test
10+
{
11+
[TestClass]
12+
public class System_String_IsPalindrome
13+
{
14+
[TestMethod]
15+
public void IsPalindrome()
16+
{
17+
18+
// Examples
19+
bool value1 = "abba".IsPalindrome(); // return true;
20+
bool value2 = "ab ba".IsPalindrome(); // return true;
21+
bool value3 = "ab'ba".IsPalindrome(); // return true;
22+
bool value4 = "abca".IsPalindrome(); // return false;
23+
bool value5 = "ab b ab".IsPalindrome(); // return false;
24+
25+
// Unit Test
26+
Assert.IsTrue(value1);
27+
Assert.IsTrue(value2);
28+
Assert.IsTrue(value3);
29+
Assert.IsFalse(value4);
30+
Assert.IsFalse(value5);
31+
}
32+
}
33+
}

test/Z.Core.Test/Z.Core.Test.csproj

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,8 @@
6161
</Choose>
6262
<ItemGroup>
6363
<Compile Include="Properties\AssemblyInfo.cs" />
64-
<Compile Include="String.IsAnagram.cs" />
64+
<Compile Include="System.String\String.IsPalindrome.cs" />
65+
<Compile Include="System.String\String.IsAnagram.cs" />
6566
<Compile Include="System.Array\Array.ClearAll.cs" />
6667
<Compile Include="System.Array\Array.WithinIndex.cs" />
6768
<Compile Include="System.Boolean\Boolean.IfFalse.cs" />

0 commit comments

Comments
 (0)