Hex words: Difference between revisions

m
syntax highlighting fixup automation
(→‎{{header|J}}: flip second part to match (now clearly stated) requirement, also reduce vertical page space used)
m (syntax highlighting fixup automation)
Line 16:
{{trans|Python}}
 
<langsyntaxhighlight lang="11l">F digroot(=n)
L n > 9
n = sum(String(n).map(d -> Int(d)))
Line 35:
print(f:‘{a[2]} {a[0]:<6}{a[1]:10}’)
 
print(‘Total count of those words: ’results.len)</langsyntaxhighlight>
 
{{out}}
Line 90:
 
=={{header|ALGOL 68}}==
<langsyntaxhighlight lang="algol68"># find words that contain only hex digits a-f #
IF FILE input file;
STRING file name = "unixdict.txt";
Line 214:
OD;
print( ( "Found ", whole( count 4, 0 ), " hex words with 4 or more distinct digits", newline ) )
FI</langsyntaxhighlight>
{{out}}
<pre>
Line 263:
=={{header|Arturo}}==
 
<langsyntaxhighlight lang="rebol">words: map read.lines relative "unixdict.txt" => strip
hexWords: new []
 
Line 301:
printTable sort.by:'root hexWords
printTable sort.descending.by:'decimal select hexWords 'h ->
4 =< size unique split h\hex</langsyntaxhighlight>
 
{{out}}
Line 355:
 
=={{header|AutoHotkey}}==
<langsyntaxhighlight AutoHotkeylang="autohotkey">FileRead, wList, % A_Desktop "\unixdict.txt"
hexWords := Hex_words(wList)
Header := "Base 10`t`tWord`tRoot`n"
Line 398:
until (sum < 10)
return sum
}</langsyntaxhighlight>
{{out}}
<pre>Base 10 Word Root
Line 445:
 
=={{header|AWK}}==
<syntaxhighlight lang="awk">
<lang AWK>
# syntax: GAWK -f HEX_WORDS.AWK unixdict.txt
{ nf += NF
Line 500:
return num + (length(s) ? 16*hex2dec(s) : 0)
}
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 551:
=={{header|Factor}}==
{{works with|Factor|0.99 2021-06-02}}
<langsyntaxhighlight lang="factor">USING: formatting io io.encodings.ascii io.files kernel literals
math math.parser prettyprint sequences sets sorting ;
 
Line 572:
 
words [ cardinality 3 > ] filter [ hex> ] f info-by
" such words found which contain 4 or more different digits." print</langsyntaxhighlight>
{{out}}
<pre>
Line 620:
 
=={{header|J}}==
<langsyntaxhighlight Jlang="j"> (#~$&1 0@#)(#,&":])/:~((+/@(".&>@":)^:_@]; ;~) dfh)@> (#~ (*/@e.&'abcdef' * 3<#)@>) cutLF fread'unixdict.txt'
26
│1│43966 │abbe │
Line 662:
│3│44013 │abed │
│1│14600926│decade│
│1│57007 │deaf │</langsyntaxhighlight>
 
=={{header|jq}}==
Line 669:
 
'''Preliminaries'''
<langsyntaxhighlight lang="jq">def lpad($len): tostring | ($len - length) as $l | (" " * $l)[:$l] + .;
 
# . may be a decimal number or a string representing a decimal number
Line 691:
.[1] += $i * .[0]
| .[0] *= 16 )
| .[1]; </langsyntaxhighlight>
'''The Task'''
<langsyntaxhighlight lang="jq">def task:
 
def format: "\(.[0]|lpad(8)) -> \(.[1]|lpad(9)) -> \(.[2])";
Line 715:
(($digits4|sort_by(.[1])|reverse[] ) | format) ;
 
task</langsyntaxhighlight>
{{out}}
'''Invocation'''
Line 767:
 
=={{header|Julia}}==
<langsyntaxhighlight lang="julia">digroot(n) = (while n > 9 n = sum(digits(n)) end; n)
 
function hexwords(wordfile = "unixdict.txt")
Line 790:
 
hexwords()
</langsyntaxhighlight>{{out}}
<pre>
Hex words in unixdict.txt:
Line 843:
 
=={{header|Perl}}==
<langsyntaxhighlight lang="perl">#!/usr/bin/perl
 
use strict; # https://rosettacode.org/wiki/Hex_words
Line 863:
"total count = @{[ scalar @byroot ]} and @{[ scalar @bydecimal
]} have at least 4 distinct digits\n",
reverse nsort_by { (split ' ')[1] } @bydecimal;</langsyntaxhighlight>
{{out}}
<pre>
Line 909:
 
=={{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;">af</span><span style="color: #0000FF;">(</span><span style="color: #004080;">string</span> <span style="color: #000000;">s</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">return</span> <span style="color: #7060A8;">max</span><span style="color: #0000FF;">(</span><span style="color: #000000;">s</span><span style="color: #0000FF;">)<=</span><span style="color: #008000;">'f'</span> <span style="color: #008080;">and</span> <span style="color: #7060A8;">min</span><span style="color: #0000FF;">(</span><span style="color: #000000;">s</span><span style="color: #0000FF;">)>=</span><span style="color: #008000;">'a'</span> <span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
Line 938:
<span style="color: #000000;">tags</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">extract</span><span style="color: #0000FF;">(</span><span style="color: #000000;">tags</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">reverse</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">custom_sort</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">extract</span><span style="color: #0000FF;">(</span><span style="color: #000000;">decml</span><span style="color: #0000FF;">,</span><span style="color: #000000;">tags</span><span style="color: #0000FF;">),</span><span style="color: #7060A8;">tagset</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">tags</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 with 4 or more distinct characters:\n\n %s\n"</span><span style="color: #0000FF;">,{</span><span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">tags</span><span style="color: #0000FF;">),</span><span style="color: #000000;">caejasp</span><span style="color: #0000FF;">()})</span>
<!--</langsyntaxhighlight>-->
{{out}}
<pre>
Line 987:
 
=={{header|Python}}==
<langsyntaxhighlight lang="python">def digroot(n):
while n > 9:
n = sum([int(d) for d in str(n)])
Line 1,010:
print("Total count of those words:", len(results))
</langsyntaxhighlight>{{out}}
<pre>
Hex words in unixdict.txt:
Line 1,064:
=={{header|Raku}}==
Sorted by digital root with a secondary alphabetical sort.
<syntaxhighlight lang="raku" perl6line>sub dr (Int $_ is copy) { $_ = dr(.comb.sum) while .chars > 1; $_ }
 
my %hex = './unixdict.txt'.IO.slurp.words.grep( *.chars > 3 )\
Line 1,075:
 
say "\nOf which {+%many} contain at least four distinct characters:";
printf "%6s ➡ %8d ➡ %d\n", .key, :16(.key), .value for %many.sort: { -:16(.key) }</langsyntaxhighlight>
{{out}}
<pre>26 hex words longer than 3 characters found in unixdict.txt:
Line 1,122:
=={{header|Vlang}}==
{{trans|wren}}
<langsyntaxhighlight lang="vlang">import os
import strconv
import math
Line 1,178:
}
println('$lines.len hex words with 4 or more distinct letters found')
}</langsyntaxhighlight>
{{out}}
<pre>Same as wren entry</pre>
Line 1,187:
{{libheader|Wren-math}}
{{libheader|Wren-seq}}
<langsyntaxhighlight lang="ecmascript">import "./ioutil" for FileUtil
import "./fmt" for Conv, Fmt
import "./math" for Int
Line 1,213:
digits4.sort { |a, b| Num.fromString(a.split("->")[1]) > Num.fromString(b.split("->")[1]) }
System.print(digits4.join("\n"))
System.print("\n%(digits4.count) such words found which contain 4 or more different digits.")</langsyntaxhighlight>
 
{{out}}
10,333

edits