Changeable words: Difference between revisions

m
syntax highlighting fixup automation
(J)
m (syntax highlighting fixup automation)
Line 23:
{{trans|Go}}
 
<langsyntaxhighlight lang=11l>V words = File(‘unixdict.txt’).read().split("\n").filter(word -> word.len > 11)
 
F hamming_dist(word1, word2)
Line 46:
count += 2
 
print("\nFound "count‘ changeable words.’)</langsyntaxhighlight>
 
{{out}}
Line 83:
 
=={{header|Ada}}==
<langsyntaxhighlight lang=Ada>with Ada.Text_Io;
with Ada.Strings.Fixed;
with Ada.Containers.Indefinite_Vectors;
Line 134:
end loop;
end loop;
end Changeable;</langsyntaxhighlight>
{{out}}
<pre>
Line 167:
=={{header|Arturo}}==
 
<langsyntaxhighlight lang=rebol>wordset: map read.lines relative "unixdict.txt" => strip
 
wordset: select wordset 'word -> 12 =< size word
Line 182:
 
loop unique results 'result ->
print join.with:" - " result</langsyntaxhighlight>
 
{{out}}
Line 214:
 
=={{header|AutoHotkey}}==
<langsyntaxhighlight lang=AutoHotkey>FileRead, db, % A_Desktop "\unixdict.txt"
oWord := [], Found := []
for i, word in StrSplit(db, "`n", "`r")
Line 235:
num++
return (StrLen(s1) - num = 1)
}</langsyntaxhighlight>
{{out}}
<pre>aristotelean <-> aristotelian
Line 265:
 
=={{header|AWK}}==
<langsyntaxhighlight lang=AWK>
# syntax: GAWK -f CHANGEABLE_WORDS.AWK unixdict.txt
#
Line 312:
exit(0)
}
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 370:
=={{header|C}}==
{{trans|Go}}
<langsyntaxhighlight lang=c>#include <stdio.h>
#include <stdlib.h>
#include <string.h>
Line 455:
free(dictionary);
return EXIT_SUCCESS;
}</langsyntaxhighlight>
 
{{out}}
Line 516:
=={{header|C++}}==
{{trans|Go}}
<langsyntaxhighlight lang=cpp>#include <cstdlib>
#include <fstream>
#include <iomanip>
Line 563:
}
return EXIT_SUCCESS;
}</langsyntaxhighlight>
 
{{out}}
Line 623:
 
=={{header|F_Sharp|F#}}==
<langsyntaxhighlight lang=fsharp>
// Changeable words: Nigel Galloway. June 14th., 2021
let fN g=Seq.fold2(fun z n g->z+if n=g then 0 else 1) 0 g
Line 629:
seq{use n=System.IO.File.OpenText("unixdict.txt") in while not n.EndOfStream do yield n.ReadLine()}|>Seq.filter(fun n->n.Length>11)
|>List.ofSeq|>List.groupBy(fun n->n.Length)|>Seq.collect(fun(_,n)->fG [] n)|>Seq.iter(fun(n,g)->printfn "%s <=> %s" n g)
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 661:
=={{header|Factor}}==
{{works with|Factor|0.99 2021-02-05}}
<langsyntaxhighlight lang=factor>USING: assocs combinators.short-circuit formatting
io.encodings.ascii io.files kernel math math.combinatorics
math.distances sequences ;
Line 671:
[ length 11 > ] filter
2 [ first2 changeable? ] filter-combinations
[ "%s <-> %s\n" printf ] assoc-each</langsyntaxhighlight>
{{out}}
<pre>
Line 704:
=={{header|FreeBASIC}}==
Brute force method, reuses some code from [[Odd_words#FreeBASIC]].
<langsyntaxhighlight lang=freebasic>
#define NULL 0
 
Line 760:
next i
curr = curr->nxt
wend</langsyntaxhighlight>
{{out}}
<pre>
Line 818:
=={{header|Go}}==
{{trans|Wren}}
<langsyntaxhighlight lang=go>package main
 
import (
Line 870:
}
}
}</langsyntaxhighlight>
 
{{out}}
Line 930:
 
=={{header|J}}==
<langsyntaxhighlight lang=J> ;:inv ({~ ~.@($ /:~"1@#: I.@,)@(1=+/ .~: ::0:&>/~))(#~ 11<#@>) cutLF fread'unixdict.txt'
aristotelean aristotelian
claustrophobia claustrophobic
Line 956:
spectroscope spectroscopy
underclassman underclassmen
upperclassman upperclassmen</langsyntaxhighlight>
 
=={{header|Java}}==
{{trans|Go}}
<langsyntaxhighlight lang=java>import java.io.*;
import java.util.*;
 
Line 1,003:
return count;
}
}</langsyntaxhighlight>
 
{{out}}
Line 1,065:
{{works with|jq}}
'''Works with gojq, the Go implementation of jq'''
<langsyntaxhighlight lang=jq># Emit a stream of words "greater than" the input word that differ by just one character.
def changeable_to($dict):
. as $w
Line 1,080:
| . as $w
| [changeable_to($dict)] | unique[]
| "\($w) <=> \(.)"</langsyntaxhighlight>
Invocation: jq -n -rR -f changeable-words.jq unixdict.txt
{{out}}
Line 1,114:
=={{header|Julia}}==
See [[Alternade_words#Julia]] for the foreachword function.
<langsyntaxhighlight lang=julia>const alts = Set{String}()
function ischangeable(w, d)
alternatives = [w[1:p[1]-1] * p[2] * w[p[1]+1:end] for p in Iterators.product(1:length(w), 'a':'z')]
Line 1,130:
 
foreachword("unixdict.txt", ischangeable, minlen = 12, colwidth=40, numcols=2)
</langsyntaxhighlight>{{out}}
<pre>
Word source: unixdict.txt
Line 1,151:
=={{header|Nim}}==
Using the standard module <code>std/editdistance</code> to get the distance between two words.
<langsyntaxhighlight lang=Nim>import std/editdistance, sugar
 
# Build list of words with length >= 12.
Line 1,169:
inc count, 2
 
echo "\nFound ", count, " changeable words."</langsyntaxhighlight>
 
{{out}}
Line 1,204:
 
=={{header|Perl}}==
<langsyntaxhighlight lang=perl>#!/usr/bin/perl
 
use strict;
Line 1,220:
}
push @{ $words[length] }, $_;
}</langsyntaxhighlight>
{{out}}
<pre>
Line 1,252:
 
=={{header|Perl}}==
<langsyntaxhighlight lang=perl>#!/usr/bin/perl
 
use strict; # https://rosettacode.org/wiki/Changeable_words
Line 1,260:
my $count = 0;
printf "%3d: %15s <-> %s\n", ++$count, $1, $4
while /^ ((\N*)\N(\N*)) \n(?=.*^ (\2\N\3) \n)/gmsx;</langsyntaxhighlight>
{{out}}
<pre>
Line 1,292:
 
=={{header|Phix}}==
<!--<langsyntaxhighlight lang=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;">changeable</span><span style="color: #0000FF;">(</span><span style="color: #004080;">string</span> <span style="color: #000000;">a</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">b</span><span style="color: #0000FF;">)</span>
Line 1,309:
<span style="color: #008080;">end</span> <span style="color: #008080;">for</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 changeable words found:\n%s\n"</span><span style="color: #0000FF;">,{</span><span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">res</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;">res</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><span style="color: #008000;">"\n"</span><span style="color: #0000FF;">)})</span>
<!--</langsyntaxhighlight>-->
{{out}}
<pre>
Line 1,325:
{{works with|SWI Prolog}}
{{trans|Go}}
<langsyntaxhighlight lang=prolog>:- dynamic dictionary_word/1.
 
main:-
Line 1,375:
Count1 is Count + 1,
Count1 < 2,
hamming_distance(Chars1, Chars2, Dist, Count1).</langsyntaxhighlight>
 
{{out}}
Line 1,436:
=={{header|Python}}==
 
<langsyntaxhighlight lang=python>from collections import defaultdict, Counter
 
 
Line 1,474:
print(f"\n{len(cwords)} words that are from changing a char from other words:")
for v in sorted(cwords):
print(f" {v[0]:12} From {', '.join(v[1:])}")</langsyntaxhighlight>
 
{{out}}
Line 1,517:
 
Get the best of both worlds by doing an initial filter with Sorensen, then get exact results with Levenshtein.
<syntaxhighlight lang=raku perl6line>use Text::Levenshtein;
use Text::Sorensen :sorensen;
 
Line 1,535:
%skip{$_}++ for @sorensens[@levenshtein];
": {$this.fmt('%14s')} <-> ", @sorensens[@levenshtein].join: ', ';
}</langsyntaxhighlight>
{{out}}
<pre> 1: aristotelean <-> aristotelian
Line 1,569:
 
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 program finds changeable words (within an identified dict.), changing any letter.*/
parse arg minL iFID . /*obtain optional arguments from the CL*/
if minL=='' | minL=="," then minL= 12 /*Not specified? Then use the default.*/
Line 1,605:
#= # - 1 /*adjust word count because of DO loop.*/
say copies('─', 30) # "words ("n 'usable words) in the dictionary file: ' iFID
return</langsyntaxhighlight>
{{out|output|text=&nbsp; when using the default inputs:}}
<pre>
Line 1,665:
 
=={{header|Ring}}==
<langsyntaxhighlight lang=ring>
cStr = read("unixdict.txt")
wordList = str2list(cStr)
Line 1,701:
 
see "done..." + nl
</syntaxhighlight>
</lang>
Output:
<pre>
Line 1,762:
 
=={{header|Ruby}}==
<langsyntaxhighlight lang=ruby>words = File.open("unixdict.txt").readlines.map(&:chomp).select{|w| w.size > 11 }
 
size_groups = words.group_by(&:size).sort.map(&:last)
Line 1,771:
puts "Found #{res.size} changeable word pairs:"
res.each{|w1, w2|puts "#{w1} - #{w2}" }
</syntaxhighlight>
</lang>
{{out}}
<pre>Found 26 changeable word pairs:
Line 1,803:
=={{header|Sidef}}==
{{trans|Perl}}
<langsyntaxhighlight lang=ruby>var file = File("unixdict.txt")
 
if (!file.exists) {
Line 1,831:
}
bucket{len} << word
}</langsyntaxhighlight>
{{out}}
<pre>
Line 1,865:
{{libheader|Wren-fmt}}
Using the Hamming Distance between two equal length strings which needs to be 1 here:
<langsyntaxhighlight lang=ecmascript>import "io" for File
import "/fmt" for Fmt
 
Line 1,896:
}
}
}</langsyntaxhighlight>
 
{{out}}
Line 1,956:
 
=={{header|XPL0}}==
<langsyntaxhighlight lang=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 2,029:
DI:= DI+1;
until DI >= DictSize;
]</langsyntaxhighlight>
 
{{out}}
10,333

edits