Teacup rim text: Difference between revisions

m
m (added omits)
m (→‎{{header|Wren}}: Minor tidy)
 
(10 intermediate revisions by 4 users not shown)
Line 34:
 
=={{header|11l}}==
<langsyntaxhighlight lang="11l">F rotated(String s)
R s[1..]‘’s[0]
 
Line 59:
print()
 
L.break</langsyntaxhighlight>
 
{{out}}
Line 67:
ate -> tea -> eat
</pre>
 
=={{header|Arturo}}==
 
<syntaxhighlight lang="rebol">wordset: map read.lines relative "unixdict.txt" => strip
 
rotateable?: function [w][
loop 1..dec size w 'i [
rotated: rotate w i
if or? [rotated = w][not? contains? wordset rotated] ->
return false
]
return true
]
 
results: new []
loop select wordset 'word [3 =< size word] 'word [
if rotateable? word ->
'results ++ @[ sort map 1..size word 'i [ rotate word i ]]
]
 
loop sort unique results 'result [
root: first result
print join.with: " -> " map 1..size root 'i [ rotate.left root i]
]</syntaxhighlight>
 
{{out}}
 
<pre>tea -> eat -> ate
rca -> car -> arc
pta -> tap -> apt</pre>
 
=={{header|AutoHotkey}}==
<langsyntaxhighlight AutoHotkeylang="autohotkey">Teacup_rim_text(wList){
oWord := [], oRes := [], n := 0
for i, w in StrSplit(wList, "`n", "`r")
Line 97 ⟶ 127:
rotate(w){
return SubStr(w, 2) . SubStr(w, 1, 1)
}</langsyntaxhighlight>
Examples:<langsyntaxhighlight AutoHotkeylang="autohotkey">FileRead, wList, % A_Desktop "\unixdict.txt"
result := ""
for i, v in Teacup_rim_text(wList)
result .= v "`n"
MsgBox % result
return</langsyntaxhighlight>
{{out}}
<pre>apt,pta,tap
Line 110 ⟶ 140:
 
=={{header|AWK}}==
<syntaxhighlight lang="awk">
<lang AWK>
# syntax: GAWK -f TEACUP_RIM_TEXT.AWK UNIXDICT.TXT
#
Line 151 ⟶ 181:
exit(0)
}
</syntaxhighlight>
</lang>
{{out}}
<p>using UNIXDICT.TXT</p>
Line 171 ⟶ 201:
 
=={{header|BaCon}}==
<langsyntaxhighlight lang="bacon">OPTION COLLAPSE TRUE
 
dict$ = LOAD$(DIRNAME$(ME$) & "/unixdict.txt")
Line 188 ⟶ 218:
 
PRINT result$
PRINT "Total words: ", AMOUNT(dict$, NL$), ", and ", AMOUNT(result$, NL$), " are circular."</langsyntaxhighlight>
{{out}}
Using 'unixdict.txt':
Line 206 ⟶ 236:
=={{header|C}}==
{{libheader|GLib}}
<langsyntaxhighlight lang="c">#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
Line 312 ⟶ 342:
g_ptr_array_free(dictionary, TRUE);
return EXIT_SUCCESS;
}</langsyntaxhighlight>
 
{{out}}
Line 331 ⟶ 361:
 
=={{header|C++}}==
<langsyntaxhighlight lang="cpp">#include <algorithm>
#include <fstream>
#include <iostream>
Line 388 ⟶ 418:
}
return EXIT_SUCCESS;
}</langsyntaxhighlight>
 
{{out}}
Line 407 ⟶ 437:
 
=={{header|F_Sharp|F#}}==
<langsyntaxhighlight lang="fsharp">
// Teacup rim text. Nigel Galloway: August 7th., 2019
let N=System.IO.File.ReadAllLines("dict.txt")|>Array.filter(fun n->String.length n=3 && Seq.length(Seq.distinct n)>1)|>Set.ofArray
let fG z=Set.map(fun n->System.String(Array.ofSeq (Seq.permute(fun g->(g+z)%3)n))) N
Set.intersectMany [N;fG 1;fG 2]|>Seq.distinctBy(Seq.sort>>Array.ofSeq>>System.String)|>Seq.iter(printfn "%s")
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 423 ⟶ 453:
 
=={{header|Factor}}==
<langsyntaxhighlight lang="factor">USING: combinators.short-circuit fry grouping hash-sets
http.client kernel math prettyprint sequences sequences.extras
sets sorting splitting ;
Line 430 ⟶ 460:
"\n" split [ { [ length 3 < ] [ all-equal? ] } 1|| ] reject
[ [ all-rotations ] map ] [ >hash-set ] bi
'[ [ _ in? ] all? ] filter [ natural-sort ] map members .</langsyntaxhighlight>
{{out}}
<pre>
Line 443 ⟶ 473:
 
=={{header|Go}}==
<langsyntaxhighlight lang="go">package main
 
import (
Line 512 ⟶ 542:
fmt.Println()
}
}</langsyntaxhighlight>
 
{{out}}
Line 534 ⟶ 564:
===Using Data.Set===
Circular words of more than 2 characters in a local copy of a word list.
<langsyntaxhighlight lang="haskell">import Data.List (groupBy, intercalate, sort, sortBy)
import qualified Data.Set as S
import Data.Ord (comparing)
Line 564 ⟶ 594:
filter
((1 <) . length)
(groupBy (on (==) fst) (sortBy (comparing fst) (((,) =<< sort) <$> xs)))</langsyntaxhighlight>
{{Out}}
<pre>arc -> car -> rca
Line 575 ⟶ 605:
 
Or taking a different approach, we can avoid the use of Data.Set by obtaining the groups of anagrams (of more than two characters) in the lexicon, and filtering out a circular subset of these:
<langsyntaxhighlight lang="haskell">import Data.Function (on)
import Data.List (groupBy, intercalate, sort, sortOn)
import Data.Ord (comparing)
Line 621 ⟶ 651:
rotated :: [a] -> [a]
rotated [] = []
rotated (x : xs) = xs <> [x]</langsyntaxhighlight>
{{Out}}
<pre>arc -> rca -> car
Line 630 ⟶ 660:
 
=={{header|J}}==
<syntaxhighlight lang="j"> >@{.@> (#~ (=&#>@{.)@> * 2 < #@>)(</.~ {.@/:~@(|."0 1~ i.@#)L:0)cutLF fread'unixdict.txt'
Definitions, which are developed following the solution
apt
<lang J>
arc
read=: CR -.~ 1!:1@boxopen NB. dang that line end!
ate</syntaxhighlight>
Filter=:(#~`)(`:6)
 
prep=: (;~ /:~);._2
 
gba=: <@:([: ,/ (>@}."1))/.~ 0&{"1
ew=: (>:&# {.)S:_1 Filter
le=: (2 < #@{.)S:_1 Filter
ra=: a: -.~ rotations&>
 
NB. prep was separated for fun, not necessity
teacup=: ra@:le@:ew@:gba
 
rotations=: 3 :0
subset=: 0 = #@:-.
assert. 0 1 -: 'ab'(subset~ , subset)'cabag'
N=. # {. y
for_word. y do.
a=. N ]\ (, (<: N)&{.) word
if. a subset y do. word return. end.
end.
''
)
</lang>
Solution finds "apt", "arc", and "ate".
<lang J>
NB. D includes the ordered anagram
D=: prep read'd:\tmp\dict'
 
NB. transposed samples of the input to teacup
|: ({~ (7 ?@$ #)) D
┌──────────┬────────────┬───────┬──────────┬──────┬────────┬────────┐
│ aaegnprty│ aadeiloprtz│ gmpsuy│ eimnnptuu│ aipst│ agggint│ effoprr│
├──────────┼────────────┼───────┼──────────┼──────┼────────┼────────┤
│pageantry │trapezoidal │gypsum │neptunium │tapis │tagging │proffer │
└──────────┴────────────┴───────┴──────────┴──────┴────────┴────────┘
 
teacup D
┌───┬───┬───┐
│apt│arc│ate│
└───┴───┴───┘
</lang>
The action of the individual verbs shown here along with intermediate pronouns having such descriptive names as to describe the proverbial names demonstrates the construction of teacup.
<lang J>
TEST_DICTIONARY=: 'abc ah ate bac bca blort cab eat ha rat tar tea tra '
TEST=: prep TEST_DICTIONARY
 
] GROUPS_BY_ANAGRAM=: gba TEST
┌───┬──┬───┬─────┬───┐
│abc│ah│ate│blort│rat│
│bac│ha│eat│ │tar│
│bca│ │tea│ │tra│
│cab│ │ │ │ │
└───┴──┴───┴─────┴───┘
] ENOUGH_WORDS=: ew GROUPS_BY_ANAGRAM
┌───┬──┬───┬───┐
│abc│ah│ate│rat│
│bac│ha│eat│tar│
│bca│ │tea│tra│
│cab│ │ │ │
└───┴──┴───┴───┘
] LONG_ENOUGH=: le ENOUGH_WORDS
┌───┬───┬───┐
│abc│ate│rat│
│bac│eat│tar│
│bca│tea│tra│
│cab│ │ │
└───┴───┴───┘
 
In other words, group words by their canonical rotation (from all rotations: the earliest, alphabetically), select groups with at least three different words, where the word count matches the letter count, then extract the first word from each group.
] SOLUTION=: ROTATIONS_ACCEPTABLE=: ra LONG_ENOUGH
┌───┬───┐
│abc│ate│
└───┴───┘
</lang>
 
=={{header|Java}}==
{{trans|C++}}
<langsyntaxhighlight lang="java">import java.io.*;
import java.util.*;
 
Line 768 ⟶ 728:
return ch;
}
}</langsyntaxhighlight>
 
{{out}}
Line 790 ⟶ 750:
Reading a local dictionary with the macOS JS for Automation library:
{{Works with|JXA}}
<langsyntaxhighlight lang="javascript">(() => {
'use strict';
 
Line 947 ⟶ 907:
// MAIN ---
return main();
})();</langsyntaxhighlight>
{{Out}}
<pre>arc -> car -> rca
Line 958 ⟶ 918:
Reading a local dictionary with the macOS JS for Automation library:
{{Works with|JXA}}
<langsyntaxhighlight lang="javascript">(() => {
'use strict';
 
Line 1,097 ⟶ 1,057:
// MAIN ---
return main();
})();</langsyntaxhighlight>
{{Out}}
<pre>arc -> rca -> car
Line 1,112 ⟶ 1,072:
`keys`; this slows it down a lot.
 
<langsyntaxhighlight lang="jq"># Output: an array of the words when read around the rim
def read_teacup:
. as $in
Line 1,142 ⟶ 1,102:
# The task:
teacup_words
| read_teacup</langsyntaxhighlight>
{{out}}
Invocation example: jq -nRc -f teacup-rim.jq unixdict.txt
Line 1,154 ⟶ 1,114:
=={{header|Julia}}==
Using the MIT 10000 word list, and excluding words of less than three letters, to reduce output length.
<langsyntaxhighlight lang="julia">using HTTP
rotate(s, n) = String(circshift(Vector{UInt8}(s), n))
Line 1,169 ⟶ 1,129:
foreach(println, getteawords("https://www.mit.edu/~ecprice/wordlist.10000"))
</langsyntaxhighlight>{{out}}
<pre>
["aim", "ima", "mai"]
Line 1,183 ⟶ 1,143:
Using https://www.mit.edu/~ecprice/wordlist.10000 as per the Julia example.
 
<langsyntaxhighlight lang="javascript">
const wc = new CS.System.Net.WebClient();
const lines = wc.DownloadString("http://wiki.puzzlers.org/pub/wordlists/unixdict.txt");
Line 1,212 ⟶ 1,172:
.filter(key => collection[key].length > 1)
.forEach(key => console.log("%s", collection[key].join(", ")));
</syntaxhighlight>
</lang>
<pre>
apt, pta, tap
Line 1,220 ⟶ 1,180:
 
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<langsyntaxhighlight Mathematicalang="mathematica">ClearAll[Teacuppable]
TeacuppableHelper[set_List] := Module[{f, s},
f = First[set];
Line 1,240 ⟶ 1,200:
s = s[[All, All, 1]];
s //= Select[StringLength[First[#]] <= Length[#] &];
Flatten[Teacuppable /@ s, 1]</langsyntaxhighlight>
{{out}}
<pre>{{"apt", "pta", "tap"}, {"arc", "car", "rca"}, {"ate", "eat", "tea"}}</pre>
Line 1,246 ⟶ 1,206:
=={{header|Nim}}==
 
<langsyntaxhighlight Nimlang="nim">import sequtils, sets, sugar
 
let words = collect(initHashSet, for word in "unixdict.txt".lines: {word})
Line 1,275 ⟶ 1,235:
w.rotate()
stdout.write " → ", w
echo()</langsyntaxhighlight>
 
{{out}}
Line 1,284 ⟶ 1,244:
=={{header|Perl}}==
{{trans|Raku}}
<langsyntaxhighlight lang="perl">use strict;
use warnings;
use feature 'say';
Line 1,318 ⟶ 1,278:
}
 
say join ', ', uniqstr @$_ for sort @teacups;</langsyntaxhighlight>
{{out}}
<pre>ARC, RCA, CAR
Line 1,328 ⟶ 1,288:
=={{header|Phix}}==
Filters anagram lists
<!--<langsyntaxhighlight Phixlang="phix">-->
<span style="color: #008080;">procedure</span> <span style="color: #000000;">filter_set</span><span style="color: #0000FF;">(</span><span style="color: #004080;">sequence</span> <span style="color: #000000;">anagrams</span><span style="color: #0000FF;">)</span>
<span style="color: #000080;font-style:italic;">-- anagrams is a (small) set of words that are all anagrams of each other
Line 1,400 ⟶ 1,360:
--teacup(join_path({"demo","rosetta","words.txt"}),4,true)
-- Note that allow_mono is needed to display eg {"agag","gaga"}</span>
<!--</langsyntaxhighlight>-->
{{out}}
<pre>
Line 1,410 ⟶ 1,370:
 
=={{header|PicoLisp}}==
<langsyntaxhighlight PicoLisplang="picolisp">(de rotw (W)
(let W (chop W)
(unless (or (apply = W) (not (cddr W)))
Line 1,440 ⟶ 1,400:
Lst )
Lst ) ) )
Words ) )</langsyntaxhighlight>
{{out}}
<pre>
Line 1,451 ⟶ 1,411:
 
=={{header|PureBasic}}==
<langsyntaxhighlight PureBasiclang="purebasic">DataSection
dname:
Data.s "./Data/unixdict.txt"
Line 1,481 ⟶ 1,441:
bset="" : res="" : cw=0
Read.s dn
Wend</langsyntaxhighlight>
{{out}}
<pre>apt pta tap
Line 1,498 ⟶ 1,458:
===Functional===
Composing generic functions, and considering only anagram groups.
<langsyntaxhighlight lang="python">'''Teacup rim text'''
 
from itertools import chain, groupby
Line 1,695 ⟶ 1,655:
# MAIN ---
if __name__ == '__main__':
main()</langsyntaxhighlight>
{{Out}}
<pre>arc -> rca -> car
Line 1,712 ⟶ 1,672:
Defaults to unixdict.txt, minimum 3 characters and mono-character 'words' disallowed. Feed a file name to use a different word list, an integer to --min-chars and/or a truthy value to --mono to allow mono-chars.
 
<syntaxhighlight lang="raku" perl6line>my %*SUB-MAIN-OPTS = :named-anywhere;
 
unit sub MAIN ( $dict = 'unixdict.txt', :$min-chars = 3, :$mono = False );
Line 1,744 ⟶ 1,704:
}
 
say .unique.join(", ") for sort @teacups;</langsyntaxhighlight>
{{out|Defaults}}
Command line: <tt>raku teacup.p6</tt>
Line 1,799 ⟶ 1,759:
 
The dictionary wasn't assumed to be sorted in any way.
<langsyntaxhighlight lang="rexx">/*REXX pgm finds circular words (length>2), using a dictionary, suppress permutations.*/
parse arg iFID L . /*obtain optional arguments from the CL*/
if iFID==''|iFID=="," then iFID= 'wordlist.10k' /*Not specified? Then use the default.*/
Line 1,829 ⟶ 1,789:
end /*j*/
say
say cw ' circular words were found.' /*stick a fork in it, we're all done. */</langsyntaxhighlight>
{{out|output|text=&nbsp; when using the default inputs:}}
<pre>
Line 1,842 ⟶ 1,802:
 
5 circular words were found.
</pre>
 
=={{header|Ruby}}==
"woordenlijst.txt" is a Dutch wordlist. It has 413125 words > 2 chars and takes about two minutes.
<syntaxhighlight lang="ruby">lists = ["unixdict.txt", "wordlist.10000", "woordenlijst.txt"]
 
lists.each do |list|
words = open(list).readlines( chomp: true).reject{|w| w.size < 3 }
grouped_by_size = words.group_by(&:size)
tea_words = words.filter_map do |word|
chars = word.chars
next unless chars.none?{|c| c < chars.first }
next if chars.uniq.size == 1
rotations = word.size.times.map {|i| chars.rotate(i).join }
rotations if rotations.all?{|rot| grouped_by_size[rot.size].include? rot }
end
puts "", list + ":"
tea_words.uniq(&:to_set).each{|ar| puts ar.join(", ") }
end
</syntaxhighlight>
{{out}}
<pre>
unixdict.txt:
apt, pta, tap
arc, rca, car
ate, tea, eat
 
wordlist.10000:
aim, ima, mai
arc, rca, car
asp, spa, pas
ate, tea, eat
ips, psi, sip
 
woordenlijst.txt:
ast, sta, tas
een, ene, nee
eer, ere, ree
</pre>
 
=={{header|Rust}}==
<langsyntaxhighlight lang="rust">use std::collections::BTreeSet;
use std::collections::HashSet;
use std::fs::File;
Line 1,907 ⟶ 1,905:
Err(error) => eprintln!("Cannot open file {}: {}", &args[1], error),
}
}</langsyntaxhighlight>
 
{{out}}
Line 1,926 ⟶ 1,924:
 
=={{header|Swift}}==
<langsyntaxhighlight lang="swift">import Foundation
 
func loadDictionary(_ path: String) throws -> Set<String> {
Line 1,981 ⟶ 1,979:
} catch {
print(error)
}</langsyntaxhighlight>
 
{{out}}
Line 1,994 ⟶ 1,992:
{{libheader|Wren-str}}
{{libheader|Wren-sort}}
<langsyntaxhighlight ecmascriptlang="wren">import "io" for File
import "./str" for Str
import "./sort" for Find
 
var readWords = Fn.new { |fileName|
Line 2,032 ⟶ 2,030:
}
System.print()
}</langsyntaxhighlight>
 
{{out}}
Line 2,052 ⟶ 2,050:
 
=={{header|zkl}}==
<langsyntaxhighlight lang="zkl">// Limited to ASCII
// This is limited to the max items a Dictionary can hold
fcn teacut(wordFile){
Line 2,069 ⟶ 2,067:
}
}
}</langsyntaxhighlight>
<langsyntaxhighlight lang="zkl">println("\nunixdict:"); teacut("unixdict.txt");
println("\nmit_wordlist_10000:"); teacut("mit_wordlist_10000.txt");</langsyntaxhighlight>
{{out}}
<pre>
9,476

edits