September Morn Update
This commit is contained in:
parent
4e2d22a71d
commit
aac6731f2c
6856 changed files with 141342 additions and 21127 deletions
|
|
@ -1,5 +1,5 @@
|
|||
Function Test-Palindrome( [String] $Text ){
|
||||
$CharArray = $Text.ToCharArray()
|
||||
[Array]::Reverse($CharArray)
|
||||
$Text -like (-join $CharArray)
|
||||
$Text -eq [string]::join('', $CharArray)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,50 @@
|
|||
Function Test-Palindrome {
|
||||
[CmdletBinding()]
|
||||
Param(
|
||||
[Parameter(ValueFromPipeline)]
|
||||
[string[]]$Text
|
||||
)
|
||||
|
||||
process {
|
||||
:stringLoop foreach ($T in $Text)
|
||||
{
|
||||
# Normalize Unicode combining characters,
|
||||
# so character á compares the same as (a+combining accent)
|
||||
$T = $T.Normalize([Text.NormalizationForm]::FormC)
|
||||
|
||||
# Remove anything from outside the Unicode category
|
||||
# "Letter from any language"
|
||||
$T = $T -replace '\P{L}', ''
|
||||
|
||||
# Walk from each end of the string inwards,
|
||||
# comparing a char at a time.
|
||||
# Avoids string copy / reverse overheads.
|
||||
$Left, $Right = 0, [math]::Max(0, ($T.Length - 1))
|
||||
while ($Left -lt $Right)
|
||||
{
|
||||
if ($T[$Left] -ne $T[$Right])
|
||||
{
|
||||
# return early if string is not a palindrome
|
||||
[PSCustomObject]@{
|
||||
Text = $T
|
||||
IsPalindrome = $False
|
||||
}
|
||||
continue stringLoop
|
||||
}
|
||||
else
|
||||
{
|
||||
$Left++
|
||||
$Right--
|
||||
}
|
||||
}
|
||||
|
||||
# made it to here, then string is a palindrome
|
||||
[PSCustomObject]@{
|
||||
Text = $T
|
||||
IsPalindrome = $True
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
'ánu-ná', 'nowt' | Test-Palindrome
|
||||
Loading…
Add table
Add a link
Reference in a new issue