Hex words: Difference between revisions

Content added Content deleted
(→‎{{header|J}}: flip second part to match (now clearly stated) requirement, also reduce vertical page space used)
m (syntax highlighting fixup automation)
Line 16: Line 16:
{{trans|Python}}
{{trans|Python}}


<lang 11l>F digroot(=n)
<syntaxhighlight lang="11l">F digroot(=n)
L n > 9
L n > 9
n = sum(String(n).map(d -> Int(d)))
n = sum(String(n).map(d -> Int(d)))
Line 35: Line 35:
print(f:‘{a[2]} {a[0]:<6}{a[1]:10}’)
print(f:‘{a[2]} {a[0]:<6}{a[1]:10}’)


print(‘Total count of those words: ’results.len)</lang>
print(‘Total count of those words: ’results.len)</syntaxhighlight>


{{out}}
{{out}}
Line 90: Line 90:


=={{header|ALGOL 68}}==
=={{header|ALGOL 68}}==
<lang algol68># find words that contain only hex digits a-f #
<syntaxhighlight lang="algol68"># find words that contain only hex digits a-f #
IF FILE input file;
IF FILE input file;
STRING file name = "unixdict.txt";
STRING file name = "unixdict.txt";
Line 214: Line 214:
OD;
OD;
print( ( "Found ", whole( count 4, 0 ), " hex words with 4 or more distinct digits", newline ) )
print( ( "Found ", whole( count 4, 0 ), " hex words with 4 or more distinct digits", newline ) )
FI</lang>
FI</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 263: Line 263:
=={{header|Arturo}}==
=={{header|Arturo}}==


<lang rebol>words: map read.lines relative "unixdict.txt" => strip
<syntaxhighlight lang="rebol">words: map read.lines relative "unixdict.txt" => strip
hexWords: new []
hexWords: new []


Line 301: Line 301:
printTable sort.by:'root hexWords
printTable sort.by:'root hexWords
printTable sort.descending.by:'decimal select hexWords 'h ->
printTable sort.descending.by:'decimal select hexWords 'h ->
4 =< size unique split h\hex</lang>
4 =< size unique split h\hex</syntaxhighlight>


{{out}}
{{out}}
Line 355: Line 355:


=={{header|AutoHotkey}}==
=={{header|AutoHotkey}}==
<lang AutoHotkey>FileRead, wList, % A_Desktop "\unixdict.txt"
<syntaxhighlight lang="autohotkey">FileRead, wList, % A_Desktop "\unixdict.txt"
hexWords := Hex_words(wList)
hexWords := Hex_words(wList)
Header := "Base 10`t`tWord`tRoot`n"
Header := "Base 10`t`tWord`tRoot`n"
Line 398: Line 398:
until (sum < 10)
until (sum < 10)
return sum
return sum
}</lang>
}</syntaxhighlight>
{{out}}
{{out}}
<pre>Base 10 Word Root
<pre>Base 10 Word Root
Line 445: Line 445:


=={{header|AWK}}==
=={{header|AWK}}==
<syntaxhighlight lang="awk">
<lang AWK>
# syntax: GAWK -f HEX_WORDS.AWK unixdict.txt
# syntax: GAWK -f HEX_WORDS.AWK unixdict.txt
{ nf += NF
{ nf += NF
Line 500: Line 500:
return num + (length(s) ? 16*hex2dec(s) : 0)
return num + (length(s) ? 16*hex2dec(s) : 0)
}
}
</syntaxhighlight>
</lang>
{{out}}
{{out}}
<pre>
<pre>
Line 551: Line 551:
=={{header|Factor}}==
=={{header|Factor}}==
{{works with|Factor|0.99 2021-06-02}}
{{works with|Factor|0.99 2021-06-02}}
<lang factor>USING: formatting io io.encodings.ascii io.files kernel literals
<syntaxhighlight lang="factor">USING: formatting io io.encodings.ascii io.files kernel literals
math math.parser prettyprint sequences sets sorting ;
math math.parser prettyprint sequences sets sorting ;


Line 572: Line 572:


words [ cardinality 3 > ] filter [ hex> ] f info-by
words [ cardinality 3 > ] filter [ hex> ] f info-by
" such words found which contain 4 or more different digits." print</lang>
" such words found which contain 4 or more different digits." print</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 620: Line 620:


=={{header|J}}==
=={{header|J}}==
<lang J> (#~$&1 0@#)(#,&":])/:~((+/@(".&>@":)^:_@]; ;~) dfh)@> (#~ (*/@e.&'abcdef' * 3<#)@>) cutLF fread'unixdict.txt'
<syntaxhighlight lang="j"> (#~$&1 0@#)(#,&":])/:~((+/@(".&>@":)^:_@]; ;~) dfh)@> (#~ (*/@e.&'abcdef' * 3<#)@>) cutLF fread'unixdict.txt'
26
26
│1│43966 │abbe │
│1│43966 │abbe │
Line 662: Line 662:
│3│44013 │abed │
│3│44013 │abed │
│1│14600926│decade│
│1│14600926│decade│
│1│57007 │deaf │</lang>
│1│57007 │deaf │</syntaxhighlight>


=={{header|jq}}==
=={{header|jq}}==
Line 669: Line 669:


'''Preliminaries'''
'''Preliminaries'''
<lang jq>def lpad($len): tostring | ($len - length) as $l | (" " * $l)[:$l] + .;
<syntaxhighlight lang="jq">def lpad($len): tostring | ($len - length) as $l | (" " * $l)[:$l] + .;


# . may be a decimal number or a string representing a decimal number
# . may be a decimal number or a string representing a decimal number
Line 691: Line 691:
.[1] += $i * .[0]
.[1] += $i * .[0]
| .[0] *= 16 )
| .[0] *= 16 )
| .[1]; </lang>
| .[1]; </syntaxhighlight>
'''The Task'''
'''The Task'''
<lang jq>def task:
<syntaxhighlight lang="jq">def task:


def format: "\(.[0]|lpad(8)) -> \(.[1]|lpad(9)) -> \(.[2])";
def format: "\(.[0]|lpad(8)) -> \(.[1]|lpad(9)) -> \(.[2])";
Line 715: Line 715:
(($digits4|sort_by(.[1])|reverse[] ) | format) ;
(($digits4|sort_by(.[1])|reverse[] ) | format) ;


task</lang>
task</syntaxhighlight>
{{out}}
{{out}}
'''Invocation'''
'''Invocation'''
Line 767: Line 767:


=={{header|Julia}}==
=={{header|Julia}}==
<lang julia>digroot(n) = (while n > 9 n = sum(digits(n)) end; n)
<syntaxhighlight lang="julia">digroot(n) = (while n > 9 n = sum(digits(n)) end; n)


function hexwords(wordfile = "unixdict.txt")
function hexwords(wordfile = "unixdict.txt")
Line 790: Line 790:


hexwords()
hexwords()
</lang>{{out}}
</syntaxhighlight>{{out}}
<pre>
<pre>
Hex words in unixdict.txt:
Hex words in unixdict.txt:
Line 843: Line 843:


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


use strict; # https://rosettacode.org/wiki/Hex_words
use strict; # https://rosettacode.org/wiki/Hex_words
Line 863: Line 863:
"total count = @{[ scalar @byroot ]} and @{[ scalar @bydecimal
"total count = @{[ scalar @byroot ]} and @{[ scalar @bydecimal
]} have at least 4 distinct digits\n",
]} have at least 4 distinct digits\n",
reverse nsort_by { (split ' ')[1] } @bydecimal;</lang>
reverse nsort_by { (split ' ')[1] } @bydecimal;</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 909: Line 909:


=={{header|Phix}}==
=={{header|Phix}}==
<!--<lang Phix>(phixonline)-->
<!--<syntaxhighlight lang="phix">(phixonline)-->
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<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>
<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: 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: #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>
<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>
<!--</lang>-->
<!--</syntaxhighlight>-->
{{out}}
{{out}}
<pre>
<pre>
Line 987: Line 987:


=={{header|Python}}==
=={{header|Python}}==
<lang python>def digroot(n):
<syntaxhighlight lang="python">def digroot(n):
while n > 9:
while n > 9:
n = sum([int(d) for d in str(n)])
n = sum([int(d) for d in str(n)])
Line 1,010: Line 1,010:
print("Total count of those words:", len(results))
print("Total count of those words:", len(results))
</lang>{{out}}
</syntaxhighlight>{{out}}
<pre>
<pre>
Hex words in unixdict.txt:
Hex words in unixdict.txt:
Line 1,064: Line 1,064:
=={{header|Raku}}==
=={{header|Raku}}==
Sorted by digital root with a secondary alphabetical sort.
Sorted by digital root with a secondary alphabetical sort.
<lang perl6>sub dr (Int $_ is copy) { $_ = dr(.comb.sum) while .chars > 1; $_ }
<syntaxhighlight lang="raku" line>sub dr (Int $_ is copy) { $_ = dr(.comb.sum) while .chars > 1; $_ }


my %hex = './unixdict.txt'.IO.slurp.words.grep( *.chars > 3 )\
my %hex = './unixdict.txt'.IO.slurp.words.grep( *.chars > 3 )\
Line 1,075: Line 1,075:


say "\nOf which {+%many} contain at least four distinct characters:";
say "\nOf which {+%many} contain at least four distinct characters:";
printf "%6s ➡ %8d ➡ %d\n", .key, :16(.key), .value for %many.sort: { -:16(.key) }</lang>
printf "%6s ➡ %8d ➡ %d\n", .key, :16(.key), .value for %many.sort: { -:16(.key) }</syntaxhighlight>
{{out}}
{{out}}
<pre>26 hex words longer than 3 characters found in unixdict.txt:
<pre>26 hex words longer than 3 characters found in unixdict.txt:
Line 1,122: Line 1,122:
=={{header|Vlang}}==
=={{header|Vlang}}==
{{trans|wren}}
{{trans|wren}}
<lang vlang>import os
<syntaxhighlight lang="vlang">import os
import strconv
import strconv
import math
import math
Line 1,178: Line 1,178:
}
}
println('$lines.len hex words with 4 or more distinct letters found')
println('$lines.len hex words with 4 or more distinct letters found')
}</lang>
}</syntaxhighlight>
{{out}}
{{out}}
<pre>Same as wren entry</pre>
<pre>Same as wren entry</pre>
Line 1,187: Line 1,187:
{{libheader|Wren-math}}
{{libheader|Wren-math}}
{{libheader|Wren-seq}}
{{libheader|Wren-seq}}
<lang ecmascript>import "./ioutil" for FileUtil
<syntaxhighlight lang="ecmascript">import "./ioutil" for FileUtil
import "./fmt" for Conv, Fmt
import "./fmt" for Conv, Fmt
import "./math" for Int
import "./math" for Int
Line 1,213: Line 1,213:
digits4.sort { |a, b| Num.fromString(a.split("->")[1]) > Num.fromString(b.split("->")[1]) }
digits4.sort { |a, b| Num.fromString(a.split("->")[1]) > Num.fromString(b.split("->")[1]) }
System.print(digits4.join("\n"))
System.print(digits4.join("\n"))
System.print("\n%(digits4.count) such words found which contain 4 or more different digits.")</lang>
System.print("\n%(digits4.count) such words found which contain 4 or more different digits.")</syntaxhighlight>


{{out}}
{{out}}