ABC words

From Rosetta Code
Revision as of 11:26, 5 December 2020 by PureFox (talk | contribs) (Added Wren)
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.

FreeBASIC

Assuming the words can contain the letters a, b, c more than once. It tracks them in the order of their first occurrence.

<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

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

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 >= 0 && c >= 0 && a < b && b < c) {
       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