Jump to content

Words from neighbour ones: Difference between revisions

m
syntax highlighting fixup automation
m (syntax highlighting fixup automation)
Line 29:
{{trans|Python}}
 
<langsyntaxhighlight lang="11l">V wordList = File(‘unixdict.txt’).read().split("\n")
 
V filteredWords = wordList.filter(chosenWord -> chosenWord.len >= 9)
Line 37:
V newWord = (0..8).map(i -> :filteredWords[@position + i][i]).join(‘’)
I newWord C filteredWords
print(newWord)</langsyntaxhighlight>
 
{{out}}
Line 76:
=={{header|AppleScript}}==
===Core language===
<langsyntaxhighlight lang="applescript">on task()
-- Since the task specifically involves unixdict.txt, this code's written in
-- the knowlege that the words are on individual lines and in dictionary order.
Line 116:
end task
 
task()</langsyntaxhighlight>
 
{{output}}
<langsyntaxhighlight lang="applescript">{"applicate", "architect", "astronomy", "christine", "christoph", "committee", "composite", "constrict", "construct", "different", "extensive", "greenwood", "implement", "improvise", "intercept", "interpret", "interrupt", "philosoph", "prescript", "receptive", "telephone", "transcend", "transport", "transpose"}</langsyntaxhighlight>
 
===AppleScriptObjC===
Same output as above.
<langsyntaxhighlight lang="applescript">use AppleScript version "2.4" -- OS X 10.10 (Yosemite) or later
use framework "Foundation"
use scripting additions
Line 159:
end task
 
task()</langsyntaxhighlight>
 
=={{header|Arturo}}==
{{trans|Nim}}
<langsyntaxhighlight lang="rebol">wordset: map read.lines relative "unixdict.txt" => strip
wordset: select wordset 'word -> 9 =< size word
 
Line 177:
lastWord: new newWord
]
]</langsyntaxhighlight>
 
{{out}}
Line 207:
 
=={{header|AutoHotkey}}==
<langsyntaxhighlight AutoHotkeylang="autohotkey">FileRead, wList, % A_Desktop "\unixdict.txt"
for word in neighbour(wList)
result .= word (Mod(A_Index, 6) ? "`t" : "`n")
Line 232:
}
return oRes
}</langsyntaxhighlight>
{{out}}
<pre>applicate architect astronomy christine christoph committee
Line 240:
 
=={{header|AWK}}==
<syntaxhighlight lang="awk">
<lang AWK>
# syntax: GAWK -f WORDS_FROM_NEIGHBOUR_ONES.AWK unixdict.txt
{ if (length($0) < 9) { next }
Line 260:
exit(0)
}
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 289:
</pre>
=={{header|C}}==
<langsyntaxhighlight lang="c">#include <stdio.h>
#include <stdlib.h>
#include <string.h>
Line 358:
free(words);
return EXIT_SUCCESS;
}</langsyntaxhighlight>
 
{{out}}
Line 389:
 
=={{header|C++}}==
<langsyntaxhighlight lang="cpp">#include <algorithm>
#include <cstdlib>
#include <fstream>
Line 427:
}
return EXIT_SUCCESS;
}</langsyntaxhighlight>
 
{{out}}
Line 460:
{{libheader| System.Classes}}
{{Trans|Java}}
<syntaxhighlight lang="delphi">
<lang Delphi>
program Words_from_neighbour_ones;
 
Line 512:
Words.Free;
readln;
end.</langsyntaxhighlight>
{{out}}
<pre> 1. applicate
Line 540:
 
=={{header|F_Sharp|F#}}==
<langsyntaxhighlight lang="fsharp">
// Words from neighbour ones. Nigel Galloway: February 11th., 2021.
let g=[|use n=System.IO.File.OpenText("unixdict.txt") in while not n.EndOfStream do yield n.ReadLine()|]|>Array.filter(fun n->n.Length>8)
g|>Array.windowed 9|>Array.map(fun n->n|>Array.mapi(fun n g->g.[n])|>System.String)|>Array.filter(fun n-> Array.contains n g)|>Array.distinct|>Array.iter(printfn "%s")
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 578:
<code><clumps></code> is the same idea except it doesn't actually store all that redundant information in memory; it's a generator that generates clumps on demand. Notice that clumps are matrices, so we can take their diagonal with <code>main-diagonal</code>.
{{works with|Factor|0.99 2020-08-14}}
<langsyntaxhighlight lang="factor">USING: formatting grouping hash-sets io.encodings.ascii io.files
kernel literals math math.matrices sequences sequences.extras
sets strings ;
Line 592:
[ wordset in? ] map-filter ! filter diagonals that are words
members ! remove duplicates
[ 1 + swap "%2d. %s\n" printf ] each-index ! print words formatted nicely</langsyntaxhighlight>
{{out}}
<pre style="height:17em">
Line 624:
=={{header|FreeBASIC}}==
{{trans|Ring}}
<langsyntaxhighlight lang="freebasic">
Open "unixdict.txt" For Input As #1
Dim As String cStr, wordList()
Line 674:
Print !"\nterminado..."
Sleep
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 714:
 
=={{header|Go}}==
<langsyntaxhighlight lang="go">package main
 
import (
Line 760:
}
}
}</langsyntaxhighlight>
 
{{out}}
Line 791:
 
=={{header|J}}==
<langsyntaxhighlight Jlang="j"> >(([-.-.)9 <@((=i.9)#&,])\ 9{.&>(#~ 8<#@>)) cutLF fread 'unixdict.txt'
applicate
architect
Line 815:
transcend
transport
transpose</langsyntaxhighlight>
 
In other words: find the set intersection (<code>([-.-.)</code>) between words and the sequences of 9 ascending position characters (<code>9 <@((=i.9)#&,])\</code> ...) from extracting the first 9 characters (<code>9{.&></code> ...) of words with more than 8 characters (<code>(#~ 8<#@>)</code>) for words from unixdict.txt (( ... )<code>cutLF fread 'unixdict.txt'</code>)
 
=={{header|Java}}==
<langsyntaxhighlight lang="java">import java.io.*;
import java.util.*;
 
Line 853:
}
}
}</langsyntaxhighlight>
 
{{out}}
Line 884:
 
=={{header|JavaScript}}==
<langsyntaxhighlight lang="javascript">
document.write(`
<p>Choose dictionary: <input id="dict" type="file"></p>
Line 913:
fr.readAsText(f);
}
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 921:
=={{header|jq}}==
 
For speed, this solution constructs a JSON object as a dictionary ($hash):<langsyntaxhighlight lang="jq">
# input: the dictionary
# $n: starting point (starting at 0)
Line 931:
| . as $dict
| (reduce.[] as $x ({}; .[$x]=true)) as $hash
| range(0; length-9) as $i | form_word($i) | select($hash[.])</langsyntaxhighlight>
{{out}}
<pre>["applicate","architect","astronomy","christine","christoph","committee","committee","committee","committee","committee","composite","constrict","constrict","construct","different","extensive","greenwood","implement","improvise","intercept","interpret","interrupt","interrupt","philosoph","prescript","receptive","telephone","transcend","transcend","transport","transpose"]
Line 938:
====Removing duplicates efficiently====
Using `def form_word`, we have only to modify the last line above:
<langsyntaxhighlight lang="jq">[inputs | select(length >= 9)]
| . as $dict
| (reduce.[] as $x ({}; .[$x]=true)) as $hash
Line 944:
($dict | form_word($i)) as $w
| if .hash[$w] then .hash[$w] = null | .words += [$w] else . end)
| .words</langsyntaxhighlight>
 
=={{header|Julia}}==
<langsyntaxhighlight lang="julia">function wordsfromneighbourones(wordfile::String, len = 9, colwidth = 11, numcols = 8)
println("Word source: $wordfile\n")
words = filter(w -> length(w) >= len, split(read(wordfile, String), r"\s+"))
Line 961:
 
wordsfromneighbourones("unixdict.txt")
</langsyntaxhighlight>{{out}}
<pre>
Word source: unixdict.txt
Line 971:
 
=={{header|Ksh}}==
<langsyntaxhighlight lang="ksh">
#!/bin/ksh
 
Line 1,027:
fi
fi
done</langsyntaxhighlight>
{{out}}<pre>
1 applicate
Line 1,055:
 
=={{header|Lua}}==
<langsyntaxhighlight lang="lua">wordlist, wordhash = {}, {}
for word in io.open("unixdict.txt", "r"):lines() do
if #word >= 9 then
Line 1,073:
print(word)
end
end</langsyntaxhighlight>
{{out}}
<pre>applicate
Line 1,108:
 
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<langsyntaxhighlight Mathematicalang="mathematica">dict = Once[Import["https://web.archive.org/web/20180611003215/http://www.puzzlers.org/pub/wordlists/unixdict.txt"]];
dict //= StringSplit[#,"\n"]&;
dict //= Select[StringLength/*GreaterEqualThan[9]];
firsts9 = Characters[dict][[All,;;9]];
words = StringJoin[Diagonal[firsts9,-#]]&/@Range[0,Length[firsts9]-9];
Intersection[words,dict]</langsyntaxhighlight>
{{out}}
<pre>{"applicate", "architect", "astronomy", "christine", "christoph", "committee", "composite", "constrict", "construct", "different", "extensive", "greenwood", "implement", "improvise", "intercept", "interpret", "interrupt", "philosoph", "prescript", "receptive", "telephone", "transcend", "transport", "transpose"}</pre>
 
=={{header|Nim}}==
<langsyntaxhighlight Nimlang="nim">import sets, strutils, sugar
 
# Build list and set of words with length >= 9.
Line 1,135:
inc count
echo ($count).align(2), ' ', newWord
lastWord = newWord</langsyntaxhighlight>
 
{{out}}
Line 1,164:
 
=={{header|Perl}}==
<langsyntaxhighlight lang="perl">#!/usr/bin/perl
 
use strict; # https://rosettacode.org/wiki/Words_from_neighbour_ones
Line 1,178:
my $new = join '', @{^CAPTURE}, "\n";
$dict{$new} and !$seen{$new}++ and print $new;
}</langsyntaxhighlight>
{{out}}
applicate
Line 1,206:
 
=={{header|Phix}}==
<!--<langsyntaxhighlight Phixlang="phix">(phixonline)-->
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<span style="color: #008080;">function</span> <span style="color: #000000;">over9</span><span style="color: #0000FF;">(</span><span style="color: #004080;">string</span> <span style="color: #000000;">word</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">return</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">word</span><span style="color: #0000FF;">)>=</span><span style="color: #000000;">9</span> <span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
Line 1,213:
<span style="color: #004080;">sequence</span> <span style="color: #000000;">neighwords</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">unique</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">filter</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">columnize</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">apply</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">tagset</span><span style="color: #0000FF;">(</span><span style="color: #000000;">9</span><span style="color: #0000FF;">),</span><span style="color: #000000;">slicen</span><span style="color: #0000FF;">)),</span><span style="color: #008000;">"in"</span><span style="color: #0000FF;">,</span><span style="color: #000000;">words</span><span style="color: #0000FF;">))</span>
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"%d words: %s\n"</span><span style="color: #0000FF;">,{</span><span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">neighwords</span><span style="color: #0000FF;">),</span><span style="color: #7060A8;">join</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">shorten</span><span style="color: #0000FF;">(</span><span style="color: #000000;">neighwords</span><span style="color: #0000FF;">,</span><span style="color: #008000;">""</span><span style="color: #0000FF;">,</span><span style="color: #000000;">3</span><span style="color: #0000FF;">))})</span>
<!--</langsyntaxhighlight>-->
{{out}}
<pre>
Line 1,220:
 
=={{header|Processing}}==
<langsyntaxhighlight Processinglang="processing">StringList words = new StringList(), found = new StringList();
for (String str : loadStrings("unixdict.txt")) {
if (str.length() >= 9) {
Line 1,237:
for (String word : found) {
println(word);
}</langsyntaxhighlight>
{{out}}
<pre style="height: 18em;">applicate
Line 1,267:
=={{header|Python}}==
Tested on Python 3+, the file download will work only if the link is still active. It is possible that you may be able to fetch the file in your browser but download via code may still fail. Check whether you are connected to a VPN, it works on open networks.
<syntaxhighlight lang="python">
<lang Python>
#Aamrun, 5th November 2021
 
Line 1,288:
if newWord in filteredWords:
print(newWord)
</syntaxhighlight>
</lang>
{{Output}}
Yes, there are duplicates, the task doesn't say that only unique elements should be present, hence the complete raw list will appear as below :
Line 1,326:
 
=={{header|Raku}}==
<syntaxhighlight lang="raku" perl6line>my @words_ge_9 = 'unixdict.txt'.IO.lines.grep( *.chars >= 9 );
my %words_eq_9 = @words_ge_9 .grep( *.chars == 9 ).Set;
 
Line 1,335:
}
 
.say for unique @new_words;</langsyntaxhighlight>
{{out}}
<pre>
Line 1,369:
 
It also allows the minimum length to be specified on the command line (CL) as well as the dictionary file identifier.
<langsyntaxhighlight lang="rexx">/*REXX pgm finds words that're composed from neighbor words (within an identified dict).*/
parse arg minL iFID . /*obtain optional arguments from the CL*/
if minL=='' | minL=="," then minL= 9 /*Not specified? Then use the default.*/
Line 1,396:
end /*j*/
/*stick a fork in it, we're all done. */
say copies('─', 30) finds ' neighbor words found with a minimum length of ' minL</langsyntaxhighlight>
{{out|output|text=&nbsp; when using the default inputs:}}
<pre>
Line 1,430:
 
=={{header|Ring}}==
<langsyntaxhighlight lang="ring">
cStr = read("unixdict.txt")
wordList = str2list(cStr)
Line 1,474:
 
see "done..." + nl
</syntaxhighlight>
</lang>
Output:
<pre>
Line 1,508:
=={{header|VBScript}}==
Run it in CScript.
<syntaxhighlight lang="vb">
<lang vb>
with createobject("ADODB.Stream")
.charset ="UTF-8"
Line 1,541:
next
 
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 1,572:
=={{header|Vlang}}==
{{trans|AutoHotkey}}
<langsyntaxhighlight lang="vlang">import os
 
fn main() {
Line 1,601:
}
return res_arr
}</langsyntaxhighlight>
 
{{out}}
Line 1,614:
{{libheader|Wren-sort}}
{{libheader|Wren-fmt}}
<langsyntaxhighlight lang="ecmascript">import "io" for File
import "/sort" for Find
import "/fmt" for Fmt
Line 1,630:
alreadyFound.add(word)
}
}</langsyntaxhighlight>
 
{{out}}
Line 1,661:
 
=={{header|XPL0}}==
<langsyntaxhighlight XPL0lang="xpl0">string 0; \use zero-terminated strings
int Dict(26000); \pointers to words (enough for unixdict.txt)
int DictSize; \actual number of pointers in Dict
Line 1,725:
until DI >= DictSize-9;
CrLf(0);
]</langsyntaxhighlight>
 
{{out}}
Line 1,736:
=={{header|Yabasic}}==
{{trans|FreeBASIC}}
<langsyntaxhighlight lang="yabasic">
open "unixdict.txt" for reading as #1
p = 0
Line 1,785:
next n
end
</syntaxhighlight>
</lang>
{{out}}
<pre>
10,333

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.