Data update
This commit is contained in:
parent
4bb20c9b71
commit
cbaf4c4b64
12390 changed files with 318560 additions and 27248 deletions
42
Task/Search-a-list/Ada/search-a-list.adb
Normal file
42
Task/Search-a-list/Ada/search-a-list.adb
Normal file
|
|
@ -0,0 +1,42 @@
|
|||
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;
|
||||
63
Task/Search-a-list/COBOL/search-a-list.cob
Normal file
63
Task/Search-a-list/COBOL/search-a-list.cob
Normal file
|
|
@ -0,0 +1,63 @@
|
|||
*> 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
|
||||
.
|
||||
15
Task/Search-a-list/Crystal/search-a-list.cr
Normal file
15
Task/Search-a-list/Crystal/search-a-list.cr
Normal file
|
|
@ -0,0 +1,15 @@
|
|||
haystack = %w(Zig Zag Wally Ronald Bush Krusty Charlie Bush Bozo)
|
||||
|
||||
# First occurrence
|
||||
haystack.index("Bush") # => 4
|
||||
|
||||
# Indexable#index returns nil if the needle is not found
|
||||
haystack.index("Washington") # => nil
|
||||
|
||||
# Indexable#index! throws an exception if the needle is not found
|
||||
haystack.index!("Washington") # => Unhandled exception: Element not found (Enumerable::NotFoundError)
|
||||
|
||||
# Indexable#rindex and Indexable#rindex! work the same way, only they search right to left
|
||||
haystack.rindex("Bush") # => 7
|
||||
haystack.rindex("Washington") # => nil
|
||||
haystack.rindex!("Washington") # => Unhandled exception: Element not found (Enumerable::NotFoundError)
|
||||
26
Task/Search-a-list/Emacs-Lisp/search-a-list.el
Normal file
26
Task/Search-a-list/Emacs-Lisp/search-a-list.el
Normal file
|
|
@ -0,0 +1,26 @@
|
|||
(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)))
|
||||
46
Task/Search-a-list/Euphoria/search-a-list.eu
Normal file
46
Task/Search-a-list/Euphoria/search-a-list.eu
Normal file
|
|
@ -0,0 +1,46 @@
|
|||
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()
|
||||
20
Task/Search-a-list/Frink/search-a-list.frink
Normal file
20
Task/Search-a-list/Frink/search-a-list.frink
Normal file
|
|
@ -0,0 +1,20 @@
|
|||
a = ["Zig", "Zag", "Wally", "Ronald", "Bush", "Krusty", "Charlie", "Bush", "Boz", "Zag"]
|
||||
|
||||
test[a, "Washington"]
|
||||
test[a, "Ronald"]
|
||||
|
||||
println[]
|
||||
println["Extra credit:"]
|
||||
[mosts, count] = mostCommon[a]
|
||||
for m = mosts
|
||||
println["$m\toccurs $count times, last at index " + a.lastIndexOf[m]]
|
||||
|
||||
test[a, str] :=
|
||||
{
|
||||
print["$str:\t"]
|
||||
i = a.indexOf[str]
|
||||
if i == -1
|
||||
println["does not exist in list"]
|
||||
else
|
||||
println[i]
|
||||
}
|
||||
25
Task/Search-a-list/Pluto/search-a-list.pluto
Normal file
25
Task/Search-a-list/Pluto/search-a-list.pluto
Normal file
|
|
@ -0,0 +1,25 @@
|
|||
local function search(haystack, needle)
|
||||
local indices = {}
|
||||
for i = 1, #haystack do
|
||||
if haystack[i] == needle then indices:insert(i) end
|
||||
end
|
||||
local c = #indices
|
||||
if c == 0 then error("Needle not found in haystack.") end
|
||||
local t = (c == 1) ? "time" : "times"
|
||||
print($"The needle occurs {c} {t} in the haystack.")
|
||||
if #indices == 1 then
|
||||
print($"It occurs at index {indices[1]}")
|
||||
else
|
||||
print($"It first occurs at index {indices[1]}")
|
||||
print($"It last occurs at index {indices:back()}")
|
||||
end
|
||||
print()
|
||||
end
|
||||
|
||||
local haystack = {"Zig", "Zag", "Wally", "Ronald", "Bush", "Krusty", "Charlie", "Bush", "Boz", "Zag"}
|
||||
print($"The haystack is: \n{haystack:concat(", ")}\n")
|
||||
local needles = {"Wally", "Bush", "Zag", "George"}
|
||||
for needles as needle do
|
||||
print($"The needle is {needle}.")
|
||||
search(haystack, needle)
|
||||
end
|
||||
12
Task/Search-a-list/PowerShell/search-a-list-1.ps1
Normal file
12
Task/Search-a-list/PowerShell/search-a-list-1.ps1
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
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"
|
||||
57
Task/Search-a-list/PowerShell/search-a-list-2.ps1
Normal file
57
Task/Search-a-list/PowerShell/search-a-list-2.ps1
Normal file
|
|
@ -0,0 +1,57 @@
|
|||
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
Task/Search-a-list/PowerShell/search-a-list-3.ps1
Normal file
1
Task/Search-a-list/PowerShell/search-a-list-3.ps1
Normal file
|
|
@ -0,0 +1 @@
|
|||
Find-Needle "house" $haystack
|
||||
1
Task/Search-a-list/PowerShell/search-a-list-4.ps1
Normal file
1
Task/Search-a-list/PowerShell/search-a-list-4.ps1
Normal file
|
|
@ -0,0 +1 @@
|
|||
Find-Needle "house" $haystack -Verbose
|
||||
1
Task/Search-a-list/PowerShell/search-a-list-5.ps1
Normal file
1
Task/Search-a-list/PowerShell/search-a-list-5.ps1
Normal file
|
|
@ -0,0 +1 @@
|
|||
Find-Needle "house" $haystack -LastIndex -Verbose
|
||||
1
Task/Search-a-list/PowerShell/search-a-list-6.ps1
Normal file
1
Task/Search-a-list/PowerShell/search-a-list-6.ps1
Normal file
|
|
@ -0,0 +1 @@
|
|||
Find-Needle "title" $haystack -LastIndex -Verbose
|
||||
1
Task/Search-a-list/PowerShell/search-a-list-7.ps1
Normal file
1
Task/Search-a-list/PowerShell/search-a-list-7.ps1
Normal file
|
|
@ -0,0 +1 @@
|
|||
Find-Needle "something" $haystack -Verbose
|
||||
38
Task/Search-a-list/Rebol/search-a-list.rebol
Normal file
38
Task/Search-a-list/Rebol/search-a-list.rebol
Normal file
|
|
@ -0,0 +1,38 @@
|
|||
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]
|
||||
]
|
||||
]
|
||||
28
Task/Search-a-list/VBScript/search-a-list.vbs
Normal file
28
Task/Search-a-list/VBScript/search-a-list.vbs
Normal file
|
|
@ -0,0 +1,28 @@
|
|||
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