September 2017 Update

This commit is contained in:
Ingy döt Net 2017-09-23 10:01:46 +02:00
parent bba7bfd280
commit ba8067c3b7
14570 changed files with 153136 additions and 63871 deletions

View file

@ -1,42 +1,17 @@
integer
void
search(list l, text s)
{
integer i;
i = 0;
while (i < l_length(l)) {
if (!compare(l_q_text(l, i), s)) {
while (i < ~l) {
if (!compare(l[i], s)) {
break;
}
i += 1;
}
if (i == l_length(l)) {
i = -1;
}
return i;
}
void
search_for(list l, ...)
{
integer i;
i = 1;
while (i < count()) {
integer index;
index = search(l, $i);
o_text($i);
if (index == -1) {
o_text(" is not in the haystack\n");
} else {
o_plan(" is at ", index, "\n");
}
i += 1;
}
o_(s, " is ", i == ~l ? "not in the haystack" : cat("at ", itoa(i)), "\n");
}
integer
@ -46,7 +21,7 @@ main(void)
l = l_effect("Zig", "Zag", "Wally", "Ronald", "Bush", "Krusty",
"Charlie", "Bush", "Boz", "Zag");
search_for(l, "Bush", "Washington", "Zag");
__ucall(search, 1, 1, l, "Bush", "Washington", "Zag");
return 0;
}

View file

@ -1,17 +1,16 @@
#import system.
#import system'routines.
#import extensions.
import system'routines.
import extensions.
#symbol program =
program =
[
#var haystack := ("Zig", "Zag", "Wally", "Ronald", "Bush", "Krusty", "Charlie", "Bush", "Bozo").
var haystack := ("Zig", "Zag", "Wally", "Ronald", "Bush", "Krusty", "Charlie", "Bush", "Bozo").
("Washington", "Bush") run &each: needle
("Washington", "Bush") forEach(:needle)
[
#var index := haystack indexOf:needle.
var index := haystack indexOfElement:needle.
(index == -1)
? [ console writeLine:needle:" is not in haystack" ]
! [ console writeLine:index:" ":needle ].
if (index == -1)
[ console printLine(needle," is not in haystack") ];
[ console printLine(index," ",needle) ].
].
].

View file

@ -0,0 +1,61 @@
' FB 1.05.0 Win64
' Works FB 1.05.0 Linux Mint 64
Function tryFindString(s() As String, search As String, ByRef index As Integer) As Boolean
Dim length As Integer = UBound(s) - LBound(s) + 1
If length = 0 Then
index = LBound(s) - 1 '' outside array
Return False
End If
For i As Integer = LBound(s) To UBound(s)
If s(i) = search Then
index = i '' first occurrence
Return True
End If
Next
index = LBound(s) - 1 '' outside array
Return False
End Function
Function tryFindLastString(s() As String, search As String, ByRef index As Integer) As Boolean
Dim length As Integer = UBound(s) - LBound(s) + 1
If length = 0 Then
index = LBound(s) - 1 '' outside array
Return False
End If
Dim maxIndex As Integer = LBound(s) - 1 '' outside array
For i As Integer = LBound(s) To UBound(s)
If s(i) = search Then
maxIndex = i
End If
Next
If maxIndex > LBound(s) - 1 Then
index = maxIndex '' last occurrence
Return True
Else
Return False
End If
End Function
Dim haystack(1 To 9) As String = {"Zig", "Zag", "Wally", "Ronald", "Bush", "Krusty", "Charlie", "Bush", "Bozo"}
Dim needle(1 To 4) As String = {"Zag", "Krusty", "Washington", "Bush"}
Dim As Integer index
Dim As Boolean found
For i As Integer = 1 To 4
found = tryFindString(haystack(), needle(i), index)
If found Then
Print needle(i); " found first at index"; index
Else
Print needle(i); " is not present"
End If
Next
found = tryFindLastString(haystack(), needle(4), index)
If found Then
Print needle(4); " found last at index"; index
Else
Print needle(4); " is not present"
End If
Print
Print "Press any key to quit"
Sleep

View file

@ -0,0 +1,16 @@
Public Sub Main()
Dim sHaystack As String[] = ["Zig", "Zag", "Wally", "Ronald", "Bush", "Krusty", "Charlie", "Bush", "Boz", "Zag"]
Dim sNeedle As String = "Charlie"
Dim sOutput As String = "No needle found!"
Dim siCount As Short
For siCount = 0 To sHaystack.Max
If sNeedle = sHaystack[siCount] Then
sOutPut = sNeedle & " found at index " & Str(siCount)
Break
End If
Next
Print sOutput
End

View file

@ -1,3 +1,4 @@
Haystack ;:^:_1@(] ,. [ ((<'is not in haystack')"_)`(#@[ I.@:= ])`(8!:0@])} i.) Needles
Washington is not in haystack
Bush 4
Needles e. Haystack
0 1
1 2 3 4 5 6 7 8 9 e. 2 3 5 60
0 1 1 0 1 0 0 0 0

View file

@ -1,8 +1,4 @@
msg=: (<'is not in haystack')"_ NB. not found message
idxmissing=: #@[ I.@:= ] NB. indices of items not found
fmtdata=: 8!:0@] NB. format atoms as boxed strings
findLastIndex=: ;:inv@(] ,. [ msg`idxmissing`fmtdata} i:)
Haystack findLastIndex Needles NB. usage
Washington is not in haystack
Bush 7
1 2 3 4 5 6 7 8 9 I. 2 3 5 60 6.66
1 2 4 9 6
(;:'eight five four nine one seven six three two') I. ;:'two three five sixty'
8 7 1 7

View file

@ -0,0 +1,3 @@
Haystack ;:^:_1@(] ,. [ ((<'is not in haystack')"_)`(#@[ I.@:= ])`(8!:0@])} i.) Needles
Washington is not in haystack
Bush 4

View file

@ -0,0 +1,8 @@
msg=: (<'is not in haystack')"_ NB. not found message
idxmissing=: #@[ I.@:= ] NB. indices of items not found
fmtdata=: 8!:0@] NB. format atoms as boxed strings
findLastIndex=: ;:inv@(] ,. [ msg`idxmissing`fmtdata} i:)
Haystack findLastIndex Needles NB. usage
Washington is not in haystack
Bush 7

View file

@ -5,11 +5,13 @@
# => 1
["a","b","c","b"]
| index("x") as $ix
| if $ix then $ix else error("element not found") end
| index("x") // error("element not found")
# => jq: error: element not found
# Extra task - the last element of an array can be retrieved
# using -1 as an index:
# using `rindex/` or by using -1 as an index into the array produced by `indices/1`:
["a","b","c","b","d"] | rindex("b")
# => 3
["a","b","c","b","d"] | indices("b")[-1]
# => 3

View file

@ -0,0 +1,14 @@
// version 1.0.6 (search_list.kt)
fun main(args: Array<String>) {
val haystack = listOf("Zig", "Zag", "Wally", "Ronald", "Bush", "Krusty", "Charlie", "Bush", "Boz", "Zag")
println(haystack)
var needle = "Zag"
var index = haystack.indexOf(needle)
val index2 = haystack.lastIndexOf(needle)
println("\n'$needle' first occurs at index $index of the list")
println("'$needle' last occurs at index $index2 of the list\n")
needle = "Donald"
index = haystack.indexOf(needle)
if (index == -1) throw Exception("$needle does not occur in the list")
}

View file

@ -1,9 +0,0 @@
haystack: ["Zig","Zag","Wally","Ronald","Bush","Zig","Zag","Krusty","Charlie","Bush","Bozo"];
needle: "Zag";
findneedle(needle, haystack, [opt]):=block([idx],
idx: sublist_indices(haystack, lambda([w], w=needle)),
if emptyp(idx) then throw('notfound),
if emptyp(opt) then return(idx),
opt: first(opt),
if opt='f then first(idx) else if opt='l then last(idx) else throw('unknownmode));

View file

@ -1,12 +0,0 @@
(%i32) catch(findneedle("Zag", haystack, 'f));
(%o32) 2
(%i33) catch(findneedle("Zag", haystack, 'l));
(%o33) 7
(%i34) catch(findneedle("Washington", haystack));
(%o34) notfound
(%i35) catch(findneedle("Bush", haystack, 'f));
(%o35) 5
(%i36) catch(findneedle("Zag", haystack));
(%o36) [2, 7]
(%i37) catch(findneedle("Zag", haystack, 'l));
(%o37) 7

View file

@ -1,18 +1,16 @@
use Structure;
use Collection;
bundle Default {
class Test {
function : Main(args : String[]) ~ Nil {
haystack := ["Zig","Zag","Wally","Ronald","Bush","Krusty","Charlie","Bush","Bozo"];
values := CompareVector->New();
each(i : haystack) {
values->AddBack(haystack[i]->As(Compare));
};
class Test {
function : Main(args : String[]) ~ Nil {
haystack := ["Zig","Zag","Wally","Ronald","Bush","Krusty","Charlie","Bush","Bozo"];
values := CompareVector->New();
each(i : haystack) {
values->AddBack(haystack[i]->As(Compare));
};
needles := ["Washington", "Bush"];
each(i : needles) {
values->Has(needles[i]->As(Compare))->PrintLine();
};
}
needles := ["Washington", "Bush"];
each(i : needles) {
values->Has(needles[i]->As(Compare))->PrintLine();
};
}
}

View file

@ -1,8 +0,0 @@
NSArray *haystack = [NSArray arrayWithObjects:@"Zig",@"Zag",@"Wally",@"Ronald",@"Bush",@"Krusty",@"Charlie",@"Bush",@"Bozo",nil];
for (id needle in [NSArray arrayWithObjects:@"Washington",@"Bush",nil]) {
int index = [haystack indexOfObject:needle];
if (index == NSNotFound)
NSLog(@"%@ is not in haystack", needle);
else
NSLog(@"%i %@", index, needle);
}

View file

@ -1,10 +0,0 @@
NSArray *haystack = [NSArray arrayWithObjects:@"Zig",@"Zag",@"Wally",@"Ronald",@"Bush",@"Krusty",@"Charlie",@"Bush",@"Bozo",nil];
id needle;
NSEnumerator *enm = [[NSArray arrayWithObjects:@"Washington",@"Bush",nil] objectEnumerator];
while ((needle = [enm nextObject]) != nil) {
int index = [haystack indexOfObject:needle];
if (index == NSNotFound)
NSLog(@"%@ is not in haystack", needle);
else
NSLog(@"%i %@", index, needle);
}

View file

@ -0,0 +1,19 @@
-- ordered collections always return the first hit
a = .array~of(1,2,3,4,4,5)
say a~index(4)
a2 = .array~new(5,5) -- multidimensional
a2[3,3] = 4
-- the returned index is an array of values
say a2~index(4)~makestring('line', ',')
-- Note, list indexes are assigned when an item is added and
-- are not tied to relative position
l = .list~of(1,2,3,4,4,5)
say l~index(4)
q = .queue~of(1,2,3,4,4,5)
say q~index(4)
-- directories are unordered, so it is
-- undertermined which one is returned
d = .directory~new
d["foo"] = 4
d["bar"] = 4
say d~index(4)

View file

@ -0,0 +1,15 @@
variable haystack = ["Zig","Zag","Wally","Ronald","Bush","Krusty","Charlie","Bush","Bozo","Ronald"];
define find(needle)
{
variable i = where(haystack == needle);
if (length(i)) {
% print(sprintf("%s: first=%d, last=%d", needle, i[0], i[-1]));
return(i[0], i[-1]);
}
else
throw ApplicationError, "an exception";
}
($1, $2) = find("Ronald"); % returns 3, 9
($1, $2) = find("McDonald"); % throws ApplicationError, labelled "an exception"

View file

@ -0,0 +1,2 @@
L("Krusty","Charlie","Bozo","Bozo").index("Charlie") //--> 1
L("Krusty","Charlie","Bozo","Bozo").index("fred") //--> throws index error

View file

@ -0,0 +1,3 @@
haystack:=L("Krusty","Charlie","Bozo","Bozo");
haystack.filterNs('==("Bozo"))[-1]; // -->3, indexError if not found
haystack.len() - 1 - haystack.reverse().index("Bozo"); // or this

View file

@ -0,0 +1,3 @@
haystack:=Data(0,String,"Krusty","Charlie","Bozo","Bozo");
if((n:=haystack.findString("Charlie")) != Void) n else throw(Exception.IndexError);
//-->7