Another update from ingydotnet^djgoku

This commit is contained in:
Ingy döt Net 2015-11-18 06:14:39 +00:00
parent 91df62d461
commit 948b86eafa
7604 changed files with 108452 additions and 22726 deletions

View file

@ -0,0 +1,40 @@
@echo off
setlocal enabledelayedexpansion
%==Sample list==%
set "data=foo, bar, baz, quux, quuux, quuuux, bazola, ztesch, foo, bar, thud, grunt"
set "data=%data% foo, bar, bletch, foo, bar, fum, fred, jim, sheila, barney, flarp, zxc"
set "data=%data% spqr, wombat, shme, foo, bar, baz, bongo, spam, eggs, snork, foo, bar"
set "data=%data% zot, blarg, wibble, toto, titi, tata, tutu, pippo, pluto, paperino, aap"
set "data=%data% noot, mies, oogle, foogle, boogle, zork, gork, bork"
%==Sample "needles" [whitespace is the delimiter]==%
set "needles=foo bar baz jim bong"
%==Counting and Seperating each Data==%
set datalen=0
for %%. in (!data!) do (
set /a datalen+=1
set data!datalen!=%%.
)
%==Do the search==%
for %%A in (!needles!) do (
set "first="
set "last="
set "found=0"
for /l %%B in (1,1,%datalen%) do (
if "!data%%B!" == "%%A" (
set /a found+=1
if !found! equ 1 set first=%%B
set last=%%B
)
)
if !found! equ 0 echo."%%A": Not found.
if !found! equ 1 echo."%%A": Found once in index [!first!].
if !found! gtr 1 echo."%%A": Found !found! times. First instance:[!first!] Last instance:[!last!].
)
%==We are done==%
echo.
pause

View file

@ -0,0 +1,7 @@
haystack = ~w(Zig Zag Wally Ronald Bush Krusty Charlie Bush Bozo)
Enum.each(~w(Bush Washington), fn needle ->
index = Enum.find_index(haystack, fn x -> x==needle end)
if index, do: (IO.puts "#{index} #{needle}"),
else: raise "#{needle} is not in haystack\n"
end)

View file

@ -0,0 +1,29 @@
include FMS-SI.f
include FMS-SILib.f
${ Dishonest Fake Left Karl Hillary Monica Bubba Hillary Multi-Millionaire } constant haystack
: needleIndex { addr len $list | cnt -- idx }
0 to cnt $list uneach:
begin
$list each:
while
@: addr len compare 0= if cnt exit then
cnt 1+ to cnt
repeat true abort" Not found" ;
: LastIndexOf { addr len $list | cnt last-found -- idx }
0 to cnt 0 to last-found $list uneach:
begin
$list each:
while
@: addr len compare 0= if cnt to last-found then
cnt 1+ to cnt
repeat
last-found if last-found
else true abort" Not found"
then ;
s" Hillary" haystack needleIndex . \ => 4
s" Hillary" haystack LastIndexOf . \ => 7
s" Washington" haystack needleIndex . \ => aborted: Not found

View file

@ -0,0 +1,14 @@
package main
var haystack = []string{"Zig", "Zag", "Wally", "Ronald", "Bush", "Krusty",
"Charlie", "Bush", "Bozo", "Zag", "mouse", "hat", "cup", "deodorant",
"television", "soap", "methamphetamine", "severed cat heads", "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", "sodium", "phosphorous", "californium",
"copernicium", "gold", "thallium", "carbon", "silver", "gold", "copper",
"helium", "sulfur"}

View file

@ -2,19 +2,6 @@ package main
import "fmt"
var haystack = []string{"Zig", "Zag", "Wally", "Ronald", "Bush", "Krusty",
"Charlie", "Bush", "Bozo", "Zag", "mouse", "hat", "cup", "deodorant",
"television", "soap", "methamphetamine", "severed cat heads", "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", "sodium", "phosphorous", "californium",
"copernicium", "gold", "thallium", "carbon", "silver", "gold", "copper",
"helium", "sulfur"}
func main() {
// first task
printSearchForward("soap")

View file

@ -0,0 +1,13 @@
package main
import "fmt"
func main() {
m := map[string][]int{}
for i, needle := range haystack {
m[needle] = append(m[needle], i)
}
for _, n := range []string{"soap", "gold", "fire"} {
fmt.Println(n, m[n])
}
}

View file

@ -1,11 +1,11 @@
String[] haystack = {"Zig","Zag","Wally","Ronald","Bush","Krusty","Charlie","Bush","Bozo"};
import java.util.Arrays;
String[] haystack = { "Zig","Zag","Wally","Ronald","Bush","Krusty","Charlie","Bush","Bozo"};
OUTERLOOP:
for (String needle : new String[]{"Washington","Bush"}) {
for (int i = 0; i < haystack.length; i++)
if (needle.equals(haystack[i])) {
System.out.println(i + " " + needle);
continue OUTERLOOP;
}
System.out.println(needle + " is not in haystack");
int index = Arrays.binarySearch(haystack, needle);
if (index < 0)
System.out.println(needle + " is not in haystack");
else
System.out.println(index + " " + needle);
}

View file

@ -0,0 +1,47 @@
(function () {
function findIndex(fnPredicate, list) {
for (var i = 0, lng = list.length; i < lng; i++) {
if (fnPredicate(list[i])) {
return i;
}
}
return Error("not found");
};
// DEFINING A PARTICULAR TYPE OF SEARCH MATCH
function matchCaseInsensitive(s, t) {
return s.toLowerCase() === t.toLowerCase();
}
var lstHaystack = [
'Zig', 'Zag', 'Wally', 'Ronald', 'Bush',
'Krusty', 'Charlie', 'Bush', 'Bozo'
],
lstReversed = lstHaystack.slice(0).reverse(),
iLast = lstHaystack.length - 1,
lstNeedles = ['bush', 'washington'];
return {
'first': lstNeedles.map(function (s) {
return [s, findIndex(function (t) {
return matchCaseInsensitive(s, t);
},
lstHaystack)];
}),
'last': lstNeedles.map(function (s) {
var varIndex = findIndex(function (t) {
return matchCaseInsensitive(s, t);
},
lstReversed);
return [
s,
typeof varIndex === 'number' ?
iLast - varIndex : varIndex
];
})
}
})();

View file

@ -0,0 +1,22 @@
{
"first": [
[
"bush",
4
],
[
"washington",
"Error: not found"
]
],
"last": [
[
"bush",
7
],
[
"washington",
"Error: not found"
]
]
}

View 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"

View file

@ -2,7 +2,7 @@ haystack = %w(Zig Zag Wally Ronald Bush Krusty Charlie Bush Bozo)
%w(Bush Washington).each do |needle|
if (i = haystack.index(needle))
print i, " ", needle, "\n"
puts "#{i} #{needle}"
else
raise "#{needle} is not in haystack\n"
end

View file

@ -5,3 +5,4 @@ haystack.each do |item|
break
end
end
#=> Bush last appears at index 7

View file

@ -1,3 +1,6 @@
multi_item = haystack .each_with_index .group_by {|elem, idx| elem} .find {|key, val| val.length > 1}
# multi_item is => ["Bush", [["Bush", 4], ["Bush", 7]]]
puts "#{multi_item[0]} last appears at index #{multi_item[1][-1][1]}" unless multi_item.nil?
multi_item = haystack.each_index.group_by{|idx| haystack[idx]}.select{|key, val| val.length > 1}
# multi_item is => {"Bush"=>[4, 7]}
multi_item.each do |key, val|
puts "#{key} appears at index #{val}"
end
#=> Bush appears at index [4, 7]

View file

@ -0,0 +1,8 @@
fn main() {
let haystack=vec!["Zig", "Zag", "Wally", "Ronald", "Bush", "Krusty", "Charlie",
"Bush", "Boz", "Zag"];
println!("First occurence of 'Bush' at {:?}",haystack.iter().position(|s| *s=="Bush"));
println!("Last occurence of 'Bush' at {:?}",haystack.iter().rposition(|s| *s=="Bush"));
println!("First occurence of 'Rob' at {:?}",haystack.iter().position(|s| *s=="Rob"));
}

View file

@ -0,0 +1,8 @@
fn main() {
let haystack=vec!["Zig", "Zag", "Wally", "Ronald", "Bush", "Krusty", "Charlie",
"Bush", "Boz", "Zag"];
println!("First occurence of 'Bush' at {:?}",haystack.iter().position(|s| *s=="Bush").unwrap());
println!("Last occurence of 'Bush' at {:?}",haystack.iter().rposition(|s| *s=="Bush").unwrap());
println!("First occurence of 'Rob' at {:?}",haystack.iter().position(|s| *s=="Rob").unwrap());
}

View 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