add sources
This commit is contained in:
parent
fcd25eea52
commit
7ce631a648
21 changed files with 548 additions and 2 deletions
|
@ -0,0 +1,33 @@
|
|||
bool find1(int[] haystack, int needle)
|
||||
{
|
||||
foreach (v; haystack)
|
||||
{
|
||||
if (v == needle)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
int[] find2(int[] haystack, int needle)
|
||||
{
|
||||
while (haystack.length > 0 && haystack[0] != needle)
|
||||
{
|
||||
haystack = haystack[1 .. $];
|
||||
}
|
||||
|
||||
return haystack;
|
||||
}
|
||||
|
||||
unittest
|
||||
{
|
||||
int[] a = [];
|
||||
assert(find2(a, 5) == []);
|
||||
a = [ 1, 2, 3 ];
|
||||
assert(find2(a, 0) == []);
|
||||
assert(find2(a, 1).length == 3);
|
||||
assert(find2(a, 2).length == 2);
|
||||
assert(a[0 .. $ - find2(a, 3).length] == [ 1, 2 ]);
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue