Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
22
Task/Palindrome-detection/D/palindrome-detection-1.d
Normal file
22
Task/Palindrome-detection/D/palindrome-detection-1.d
Normal file
|
|
@ -0,0 +1,22 @@
|
|||
import std.traits, std.algorithm;
|
||||
|
||||
bool isPalindrome1(C)(in C[] s) pure /*nothrow*/
|
||||
if (isSomeChar!C) {
|
||||
auto s2 = s.dup;
|
||||
s2.reverse(); // works on Unicode too, not nothrow.
|
||||
return s == s2;
|
||||
}
|
||||
|
||||
void main() {
|
||||
alias pali = isPalindrome1;
|
||||
assert(pali(""));
|
||||
assert(pali("z"));
|
||||
assert(pali("aha"));
|
||||
assert(pali("sees"));
|
||||
assert(!pali("oofoe"));
|
||||
assert(pali("deified"));
|
||||
assert(!pali("Deified"));
|
||||
assert(pali("amanaplanacanalpanama"));
|
||||
assert(pali("ingirumimusnocteetconsumimurigni"));
|
||||
assert(pali("salà las"));
|
||||
}
|
||||
26
Task/Palindrome-detection/D/palindrome-detection-2.d
Normal file
26
Task/Palindrome-detection/D/palindrome-detection-2.d
Normal file
|
|
@ -0,0 +1,26 @@
|
|||
import std.traits;
|
||||
|
||||
bool isPalindrome2(C)(in C[] s) pure if (isSomeChar!C) {
|
||||
dchar[] dstr;
|
||||
foreach (dchar c; s) // not nothrow
|
||||
dstr ~= c;
|
||||
|
||||
for (int i; i < dstr.length / 2; i++)
|
||||
if (dstr[i] != dstr[$ - i - 1])
|
||||
return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
void main() {
|
||||
alias isPalindrome2 pali;
|
||||
assert(pali(""));
|
||||
assert(pali("z"));
|
||||
assert(pali("aha"));
|
||||
assert(pali("sees"));
|
||||
assert(!pali("oofoe"));
|
||||
assert(pali("deified"));
|
||||
assert(!pali("Deified"));
|
||||
assert(pali("amanaplanacanalpanama"));
|
||||
assert(pali("ingirumimusnocteetconsumimurigni"));
|
||||
assert(pali("salà las"));
|
||||
}
|
||||
39
Task/Palindrome-detection/D/palindrome-detection-3.d
Normal file
39
Task/Palindrome-detection/D/palindrome-detection-3.d
Normal file
|
|
@ -0,0 +1,39 @@
|
|||
import std.stdio, core.exception, std.traits;
|
||||
|
||||
// assume alloca() to be pure for this program
|
||||
extern(C) pure nothrow void* alloca(in size_t size);
|
||||
|
||||
bool isPalindrome3(C)(in C[] s) pure if (isSomeChar!C) {
|
||||
auto p = cast(dchar*)alloca(s.length * 4);
|
||||
if (p == null)
|
||||
// no fallback heap allocation used
|
||||
throw new OutOfMemoryError();
|
||||
dchar[] dstr = p[0 .. s.length];
|
||||
|
||||
// use std.utf.stride for an even lower level version
|
||||
int i = 0;
|
||||
foreach (dchar c; s) { // not nothrow
|
||||
dstr[i] = c;
|
||||
i++;
|
||||
}
|
||||
dstr = dstr[0 .. i];
|
||||
|
||||
foreach (j; 0 .. dstr.length / 2)
|
||||
if (dstr[j] != dstr[$ - j - 1])
|
||||
return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
void main() {
|
||||
alias isPalindrome3 pali;
|
||||
assert(pali(""));
|
||||
assert(pali("z"));
|
||||
assert(pali("aha"));
|
||||
assert(pali("sees"));
|
||||
assert(!pali("oofoe"));
|
||||
assert(pali("deified"));
|
||||
assert(!pali("Deified"));
|
||||
assert(pali("amanaplanacanalpanama"));
|
||||
assert(pali("ingirumimusnocteetconsumimurigni"));
|
||||
assert(pali("salà las"));
|
||||
}
|
||||
23
Task/Palindrome-detection/D/palindrome-detection-4.d
Normal file
23
Task/Palindrome-detection/D/palindrome-detection-4.d
Normal file
|
|
@ -0,0 +1,23 @@
|
|||
bool isPalindrome4(in string str) pure nothrow {
|
||||
if (str.length == 0) return true;
|
||||
immutable(char)* s = str.ptr;
|
||||
immutable(char)* t = &(str[$ - 1]);
|
||||
while (s < t)
|
||||
if (*s++ != *t--) // ugly
|
||||
return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
void main() {
|
||||
alias isPalindrome4 pali;
|
||||
assert(pali(""));
|
||||
assert(pali("z"));
|
||||
assert(pali("aha"));
|
||||
assert(pali("sees"));
|
||||
assert(!pali("oofoe"));
|
||||
assert(pali("deified"));
|
||||
assert(!pali("Deified"));
|
||||
assert(pali("amanaplanacanalpanama"));
|
||||
assert(pali("ingirumimusnocteetconsumimurigni"));
|
||||
//assert(pali("salà las"));
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue