Data update
This commit is contained in:
parent
5150844a7d
commit
4bb20c9b71
7735 changed files with 38060 additions and 199180 deletions
|
|
@ -1,42 +0,0 @@
|
|||
with Ada.Strings.Unbounded; use Ada.Strings.Unbounded;
|
||||
with Ada.Text_IO; use Ada.Text_IO;
|
||||
|
||||
procedure Test_List_Index is
|
||||
Not_In : exception;
|
||||
|
||||
type List is array (Positive range <>) of Unbounded_String;
|
||||
|
||||
function Index (Haystack : List; Needle : String) return Positive is
|
||||
begin
|
||||
for Index in Haystack'Range loop
|
||||
if Haystack (Index) = Needle then
|
||||
return Index;
|
||||
end if;
|
||||
end loop;
|
||||
raise Not_In;
|
||||
end Index;
|
||||
|
||||
-- Functions to create lists
|
||||
function "+" (X, Y : String) return List is
|
||||
begin
|
||||
return (1 => To_Unbounded_String (X), 2 => To_Unbounded_String (Y));
|
||||
end "+";
|
||||
|
||||
function "+" (X : List; Y : String) return List is
|
||||
begin
|
||||
return X & (1 => To_Unbounded_String (Y));
|
||||
end "+";
|
||||
|
||||
Haystack : List := "Zig"+"Zag"+"Wally"+"Ronald"+"Bush"+"Krusty"+"Charlie"+"Bush"+"Bozo";
|
||||
|
||||
procedure Check (Needle : String) is
|
||||
begin
|
||||
Put (Needle);
|
||||
Put_Line ("at" & Positive'Image (Index (Haystack, Needle)));
|
||||
exception
|
||||
when Not_In => Put_Line (" is not in");
|
||||
end Check;
|
||||
begin
|
||||
Check ("Washington");
|
||||
Check ("Bush");
|
||||
end Test_List_Index;
|
||||
|
|
@ -3,6 +3,7 @@ haystack: [Zig Zag Wally Ronald Bush Krusty Charlie Bush Bozo]
|
|||
loop [Bush Washington] 'needle [
|
||||
i: index haystack needle
|
||||
|
||||
if? empty? i -> panic ~"|needle| is not in haystack"
|
||||
else -> print [i needle]
|
||||
switch null? i
|
||||
-> panic ~"|needle| is not in haystack"
|
||||
-> print [i needle]
|
||||
]
|
||||
|
|
|
|||
|
|
@ -1,63 +0,0 @@
|
|||
*> This is written to COBOL85, which does not include exceptions.
|
||||
IDENTIFICATION DIVISION.
|
||||
PROGRAM-ID. Search-List.
|
||||
|
||||
DATA DIVISION.
|
||||
WORKING-STORAGE SECTION.
|
||||
01 haystack-area.
|
||||
78 Haystack-Size VALUE 10.
|
||||
03 haystack-data.
|
||||
05 FILLER PIC X(7) VALUE "Zig".
|
||||
05 FILLER PIC X(7) VALUE "Zag".
|
||||
05 FILLER PIC X(7) VALUE "Wally".
|
||||
05 FILLER PIC X(7) VALUE "Ronald".
|
||||
05 FILLER PIC X(7) VALUE "Bush".
|
||||
05 FILLER PIC X(7) VALUE "Krusty".
|
||||
05 FILLER PIC X(7) VALUE "Charlie".
|
||||
05 FILLER PIC X(7) VALUE "Bush".
|
||||
05 FILLER PIC X(7) VALUE "Boz".
|
||||
05 FILLER PIC X(7) VALUE "Zag".
|
||||
|
||||
03 haystack-table REDEFINES haystack-data.
|
||||
05 haystack PIC X(7) OCCURS Haystack-Size TIMES
|
||||
INDEXED BY haystack-index.
|
||||
|
||||
01 needle PIC X(7).
|
||||
|
||||
PROCEDURE DIVISION.
|
||||
main.
|
||||
MOVE "Bush" TO needle
|
||||
PERFORM find-needle
|
||||
|
||||
MOVE "Goofy" TO needle
|
||||
PERFORM find-needle
|
||||
|
||||
* *> Extra task
|
||||
MOVE "Bush" TO needle
|
||||
PERFORM find-last-of-needle
|
||||
|
||||
GOBACK
|
||||
.
|
||||
|
||||
find-needle.
|
||||
SEARCH haystack
|
||||
AT END
|
||||
DISPLAY needle " not found."
|
||||
|
||||
WHEN haystack (haystack-index) = needle
|
||||
DISPLAY "Found " needle " at " haystack-index "."
|
||||
END-SEARCH
|
||||
.
|
||||
|
||||
find-last-of-needle.
|
||||
PERFORM VARYING haystack-index FROM Haystack-Size BY -1
|
||||
UNTIL haystack-index = 0
|
||||
OR haystack (haystack-index) = needle
|
||||
END-PERFORM
|
||||
|
||||
IF haystack-index = 0
|
||||
DISPLAY needle " not found."
|
||||
ELSE
|
||||
DISPLAY "Found last of " needle " at " haystack-index "."
|
||||
END-IF
|
||||
.
|
||||
|
|
@ -1,9 +1,9 @@
|
|||
import system'routines;
|
||||
import extensions;
|
||||
|
||||
public program()
|
||||
public Program()
|
||||
{
|
||||
var haystack := new string[]{"Zig", "Zag", "Wally", "Ronald", "Bush", "Krusty", "Charlie", "Bush", "Bozo"};
|
||||
auto haystack := new string[]{"Zig", "Zag", "Wally", "Ronald", "Bush", "Krusty", "Charlie", "Bush", "Bozo"};
|
||||
|
||||
new string[]{"Washington", "Bush"}.forEach::(needle)
|
||||
{
|
||||
|
|
@ -11,11 +11,11 @@ public program()
|
|||
|
||||
if (index == -1)
|
||||
{
|
||||
console.printLine(needle," is not in haystack")
|
||||
Console.printLine(needle," is not in haystack")
|
||||
}
|
||||
else
|
||||
{
|
||||
console.printLine(needle, " - ", index)
|
||||
Console.printLine(needle, " - ", index)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,26 +0,0 @@
|
|||
(defun get-index (item list)
|
||||
"Return index of first ITEM in LIST.
|
||||
Display error message if ITEM is not
|
||||
part of LIST."
|
||||
(if (seq-position list item) ; if it's in LIST
|
||||
(seq-position list item) ; return index
|
||||
;; else, provide an error message
|
||||
(message "%s is not an element in %s." item list)))
|
||||
|
||||
;; My thanks to Protesileos Stavrou (aka "Prot") for
|
||||
;; key ideas for the section below, including reversing
|
||||
;; the list in order to easily find the last item in it.
|
||||
(defun last-position (item list)
|
||||
"Return last ITEM index in LIST."
|
||||
;; test if ITEM is in LIST
|
||||
(if (seq-position list item) ; if it's in LIST
|
||||
(progn
|
||||
;; subtract the adjusted length of the list from
|
||||
;; the position of ITEM in the reverse of LIST
|
||||
(-
|
||||
;; 1- corrects for length being 1 based, but seq-position is 0 based
|
||||
(1- (length list))
|
||||
;; reverse list because
|
||||
;; seq-position searches from beginning of list
|
||||
(seq-position (nreverse list) item)))
|
||||
(message "%s is not an element in %s." item list)))
|
||||
|
|
@ -1,46 +0,0 @@
|
|||
include std/search.e
|
||||
include std/console.e
|
||||
|
||||
--the string "needle" and example haystacks to test the procedure
|
||||
sequence searchStr1 = "needle"
|
||||
sequence haystack1 = { "needle", "needle", "noodle", "node", "need", "needle ", "needle" }
|
||||
sequence haystack2 = {"spoon", "fork", "hay", "knife", "needle", "barn", "etcetera", "more hay", "needle", "a cow", "farmer", "needle", "dirt"}
|
||||
sequence haystack3 = {"needle"}
|
||||
sequence haystack4 = {"no", "need le s", "in", "this", "haystack"}
|
||||
sequence haystack5 = {"knee", "needle", "dull", "needle"}
|
||||
sequence haystack6 = {}
|
||||
|
||||
--search procedure with console output
|
||||
procedure haystackSearch(sequence hStack)
|
||||
sequence foundNeedles = find_all(searchStr1, hStack)
|
||||
puts(1,"---------------------------------\r\n")
|
||||
if object(foundNeedles) and length(foundNeedles) > 0 then
|
||||
printf(1, "First needle found at index %d \r\n", foundNeedles[1])
|
||||
|
||||
if length(foundNeedles) > 1 then
|
||||
printf(1, "Last needle found at index %d \r\n", foundNeedles[length(foundNeedles)] )
|
||||
|
||||
for i = 1 to length(foundNeedles) do
|
||||
printf(1, "Needle #%d ", i)
|
||||
printf(1, "was at index %d .\r\n", foundNeedles[i])
|
||||
end for
|
||||
|
||||
else
|
||||
puts(1, "There was only one needle found in this haystack. \r\n")
|
||||
end if
|
||||
|
||||
else
|
||||
puts(1, "Simulated exception - No needles found in this haystack.\r\n")
|
||||
end if
|
||||
|
||||
end procedure
|
||||
|
||||
--runs the procedure on all haystacks
|
||||
haystackSearch(haystack1)
|
||||
haystackSearch(haystack2)
|
||||
haystackSearch(haystack3)
|
||||
haystackSearch(haystack4)
|
||||
haystackSearch(haystack5)
|
||||
haystackSearch(haystack6)
|
||||
--wait for user to press a key to exit
|
||||
any_key()
|
||||
|
|
@ -1,9 +1,7 @@
|
|||
-->
|
||||
<span style="color: #008080;">constant</span> <span style="color: #000000;">s</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{</span><span style="color: #008000;">"Zig"</span><span style="color: #0000FF;">,</span> <span style="color: #008000;">"Zag"</span><span style="color: #0000FF;">,</span> <span style="color: #008000;">"Wally"</span><span style="color: #0000FF;">,</span> <span style="color: #008000;">"Ronald"</span><span style="color: #0000FF;">,</span> <span style="color: #008000;">"Bush"</span><span style="color: #0000FF;">,</span> <span style="color: #008000;">"Krusty"</span><span style="color: #0000FF;">,</span> <span style="color: #008000;">"Charlie"</span><span style="color: #0000FF;">,</span> <span style="color: #008000;">"Bush"</span><span style="color: #0000FF;">,</span> <span style="color: #008000;">"Boz"</span><span style="color: #0000FF;">,</span> <span style="color: #008000;">"Zag"</span><span style="color: #0000FF;">}</span>
|
||||
constant s = {"Zig", "Zag", "Wally", "Ronald", "Bush", "Krusty", "Charlie", "Bush", "Boz", "Zag"}
|
||||
|
||||
<span style="color: #004080;">integer</span> <span style="color: #000000;">r</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">find</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"Zag"</span><span style="color: #0000FF;">,</span><span style="color: #000000;">s</span><span style="color: #0000FF;">)</span> <span style="color: #0000FF;">?</span><span style="color: #000000;">r</span> <span style="color: #000080;font-style:italic;">-- 2 (first)</span>
|
||||
<span style="color: #000000;">r</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">find</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"Zag"</span><span style="color: #0000FF;">,</span><span style="color: #000000;">s</span><span style="color: #0000FF;">,</span><span style="color: #000000;">r</span><span style="color: #0000FF;">+</span><span style="color: #000000;">1</span><span style="color: #0000FF;">)</span> <span style="color: #0000FF;">?</span><span style="color: #000000;">r</span> <span style="color: #000080;font-style:italic;">-- 10 (next)</span>
|
||||
<span style="color: #000000;">r</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">find</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"Zag"</span><span style="color: #0000FF;">,</span><span style="color: #000000;">s</span><span style="color: #0000FF;">,</span><span style="color: #000000;">r</span><span style="color: #0000FF;">+</span><span style="color: #000000;">1</span><span style="color: #0000FF;">)</span> <span style="color: #0000FF;">?</span><span style="color: #000000;">r</span> <span style="color: #000080;font-style:italic;">-- 0 (no more)</span>
|
||||
<span style="color: #000000;">r</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">rfind</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"Zag"</span><span style="color: #0000FF;">,</span><span style="color: #000000;">s</span><span style="color: #0000FF;">)</span> <span style="color: #0000FF;">?</span><span style="color: #000000;">r</span> <span style="color: #000080;font-style:italic;">-- 10 (last)</span>
|
||||
<span style="color: #000000;">r</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">find</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"Zog"</span><span style="color: #0000FF;">,</span><span style="color: #000000;">s</span><span style="color: #0000FF;">)</span> <span style="color: #0000FF;">?</span><span style="color: #000000;">r</span> <span style="color: #000080;font-style:italic;">-- 0 (none)</span>
|
||||
<!--
|
||||
integer r = find("Zag",s) ?r -- 2 (first)
|
||||
r = find("Zag",s,r+1) ?r -- 10 (next)
|
||||
r = find("Zag",s,r+1) ?r -- 0 (no more)
|
||||
r = rfind("Zag",s) ?r -- 10 (last)
|
||||
r = find("Zog",s) ?r -- 0 (none)
|
||||
|
|
|
|||
|
|
@ -1,12 +0,0 @@
|
|||
function index($haystack,$needle) {
|
||||
$index = $haystack.IndexOf($needle)
|
||||
if($index -eq -1) {
|
||||
Write-Warning "$needle is absent"
|
||||
} else {
|
||||
$index
|
||||
}
|
||||
|
||||
}
|
||||
$haystack = @("word", "phrase", "preface", "title", "house", "line", "chapter", "page", "book", "house")
|
||||
index $haystack "house"
|
||||
index $haystack "paragraph"
|
||||
|
|
@ -1,57 +0,0 @@
|
|||
function Find-Needle
|
||||
{
|
||||
[CmdletBinding()]
|
||||
[OutputType([int])]
|
||||
Param
|
||||
(
|
||||
[Parameter(Mandatory=$true, Position=0)]
|
||||
[string]
|
||||
$Needle,
|
||||
|
||||
[Parameter(Mandatory=$true, Position=1)]
|
||||
[string[]]
|
||||
$Haystack,
|
||||
|
||||
[switch]
|
||||
$LastIndex
|
||||
)
|
||||
|
||||
if ($LastIndex)
|
||||
{
|
||||
$index = [Array]::LastIndexOf($Haystack,$Needle)
|
||||
|
||||
if ($index -eq -1)
|
||||
{
|
||||
Write-Verbose "Needle not found in Haystack"
|
||||
return $index
|
||||
}
|
||||
|
||||
if ((($Haystack | Group-Object | Where-Object Count -GT 1).Group).IndexOf($Needle) -ne -1)
|
||||
{
|
||||
Write-Verbose "Last needle found in Haystack at index $index"
|
||||
}
|
||||
else
|
||||
{
|
||||
Write-Verbose "Needle found in Haystack at index $index (No duplicates were found)"
|
||||
}
|
||||
|
||||
return $index
|
||||
}
|
||||
else
|
||||
{
|
||||
$index = [Array]::IndexOf($Haystack,$Needle)
|
||||
|
||||
if ($index -eq -1)
|
||||
{
|
||||
Write-Verbose "Needle not found in Haystack"
|
||||
}
|
||||
else
|
||||
{
|
||||
Write-Verbose "Needle found in Haystack at index $index"
|
||||
}
|
||||
|
||||
return $index
|
||||
}
|
||||
}
|
||||
|
||||
$haystack = @("word", "phrase", "preface", "title", "house", "line", "chapter", "page", "book", "house")
|
||||
|
|
@ -1 +0,0 @@
|
|||
Find-Needle "house" $haystack
|
||||
|
|
@ -1 +0,0 @@
|
|||
Find-Needle "house" $haystack -Verbose
|
||||
|
|
@ -1 +0,0 @@
|
|||
Find-Needle "house" $haystack -LastIndex -Verbose
|
||||
|
|
@ -1 +0,0 @@
|
|||
Find-Needle "title" $haystack -LastIndex -Verbose
|
||||
|
|
@ -1 +0,0 @@
|
|||
Find-Needle "something" $haystack -Verbose
|
||||
|
|
@ -1,38 +0,0 @@
|
|||
REBOL [
|
||||
Title: "List Indexing"
|
||||
URL: http://rosettacode.org/wiki/Index_in_a_list
|
||||
]
|
||||
|
||||
locate: func [
|
||||
"Find the index of a string (needle) in string collection (haystack)."
|
||||
haystack [series!] "List of values to search."
|
||||
needle [string!] "String to find in value list."
|
||||
/largest "Return the largest index if more than one needle."
|
||||
/local i
|
||||
][
|
||||
i: either largest [
|
||||
find/reverse tail haystack needle][find haystack needle]
|
||||
either i [return index? i][
|
||||
throw reform [needle "is not in haystack."]
|
||||
]
|
||||
]
|
||||
|
||||
; Note that REBOL uses 1-base lists instead of 0-based like most
|
||||
; computer languages. Therefore, the index provided will be one
|
||||
; higher than other results on this page.
|
||||
|
||||
haystack: parse "Zig Zag Wally Ronald Bush Krusty Charlie Bush Bozo" none
|
||||
|
||||
print "Search for first occurance:"
|
||||
foreach needle ["Washington" "Bush"] [
|
||||
print catch [
|
||||
reform [needle "=>" locate haystack needle]
|
||||
]
|
||||
]
|
||||
|
||||
print [crlf "Search for last occurance:"]
|
||||
foreach needle ["Washington" "Bush"] [
|
||||
print catch [
|
||||
reform [needle "=>" locate/largest haystack needle]
|
||||
]
|
||||
]
|
||||
|
|
@ -1,28 +0,0 @@
|
|||
data = "foo,bar,baz,quux,quuux,quuuux,bazola,ztesch,foo,bar,thud,grunt," &_
|
||||
"foo,bar,bletch,foo,bar,fum,fred,jim,sheila,barney,flarp,zxc," &_
|
||||
"spqr,wombat,shme,foo,bar,baz,bongo,spam,eggs,snork,foo,bar," &_
|
||||
"zot,blarg,wibble,toto,titi,tata,tutu,pippo,pluto,paperino,aap," &_
|
||||
"noot,mies,oogle,foogle,boogle,zork,gork,bork"
|
||||
|
||||
haystack = Split(data,",")
|
||||
|
||||
Do
|
||||
WScript.StdOut.Write "Word to search for? (Leave blank to exit) "
|
||||
needle = WScript.StdIn.ReadLine
|
||||
If needle <> "" Then
|
||||
found = 0
|
||||
For i = 0 To UBound(haystack)
|
||||
If UCase(haystack(i)) = UCase(needle) Then
|
||||
found = 1
|
||||
WScript.StdOut.Write "Found " & Chr(34) & needle & Chr(34) & " at index " & i
|
||||
WScript.StdOut.WriteLine
|
||||
End If
|
||||
Next
|
||||
If found < 1 Then
|
||||
WScript.StdOut.Write Chr(34) & needle & Chr(34) & " not found."
|
||||
WScript.StdOut.WriteLine
|
||||
End If
|
||||
Else
|
||||
Exit do
|
||||
End If
|
||||
Loop
|
||||
Loading…
Add table
Add a link
Reference in a new issue