ABC words: Difference between revisions

m
Automated syntax highlighting fixup (second round - minor fixes)
m (syntax highlighting fixup automation)
m (Automated syntax highlighting fixup (second round - minor fixes))
Line 1:
{{draft task}}
 
{{omit from|6502 Assembly|unixdict.txt is much larger than the CPU's address space.}}
{{omit from|8080 Assembly|See 6502 Assembly.}}
{{omit from|Z80 Assembly|See 6502 Assembly.}}
 
;Definition
Line 19 ⟶ 16:
 
=={{header|11l}}==
<syntaxhighlight lang="11l">L(ln) File(‘unixdict.txt’).read().split("\n")
V? a = ln.find(‘a’)
I a != N
Line 87 ⟶ 84:
=={{header|Action!}}==
In the following solution the input file [https://gitlab.com/amarok8bit/action-rosetta-code/-/blob/master/source/unixdict.txt unixdict.txt] is loaded from H6 drive. Altirra emulator automatically converts CR/LF character from ASCII into 155 character in ATASCII charset used by Atari 8-bit computer when one from H6-H10 hard drive under DOS 2.5 is used.
<syntaxhighlight lang=Action"action!">BYTE FUNC FindC(CHAR ARRAY text CHAR c)
BYTE i
 
Line 160 ⟶ 157:
 
=={{header|Ada}}==
<syntaxhighlight lang=Ada"ada">with Ada.Text_Io;
with Ada.Strings.Fixed;
 
Line 205 ⟶ 202:
 
=={{header|ALGOL 68}}==
<syntaxhighlight lang="algol68"># find words that have "a", "b" and "C" in order in them #
IF FILE input file;
STRING file name = "unixdict.txt";
Line 309 ⟶ 306:
=={{header|APL}}==
{{works with|Dyalog APL}}
<syntaxhighlight lang="apl">abcwords←{
⍺←'abc'
words←((~∊)∘⎕TC⊆⊢) 80 ¯1⎕MAP ⍵
Line 332 ⟶ 329:
===Core language===
This is a fairly simple solution, hard-coded for "a", "b", and "c". The 'offset' commands are performed by AppleScript's StandardAdditions OSAX, so the time taken by the multiple communications between the script and the OSAX makes the code comparatively slow. Still, the overall running time with the specified file on my current machine is less than 1.5 seconds.
<syntaxhighlight lang="applescript">on abcWords(wordFile)
-- The word file text is assumed to be UTF-8 encoded and to have one word per line.
script o
Line 353 ⟶ 350:
 
{{output}}
<syntaxhighlight lang="applescript">{"aback", "abacus", "abc", "abdicate", "abduct", "abeyance", "abject", "abreact", "abscess", "abscissa", "abscissae", "absence", "abstract", "abstracter", "abstractor", "adiabatic", "aerobacter", "aerobic", "albacore", "alberich", "albrecht", "algebraic", "alphabetic", "ambiance", "ambuscade", "aminobenzoic", "anaerobic", "arabic", "athabascan", "auerbach", "diabetic", "diabolic", "drawback", "fabric", "fabricate", "flashback", "halfback", "iambic", "lampblack", "leatherback", "metabolic", "nabisco", "paperback", "parabolic", "playback", "prefabricate", "quarterback", "razorback", "roadblock", "sabbatical", "snapback", "strabismic", "syllabic", "tabernacle", "tablecloth"}</syntaxhighlight>
 
The following alternative uses delimiters and text items instead and is considerably faster at around 0.25 seconds. Also, for the hell of it, it takes the characters (or even longer substrings) that the returned words must contain as a parameter. Same output here as above.
 
<syntaxhighlight lang="applescript">on abcWords(wordFile, theLetters)
-- The word file text is assumed to be UTF-8 encoded and to have one word per line.
script o
Line 389 ⟶ 386:
===AppleScriptObjC===
This is faster still at 0.01 seconds and uses AppleScriptObjC to access the regex facilities provided by macOS's Foundation framework. It too takes the characters the returned words must contain as a parameter, but, unlike the script above, doesn't recognise longer substring inputs as units in themselves. Same output as with the two "Core language" scripts above.
<syntaxhighlight lang="applescript">use AppleScript version "2.4" -- OS X 10.10 (Yosemite) or later
use framework "Foundation"
use scripting additions
Line 432 ⟶ 429:
=={{header|Arturo}}==
 
<syntaxhighlight lang="rebol">words: read.lines relative "unixdict.txt"
 
isABC?: function [w][
Line 510 ⟶ 507:
 
=={{header|AutoHotkey}}==
<syntaxhighlight lang="autohotkey">FileRead, unixdict, unixdict.txt
Loop, Parse, unixdict, `n
if ABCWord(A_LoopField)
Line 595 ⟶ 592:
The following one-liner entered into a Posix shell returns the same 55 words as other entries.
 
<syntaxhighlight lang="awk">awk '/^[^bc]*a[^c]*b.*c/' unixdict.txt</syntaxhighlight>
 
=={{header|BASIC}}==
<syntaxhighlight lang="basic">10 DEFINT A,B,C: DEFSTR W
20 OPEN "I",1,"unixdict.txt"
30 IF EOF(1) THEN END
Line 621 ⟶ 618:
 
=={{header|BCPL}}==
<syntaxhighlight lang="bcpl">get "libhdr"
 
let find(s, c) = valof
Line 716 ⟶ 713:
 
=={{header|C}}==
<syntaxhighlight lang="c">#include <stdio.h>
#include <string.h>
 
Line 799 ⟶ 796:
=={{header|C sharp|C#}}==
Takes an optional command line for other character combinations. User can specify any reasonable number of unique characters. Caveat: see discussion page for issue about specifying repeated characters.
<syntaxhighlight lang="csharp">class Program {
static void Main(string[] args) { int bi, i = 0; string chars = args.Length < 1 ? "abc" : args[0];
foreach (var item in System.IO.File.ReadAllLines("unixdict.txt")) {
Line 831 ⟶ 828:
 
=={{header|C++}}==
<syntaxhighlight lang="cpp">#include <cstdlib>
#include <fstream>
#include <iostream>
Line 936 ⟶ 933:
 
=={{header|CLU}}==
<syntaxhighlight lang="clu">abc_word = proc (s: string) returns (bool)
a: int := string$indexc('a', s)
b: int := string$indexc('b', s)
Line 1,011 ⟶ 1,008:
 
=={{header|COBOL}}==
<syntaxhighlight lang="cobol"> IDENTIFICATION DIVISION.
PROGRAM-ID. ABC-WORDS.
Line 1,113 ⟶ 1,110:
{{libheader| System.IoUtils}}
{{Trans|C#}}
<syntaxhighlight lang=Delphi"delphi">
program ABC_words;
 
Line 1,170 ⟶ 1,167:
 
=={{header|Diego}}==
<syntaxhighlight lang="diego">add_ary({str},foundWords);
with_file()
()_read⟦{raw},unixdict.txt⟧_splitto(words,⟦\n⟧)
Line 1,181 ⟶ 1,178:
 
Alternatively...
<syntaxhighlight lang="diego">add_ary({str},foundWords);
with_file()
()_read⟦{raw},unixdict.txt⟧_splitto(words,⟦\n⟧)
Line 1,203 ⟶ 1,200:
 
=={{header|Draco}}==
<syntaxhighlight lang="draco">\util.g
 
proc nonrec abc_word(*char line) bool:
Line 1,286 ⟶ 1,283:
 
=={{header|Factor}}==
<syntaxhighlight lang="factor">USING: grouping io.encodings.ascii io.files kernel prettyprint
sequences sets ;
 
Line 1,309 ⟶ 1,306:
=={{header|Forth}}==
{{works with|Gforth}}
<syntaxhighlight lang="forth">: abc-word? ( addr u -- ? )
false false { a b }
0 do
Line 1,403 ⟶ 1,400:
 
=={{header|FreeBASIC}}==
<syntaxhighlight lang="freebasic">
#define NOTINSTRING 9999
 
Line 1,492 ⟶ 1,489:
 
=={{header|Go}}==
<syntaxhighlight lang="go">package main
 
import (
Line 1,582 ⟶ 1,579:
 
=={{header|Haskell}}==
<syntaxhighlight lang="haskell">import Data.List (elemIndex)
import Data.Maybe (isJust)
 
Line 1,666 ⟶ 1,663:
 
=={{header|Java}}==
<syntaxhighlight lang="java">import java.io.BufferedReader;
import java.io.FileReader;
 
Line 1,768 ⟶ 1,765:
 
=={{header|JavaScript}}==
<syntaxhighlight lang="javascript">(() => {
"use strict";
 
Line 1,921 ⟶ 1,918:
 
=={{header|J}}==
A word is an abc word if the order of the indices of 'a', 'b' and 'c' and the final letter of the word are unchanged by sorting. (The index of 'a' would be the length of the word -- one greater than the last index into the word -- if 'a' was missing from the word. So by including that last index in our list of indices to be sorted, we eliminate all words which are missing an 'a', 'b' or 'c'.)<syntaxhighlight lang=J"j"> >(#~ (-: /:~)@(<:@#,~i.&'abc')@>) cutLF tolower fread 'unixdict.txt'
aback
abacus
Line 1,981 ⟶ 1,978:
{{works with|jq}}
'''Works with gojq, the Go implementation of jq'''
<syntaxhighlight lang="jq">def is_abc_word:
[index("a", "b", "c")]
| all(.[]; . != null) and .[0] < .[1] and .[1] < .[2] ;
Line 2,000 ⟶ 1,997:
=={{header|Julia}}==
See [[Alternade_words#Julia]] for the foreachword function.
<syntaxhighlight lang="julia">function isabcword(w, _)
positions = [findfirst(c -> c == ch, w) for ch in "abc"]
return all(!isnothing, positions) && issorted(positions) ? w : ""
Line 2,023 ⟶ 2,020:
 
=={{header|Lua}}==
<syntaxhighlight lang=Lua"lua">for word in io.lines('unixdict.txt') do
if string.find(word, "^[^bc]*a[^c]*b.*c") then
print(word)
Line 2,030 ⟶ 2,027:
 
=={{header|Modula-2}}==
<syntaxhighlight lang="modula2">MODULE ABCWords;
IMPORT SeqIO;
IMPORT Texts;
Line 2,129 ⟶ 2,126:
 
=={{header|Nim}}==
<syntaxhighlight lang=Nim"nim">import strutils
 
func isAbcWord(word: string): bool =
Line 2,205 ⟶ 2,202:
=={{header|Perl}}==
Outputs same 55 words everyone else finds.
<syntaxhighlight lang="perl">#!/usr/bin/perl
 
@ARGV = 'unixdict.txt';
Line 2,211 ⟶ 2,208:
 
=={{header|Phix}}==
<!--<syntaxhighlight lang=Phix"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;">abc</span><span style="color: #0000FF;">(</span><span style="color: #004080;">string</span> <span style="color: #000000;">word</span><span style="color: #0000FF;">)</span>
Line 2,226 ⟶ 2,223:
 
=={{header|PL/I}}==
<syntaxhighlight lang="pli">abcWords: procedure options(main);
declare dict file;
open file(dict) title('unixdict.txt');
Line 2,265 ⟶ 2,262:
 
=={{header|Processing}}==
<syntaxhighlight lang="processing">String[] words;
 
void setup() {
Line 2,344 ⟶ 2,341:
Outputs the same 55 words as other examples when entered in a Posix terminal shell
 
<syntaxhighlight lang="python">python -c '
import sys
for ln in sys.stdin:
Line 2,354 ⟶ 2,351:
Or a functionally composed variant, with a predicate which takes a single recursive pass through the characters of each word:
 
<syntaxhighlight lang="python">'''ABC Words'''
 
 
Line 2,490 ⟶ 2,487:
Or using a regular expression.
 
<syntaxhighlight lang="python">import re
import textwrap
 
Line 2,516 ⟶ 2,513:
=={{header|Quackery}}==
 
<syntaxhighlight lang=Quackery"quackery"> [ true swap
behead swap
witheach
Line 2,556 ⟶ 2,553:
=={{header|Racket}}==
 
<syntaxhighlight lang="racket">#lang racket
 
(for ((i (in-naturals 1))
Line 2,587 ⟶ 2,584:
 
=={{header|Raku}}==
<syntaxhighlight lang=perl6"raku" line>put display 'unixdict.txt'.IO.words».fc.grep({ (.index('a')//next) < (.index('b')//next) < (.index('c')//next) }),
:11cols, :fmt('%-12s');
 
Line 2,607 ⟶ 2,604:
 
It also allows the &nbsp; (ABC) &nbsp; characters to be specified on the command line (CL) as well as the dictionary file identifier.
<syntaxhighlight lang="rexx">/*REXX program finds all the caseless alternade words (within an identified dictionary).*/
parse arg minL iFID . /*obtain optional arguments from the CL*/
if minL=='' | minL=="," then minL= 6 /*Not specified? Then use the default.*/
Line 2,704 ⟶ 2,701:
 
=={{header|Ring}}==
<syntaxhighlight lang="ring">
cStr = read("unixdict.txt")
wordList = str2list(cStr)
Line 2,785 ⟶ 2,782:
=={{header|Ruby}}==
translation from Perl
<syntaxhighlight lang="ruby">puts File.open("unixdict.txt").grep(/^[^bc]*a[^c]*b.*c/)
</syntaxhighlight>
{{out}}Same 55 words:
Line 2,810 ⟶ 2,807:
 
=={{header|Swift}}==
<syntaxhighlight lang="swift">import Foundation
 
func loadDictionary(_ path: String) throws -> [String] {
Line 2,915 ⟶ 2,912:
 
=={{header|Tcl}}==
<syntaxhighlight lang="tcl">proc is_abc_word word {
regexp {^[^bc]*a[^c]*b.*c} $word
}
Line 2,985 ⟶ 2,982:
 
=={{header|Transd}}==
<syntaxhighlight lang="scheme">#lang transd
 
MainModule : {
Line 3,006 ⟶ 3,003:
 
=={{header|Vlang}}==
<syntaxhighlight lang="vlang">import os
 
fn main() {
Line 3,082 ⟶ 3,079:
=={{header|Wren}}==
{{libheader|Wren-fmt}}
<syntaxhighlight lang="ecmascript">import "io" for File
import "/fmt" for Fmt
 
Line 3,160 ⟶ 3,157:
 
=={{header|XPL0}}==
<syntaxhighlight lang=XPL0"xpl0">string 0; \use zero-terminated strings
int I, J, K, Ch, Len;
char Word(100); \(longest word in unixdict.txt is 22 chars)
Line 3,249 ⟶ 3,246:
tablecloth
</pre>
{{omit from|6502 Assembly|unixdict.txt is much larger than the CPU's address space.}}
{{omit from|8080 Assembly|See 6502 Assembly.}}
{{omit from|Z80 Assembly|See 6502 Assembly.}}
10,327

edits