Word search: Difference between revisions

Content added Content deleted
m (→‎{{header|Phix}}: use unix_dict())
m (syntax highlighting fixup automation)
Line 54: Line 54:
{{trans|Python}}
{{trans|Python}}


<lang 11l>-V
<syntaxhighlight lang="11l">-V
dirs = [[1, 0], [ 0, 1], [ 1, 1],
dirs = [[1, 0], [ 0, 1], [ 1, 1],
[1, -1], [-1, 0],
[1, -1], [-1, 0],
Line 192: Line 192:
print(grid.solutions[size - 1])
print(grid.solutions[size - 1])


print_result(create_word_search(&read_words(‘unixdict.txt’)))</lang>
print_result(create_word_search(&read_words(‘unixdict.txt’)))</syntaxhighlight>


{{out}}
{{out}}
Line 229: Line 229:
=={{header|C sharp}}==
=={{header|C sharp}}==
{{trans|Java}}
{{trans|Java}}
<lang csharp>using System;
<syntaxhighlight lang="csharp">using System;
using System.Collections.Generic;
using System.Collections.Generic;
using System.Linq;
using System.Linq;
Line 437: Line 437:
}
}
}
}
}</lang>
}</syntaxhighlight>


<pre>
<pre>
Line 473: Line 473:


=={{header|C++}}==
=={{header|C++}}==
<lang cpp>
<syntaxhighlight lang="cpp">
#include <iomanip>
#include <iomanip>
#include <ctime>
#include <ctime>
Line 643: Line 643:
return 0;
return 0;
}
}
</syntaxhighlight>
</lang>
{{out}}
{{out}}
<pre>
<pre>
Line 684: Line 684:
=={{header|D}}==
=={{header|D}}==
{{trans|Java}}
{{trans|Java}}
<lang D>import std.random : Random, uniform, randomShuffle;
<syntaxhighlight lang="d">import std.random : Random, uniform, randomShuffle;
import std.stdio;
import std.stdio;


Line 877: Line 877:
writeln(grid.solutions[size - 1]);
writeln(grid.solutions[size - 1]);
}
}
}</lang>
}</syntaxhighlight>


{{out}}
{{out}}
Line 922: Line 922:
FILLED was not set to FALSE every time Initialize was called.
FILLED was not set to FALSE every time Initialize was called.
Set all integer to (U)long.
Set all integer to (U)long.
<lang freebasic>Randomize Timer ' OK getting a good puzzle every time
<syntaxhighlight lang="freebasic">Randomize Timer ' OK getting a good puzzle every time


#Macro TrmSS (n)
#Macro TrmSS (n)
Line 1,280: Line 1,280:


Sleep
Sleep
End</lang>
End</syntaxhighlight>
{{out}}
{{out}}
<pre style="height:52ex;overflow:scroll"> 0 1 2 3 4 5 6 7 8 9
<pre style="height:52ex;overflow:scroll"> 0 1 2 3 4 5 6 7 8 9
Line 1,370: Line 1,370:
<br>
<br>
The link to "unixdict" appears to be broken so I've used instead the dictionary at "/usr/share/dict/words" which came pre-installed with my Ubuntu 16.04 distribution. I've no idea whether these dictionaries are the same or not.
The link to "unixdict" appears to be broken so I've used instead the dictionary at "/usr/share/dict/words" which came pre-installed with my Ubuntu 16.04 distribution. I've no idea whether these dictionaries are the same or not.
<lang go>package main
<syntaxhighlight lang="go">package main


import (
import (
Line 1,558: Line 1,558:
unixDictPath := "/usr/share/dict/words"
unixDictPath := "/usr/share/dict/words"
printResult(createWordSearch(readWords(unixDictPath)))
printResult(createWordSearch(readWords(unixDictPath)))
}</lang>
}</syntaxhighlight>


{{out}}
{{out}}
Line 1,599: Line 1,599:
Implementation:
Implementation:


<lang J>require'web/gethttp'
<syntaxhighlight lang="j">require'web/gethttp'


unixdict=:verb define
unixdict=:verb define
Line 1,651: Line 1,651:
puz=. (_23{.":i.10),' ',1j1#"1(":i.10 1),.' ',.10 10$fill (I.grid=' ')} grid
puz=. (_23{.":i.10),' ',1j1#"1(":i.10 1),.' ',.10 10$fill (I.grid=' ')} grid
puz,' ',1 1}._1 _1}.":((</.~ <.) i.@# * 3%#)key
puz,' ',1 1}._1 _1}.":((</.~ <.) i.@# * 3%#)key
)</lang>
)</syntaxhighlight>


Notes:
Notes:
Line 1,695: Line 1,695:
Example run:
Example run:


<lang J> genpuz''
<syntaxhighlight lang="j"> genpuz''
0 1 2 3 4 5 6 7 8 9
0 1 2 3 4 5 6 7 8 9
Line 1,718: Line 1,718:
cop 10 8 nw │ paz 8 10 west │ wan 9 6 west
cop 10 8 nw │ paz 8 10 west │ wan 9 6 west
fizzle 1 6 sw │ penna 1 9 south│
fizzle 1 6 sw │ penna 1 9 south│
</syntaxhighlight>
</lang>


=={{header|Java}}==
=={{header|Java}}==
{{works with|Java|7}}
{{works with|Java|7}}
<lang java>import java.io.*;
<syntaxhighlight lang="java">import java.io.*;
import static java.lang.String.format;
import static java.lang.String.format;
import java.util.*;
import java.util.*;
Line 1,894: Line 1,894:
System.out.println(grid.solutions.get(size - 1));
System.out.println(grid.solutions.get(size - 1));
}
}
}</lang>
}</syntaxhighlight>


<pre>Attempts: 2
<pre>Attempts: 2
Line 1,929: Line 1,929:
=={{header|Julia}}==
=={{header|Julia}}==
Modified from the Go version. The task listed word list is offline, so the Debian distribution file "words.txt" was used instead.
Modified from the Go version. The task listed word list is offline, so the Debian distribution file "words.txt" was used instead.
<lang julia>using Random
<syntaxhighlight lang="julia">using Random


const stepdirections = [[1, 0], [0, 1], [1, 1], [1, -1], [-1, 0], [0, -1], [-1, -1], [-1, 1]]
const stepdirections = [[1, 0], [0, 1], [1, 1], [1, -1], [-1, 0], [0, -1], [-1, -1], [-1, 1]]
Line 2,045: Line 2,045:


printresult(wordmatrix("words.txt", false))
printresult(wordmatrix("words.txt", false))
</lang>{{output}}<pre>
</syntaxhighlight>{{output}}<pre>
Attempts: 1
Attempts: 1
Number of words: 25
Number of words: 25
Line 2,078: Line 2,078:
=={{header|Kotlin}}==
=={{header|Kotlin}}==
{{trans|Java}}
{{trans|Java}}
<lang scala>// version 1.2.0
<syntaxhighlight lang="scala">// version 1.2.0


import java.util.Random
import java.util.Random
Line 2,228: Line 2,228:
fun main(args: Array<String>) {
fun main(args: Array<String>) {
printResult(createWordSearch(readWords("unixdict.txt")))
printResult(createWordSearch(readWords("unixdict.txt")))
}</lang>
}</syntaxhighlight>


Sample output:
Sample output:
Line 2,266: Line 2,266:
=={{header|Nim}}==
=={{header|Nim}}==
{{trans|D}}
{{trans|D}}
<lang Nim>import random, sequtils, strformat, strutils
<syntaxhighlight lang="nim">import random, sequtils, strformat, strutils


const
const
Line 2,395: Line 2,395:
randomize()
randomize()
let grid = initGrid("unixdict.txt".readWords())
let grid = initGrid("unixdict.txt".readWords())
grid.printResult()</lang>
grid.printResult()</syntaxhighlight>


{{out}}
{{out}}
Line 2,431: Line 2,431:


=={{header|Perl}}==
=={{header|Perl}}==
<lang perl>#!/usr/bin/perl
<syntaxhighlight lang="perl">#!/usr/bin/perl


use strict; # http://www.rosettacode.org/wiki/Word_search
use strict; # http://www.rosettacode.org/wiki/Word_search
Line 2,530: Line 2,530:


return undef;
return undef;
}</lang>
}</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 2,560: Line 2,560:


=={{header|Phix}}==
=={{header|Phix}}==
<!--<lang Phix>(phixonline)-->
<!--<syntaxhighlight lang="phix">(phixonline)-->
<span style="color: #000080;font-style:italic;">--
<span style="color: #000080;font-style:italic;">--
-- demo\rosetta\wordsearch.exw
-- demo\rosetta\wordsearch.exw
Line 2,670: Line 2,670:
<span style="color: #0000FF;">{}</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">wait_key</span><span style="color: #0000FF;">()</span>
<span style="color: #0000FF;">{}</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">wait_key</span><span style="color: #0000FF;">()</span>
<!--</lang>-->
<!--</syntaxhighlight>-->
{{out}}
{{out}}
<pre>
<pre>
Line 2,706: Line 2,706:
{{trans|Java}}
{{trans|Java}}
{{works with|Python|3.x}}
{{works with|Python|3.x}}
<lang python>
<syntaxhighlight lang="python">
import re
import re
from random import shuffle, randint
from random import shuffle, randint
Line 2,870: Line 2,870:
if __name__ == "__main__":
if __name__ == "__main__":
print_result(create_word_search(read_words("unixdict.txt")))
print_result(create_word_search(read_words("unixdict.txt")))
</syntaxhighlight>
</lang>


{{out}}
{{out}}
Line 2,910: Line 2,910:
The following zip file is needed for the Unix dictionary and a QB64 words mod for fun! ...and some samples.<br>
The following zip file is needed for the Unix dictionary and a QB64 words mod for fun! ...and some samples.<br>
[https://www.qb64.org/forum/index.php?action=dlattach;topic=2334.0;attach=5434 Rosetta Code Word Search Challenge.zip]
[https://www.qb64.org/forum/index.php?action=dlattach;topic=2334.0;attach=5434 Rosetta Code Word Search Challenge.zip]
<lang qbasic> OPTION _EXPLICIT
<syntaxhighlight lang="qbasic"> OPTION _EXPLICIT
_TITLE "Puzzle Builder for Rosetta" 'by B+ started 2018-10-31
_TITLE "Puzzle Builder for Rosetta" 'by B+ started 2018-10-31
' 2018-11-02 Now that puzzle is working with basic and plus starters remove them and make sure puzzle works as well.
' 2018-11-02 Now that puzzle is working with basic and plus starters remove them and make sure puzzle works as well.
Line 3,235: Line 3,235:
NEXT
NEXT
CLOSE #1
CLOSE #1
END SUB</lang>
END SUB</syntaxhighlight>


----
----
Line 3,319: Line 3,319:
(or at least it started out that way... so more "inspired by")
(or at least it started out that way... so more "inspired by")


<lang racket>#lang racket
<syntaxhighlight lang="racket">#lang racket
;; ---------------------------------------------------------------------------------------------------
;; ---------------------------------------------------------------------------------------------------
(module+ main
(module+ main
Line 3,431: Line 3,431:
[(hash-has-key? cells (cons r c)) (loop r (add1 c) cells msg)]
[(hash-has-key? cells (cons r c)) (loop r (add1 c) cells msg)]
[else (loop r (add1 c) (hash-set cells (cons r c) (car msg)) (cdr msg))])))))
[else (loop r (add1 c) (hash-set cells (cons r c) (car msg)) (cdr msg))])))))
</syntaxhighlight>
</lang>


{{out}}
{{out}}
Line 3,531: Line 3,531:
{{works with|Rakudo|2020.01}}
{{works with|Rakudo|2020.01}}


<lang perl6>my $rows = 10;
<syntaxhighlight lang="raku" line>my $rows = 10;
my $cols = 10;
my $cols = 10;


Line 3,659: Line 3,659:
}
}
True
True
}</lang>
}</syntaxhighlight>
{{out|Sample output}}
{{out|Sample output}}
<pre> A B C D E F G H I J
<pre> A B C D E F G H I J
Line 3,694: Line 3,694:
=={{header|Visual Basic .NET}}==
=={{header|Visual Basic .NET}}==
{{trans|C#}}
{{trans|C#}}
<lang vbnet>Module Module1
<syntaxhighlight lang="vbnet">Module Module1


ReadOnly Dirs As Integer(,) = {
ReadOnly Dirs As Integer(,) = {
Line 3,917: Line 3,917:
End Sub
End Sub


End Module</lang>
End Module</syntaxhighlight>
{{out}}
{{out}}
<pre>Attempts: 148
<pre>Attempts: 148
Line 3,955: Line 3,955:
{{libheader|Wren-str}}
{{libheader|Wren-str}}
{{libheader|Wren-fmt}}
{{libheader|Wren-fmt}}
<lang ecmascript>import "random" for Random
<syntaxhighlight lang="ecmascript">import "random" for Random
import "/ioutil" for FileUtil
import "/ioutil" for FileUtil
import "/pattern" for Pattern
import "/pattern" for Pattern
Line 4,112: Line 4,112:
}
}


printResult.call(createWordSearch.call(readWords.call("unixdict.txt")))</lang>
printResult.call(createWordSearch.call(readWords.call("unixdict.txt")))</syntaxhighlight>


{{out}}
{{out}}
Line 4,152: Line 4,152:
=={{header|zkl}}==
=={{header|zkl}}==
Repeat words allowed. Rather brute force as I didn't realize that the message has to fit exactly.
Repeat words allowed. Rather brute force as I didn't realize that the message has to fit exactly.
<lang zkl>fcn buildVectors(R,C){ //-->up to 8 vectors of wild card strings
<syntaxhighlight lang="zkl">fcn buildVectors(R,C){ //-->up to 8 vectors of wild card strings
var [const] dirs=T(T(1,0), T(0,1), T(1,1), T(1,-1), T(-1,0),T(0,-1), T(-1,-1), T(-1,1));
var [const] dirs=T(T(1,0), T(0,1), T(1,1), T(1,-1), T(-1,0),T(0,-1), T(-1,-1), T(-1,1));
vs,v:=List(),List();
vs,v:=List(),List();
Line 4,178: Line 4,178:
foreach r,c in (10,10){ if(grid[r][c]=="?") grid[r][c]=MSG.next() }
foreach r,c in (10,10){ if(grid[r][c]=="?") grid[r][c]=MSG.next() }
MSG._n==msg.len() // use all of, not more, not less, of msg?
MSG._n==msg.len() // use all of, not more, not less, of msg?
}</lang>
}</syntaxhighlight>
<lang zkl>msg:="RosettaCode";
<syntaxhighlight lang="zkl">msg:="RosettaCode";


validWord:=RegExp("[A-Za-z]+\n$").matches;
validWord:=RegExp("[A-Za-z]+\n$").matches;
Line 4,208: Line 4,208:
fcn([(r,c,w)]){ "%-19s".fmt("[%d,%d]: %s ".fmt(r,c,w)) }) }
fcn([(r,c,w)]){ "%-19s".fmt("[%d,%d]: %s ".fmt(r,c,w)) }) }
);
);
fitted.apply(fcn(w){ w[2].len() }).sum(0).println();</lang>
fitted.apply(fcn(w){ w[2].len() }).sum(0).println();</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>