Initial data commit
This commit is contained in:
parent
72d218235f
commit
f23f22d71c
199087 changed files with 3378941 additions and 0 deletions
18
Task/Search-a-list/D/search-a-list.d
Normal file
18
Task/Search-a-list/D/search-a-list.d
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
import std.algorithm, std.range, std.string;
|
||||
|
||||
auto firstIndex(R, T)(R hay, T needle) {
|
||||
auto i = countUntil(hay, needle);
|
||||
if (i == -1)
|
||||
throw new Exception("No needle found in haystack");
|
||||
return i;
|
||||
}
|
||||
|
||||
auto lastIndex(R, T)(R hay, T needle) {
|
||||
return walkLength(hay) - firstIndex(retro(hay), needle) - 1;
|
||||
}
|
||||
|
||||
void main() {
|
||||
auto h = split("Zig Zag Wally Ronald Bush Krusty Charlie Bush Bozo");
|
||||
assert(firstIndex(h, "Bush") == 4);
|
||||
assert(lastIndex(h, "Bush") == 7);
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue