ABC words

From Rosetta Code
Revision as of 16:37, 5 December 2020 by Thundergnat (talk | contribs) (→‎{{header|Raku}}: neaten up output)
ABC words is a draft programming task. It is not yet considered ready to be promoted as a complete task, for reasons that should be found in its talk page.
Definition

A word is an ABC word if the letters "a", "b" and "c" appear in it in alphabetical order.

If any or all of these letters occur more than once in a word, then only the first occurrence of each letter should be used to determine whether a word is an ABC word or not.

Task

Show here every ABC word in unixdict.txt.

ALGOL 68

<lang algol68># find words that have "a", "b" and "C" in order in them # IF FILE input file;

   STRING file name = "unixdict.txt";
   open( input file, file name, stand in channel ) /= 0

THEN

   # failed to open the file #
   print( ( "Unable to open """ + file name + """", newline ) )

ELSE

   # file opened OK #
   BOOL at eof := FALSE;
   # set the EOF handler for the file #
   on logical file end( input file, ( REF FILE f )BOOL:
                                    BEGIN
                                        # note that we reached EOF on the #
                                        # latest read #
                                        at eof := TRUE;
                                        # return TRUE so processing can continue #
                                        TRUE
                                    END
                      );
   INT abc count := 0;
   WHILE STRING word;
         get( input file, ( word, newline ) );
         NOT at eof
   DO
       IF  INT w pos := LWB word;
           INT w max  = UPB word;
           INT a pos := w max + 1;
           INT b pos := w max + 1;
           INT c pos := w max + 1;
           char in string( "a", a pos, word );
           char in string( "b", b pos, word );
           char in string( "c", c pos, word );
           a pos <  b pos
       AND b pos <  c pos
       AND c pos <= w max
       THEN
           abc count +:= 1;
           print( ( whole( abc count, -5 ), ": ", word, newline ) )
       FI
   OD;
   close( input file )

FI</lang>

Output:
    1: aback
    2: abacus
    3: abc
    4: abdicate
    5: abduct
    6: abeyance
    7: abject
    8: abreact
    9: abscess
   10: abscissa
   11: abscissae
   12: absence
   13: abstract
   14: abstracter
   15: abstractor
   16: adiabatic
   17: aerobacter
   18: aerobic
   19: albacore
   20: alberich
   21: albrecht
   22: algebraic
   23: alphabetic
   24: ambiance
   25: ambuscade
   26: aminobenzoic
   27: anaerobic
   28: arabic
   29: athabascan
   30: auerbach
   31: diabetic
   32: diabolic
   33: drawback
   34: fabric
   35: fabricate
   36: flashback
   37: halfback
   38: iambic
   39: lampblack
   40: leatherback
   41: metabolic
   42: nabisco
   43: paperback
   44: parabolic
   45: playback
   46: prefabricate
   47: quarterback
   48: razorback
   49: roadblock
   50: sabbatical
   51: snapback
   52: strabismic
   53: syllabic
   54: tabernacle
   55: tablecloth

FreeBASIC

<lang freebasic>

  1. define NOTINSTRING 9999

function first_occ( s as string, letter as string ) as uinteger

   for i as ubyte = 1 to len(s)
       if mid(s,i,1) = letter then return i
   next i
   return NOTINSTRING - asc(letter)

end function

function is_abc( s as string ) as boolean

   if first_occ( s, "a" ) > first_occ( s, "b" ) then return false
   if first_occ( s, "b" ) > first_occ( s, "c" ) then return false
   if first_occ( s, "c" ) > len(s) then return false
   return true

end function

dim as string word dim as uinteger c = 0

open "unixdict.txt" for input as #1 while true

   line input #1, word
   if word="" then exit while
   if is_abc( word ) then
       c+=1
       print c;".   ";word
   end if

wend close #1</lang>

Output:
1.   aback
2.   abacus
3.   abc
4.   abdicate
5.   abduct
6.   abeyance
7.   abject
8.   abreact
9.   abscess
10.   abscissa
11.   abscissae
12.   absence
13.   abstract
14.   abstracter
15.   abstractor
16.   adiabatic
17.   aerobacter
18.   aerobic
19.   albacore
20.   alberich
21.   albrecht
22.   algebraic
23.   alphabetic
24.   ambiance
25.   ambuscade
26.   aminobenzoic
27.   anaerobic
28.   arabic
29.   athabascan
30.   auerbach
31.   diabetic
32.   diabolic
33.   drawback
34.   fabric
35.   fabricate
36.   flashback
37.   halfback
38.   iambic
39.   lampblack
40.   leatherback
41.   metabolic
42.   nabisco
43.   paperback
44.   parabolic
45.   playback
46.   prefabricate
47.   quarterback
48.   razorback
49.   roadblock
50.   sabbatical
51.   snapback
52.   strabismic
53.   syllabic
54.   tabernacle
55.   tablecloth

Go

<lang go>package main

import (

   "bytes"
   "fmt"
   "io/ioutil"
   "log"

)

func main() {

   wordList := "unixdict.txt"
   b, err := ioutil.ReadFile(wordList)
   if err != nil {
       log.Fatal("Error reading file")
   }
   bwords := bytes.Fields(b)
   count := 0
   fmt.Println("Based on first occurrences only, the ABC words in", wordList, "are:")
   for _, bword := range bwords {
       a := bytes.IndexRune(bword, 'a')
       b := bytes.IndexRune(bword, 'b')
       c := bytes.IndexRune(bword, 'c')
       if a >= 0 && b > a && c > b {
           count++
           fmt.Printf("%2d: %s\n", count, string(bword))
       }
   }

}</lang>

Output:
Based on first occurrences only, the ABC words in unixdict.txt are:
 1: aback
 2: abacus
 3: abc
 4: abdicate
 5: abduct
 6: abeyance
 7: abject
 8: abreact
 9: abscess
10: abscissa
11: abscissae
12: absence
13: abstract
14: abstracter
15: abstractor
16: adiabatic
17: aerobacter
18: aerobic
19: albacore
20: alberich
21: albrecht
22: algebraic
23: alphabetic
24: ambiance
25: ambuscade
26: aminobenzoic
27: anaerobic
28: arabic
29: athabascan
30: auerbach
31: diabetic
32: diabolic
33: drawback
34: fabric
35: fabricate
36: flashback
37: halfback
38: iambic
39: lampblack
40: leatherback
41: metabolic
42: nabisco
43: paperback
44: parabolic
45: playback
46: prefabricate
47: quarterback
48: razorback
49: roadblock
50: sabbatical
51: snapback
52: strabismic
53: syllabic
54: tabernacle
55: tablecloth

Julia

<lang julia>function lettersinorder(dictfile, letters)

   chars = sort(collect(letters))
   for word in split(read(dictfile, String))
       positions = [findfirst(c -> c == ch, word) for ch in chars]
       all(!isnothing, positions) && issorted(positions) && println(word)
   end

end

lettersinorder("unixdict.txt", "abc")

</lang>

Output:
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

Phix

<lang Phix>function abc(string word)

   sequence idii = apply(true,find,{"abc",{word}})
   return find(0,idii)==0 and idii==sort(idii)

end function sequence words = filter(split_any(get_text("demo/unixdict.txt")," \r\n",no_empty:=true),abc) printf(1,"%d abc words found: %s\n",{length(words),join(shorten(words,"",3),", ")})</lang>

Output:
55 abc words found: aback, abacus, abc, ..., syllabic, tabernacle, tablecloth

Raku

<lang perl6>put 'unixdict.txt'.IO.words».fc.grep({ (.index('a')//next) < (.index('b')//next) < (.index('c')//next) })\

   .&{"{+$_} words:\n  " ~ .batch(11)».fmt('%-12s').join: "\n  "};</lang>
Output:
55 words:
  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 

Ring

<lang ring> cStr = read("unixdict.txt") wordList = str2list(cStr) num = 0

see "ABC words are:" + nl

for n = 1 to len(wordList)

   bool1 = substr(wordList[n],"a")
   bool2 = substr(wordList[n],"b")
   bool3 = substr(wordList[n],"c")
   bool4 = bool1 > 0 and bool2 > 0 and bool3 > 0
   bool5 = bool2 > bool1 and bool3 > bool2
   if bool4 = 1 and bool5 = 1
      num = num + 1
      see "" + num + ". " + wordList[n] + nl
   ok

next </lang> Output:

ABC words are:
1. aback
2. abacus
3. abc
4. abdicate
5. abduct
6. abeyance
7. abject
8. abreact
9. abscess
10. abscissa
11. abscissae
12. absence
13. abstract
14. abstracter
15. abstractor
16. adiabatic
17. aerobacter
18. aerobic
19. albacore
20. alberich
21. albrecht
22. algebraic
23. alphabetic
24. ambiance
25. ambuscade
26. aminobenzoic
27. anaerobic
28. arabic
29. athabascan
30. auerbach
31. diabetic
32. diabolic
33. drawback
34. fabric
35. fabricate
36. flashback
37. halfback
38. iambic
39. lampblack
40. leatherback
41. metabolic
42. nabisco
43. paperback
44. parabolic
45. playback
46. prefabricate
47. quarterback
48. razorback
49. roadblock
50. sabbatical
51. snapback
52. strabismic
53. syllabic
54. tabernacle
55. tablecloth

Wren

Library: Wren-fmt

<lang ecmascript>import "io" for File import "/fmt" for Fmt

var wordList = "unixdict.txt" // local copy var words = File.read(wordList).trimEnd().split("\n") var count = 0 System.print("Based on first occurrences only, the ABC words in %(wordList) are:") for (word in words) {

   var a = word.indexOf("a")
   var b = word.indexOf("b")
   var c = word.indexOf("c")
   if (a >= 0 && b > a && c > b) {
       count = count + 1
       Fmt.print("$2d: $s", count, word)
   }

}</lang>

Output:
Based on first occurrences only, the ABC words in unixdict.txt are:
 1: aback
 2: abacus
 3: abc
 4: abdicate
 5: abduct
 6: abeyance
 7: abject
 8: abreact
 9: abscess
10: abscissa
11: abscissae
12: absence
13: abstract
14: abstracter
15: abstractor
16: adiabatic
17: aerobacter
18: aerobic
19: albacore
20: alberich
21: albrecht
22: algebraic
23: alphabetic
24: ambiance
25: ambuscade
26: aminobenzoic
27: anaerobic
28: arabic
29: athabascan
30: auerbach
31: diabetic
32: diabolic
33: drawback
34: fabric
35: fabricate
36: flashback
37: halfback
38: iambic
39: lampblack
40: leatherback
41: metabolic
42: nabisco
43: paperback
44: parabolic
45: playback
46: prefabricate
47: quarterback
48: razorback
49: roadblock
50: sabbatical
51: snapback
52: strabismic
53: syllabic
54: tabernacle
55: tablecloth