Alternade words: Difference between revisions

From Rosetta Code
Content added Content deleted
(alter task description to match Ring entry and cut down on output)
(Add Factor)
Line 8: Line 8:
;See also:
;See also:
:* '''https://en.wikipedia.org/wiki/Alternade'''
:* '''https://en.wikipedia.org/wiki/Alternade'''

=={{header|Factor}}==
<lang factor>USING: formatting io.encodings.ascii io.files kernel literals
math sequences sequences.extras sets strings ;

<< CONSTANT: words $[ "unixdict.txt" ascii file-lines ] >>

CONSTANT: wordset $[ words HS{ } set-like ]

: word? ( str -- ? ) wordset in? ;

: subwords ( str -- str str )
dup <evens> >string swap <odds> >string ;

: alternade? ( str -- ? ) subwords [ word? ] both? ;

words
[ alternade? ] filter
[ length 5 > ] filter
[ dup subwords "%-8s %-4s %-4s\n" printf ] each</lang>
{{out}}
<pre>
accost acs cot
accuse acs cue
afield ail fed
agleam ala gem
alcott act lot
allele all lee
allied ale lid
alpert apr let
ambient abet min
annette ante net
apport apr pot
ariadne aide ran
assist ass sit
battle btl ate
blaine ban lie
brenda bed rna
calliope clip aloe
choose cos hoe
choosy cos hoy
claire car lie
collude clue old
effete eft fee
fabric fbi arc
fealty fat ely
fluent fun let
forwent fret own
friend fin red
george gog ere
inroad ira nod
israel ire sal
jaunty jut any
joanne jan one
lounge lug one
oriole oil roe
oswald owl sad
parrot pro art
peoria poi era
pierre per ire
poodle pol ode
pounce puc one
racial rca ail
realty rat ely
sordid sri odd
spatial sail pta
sprain sri pan
strain sri tan
strait sri tat
sturdy sud try
sweaty set way
tattle ttl ate
theorem term hoe
though tog huh
throaty tray hot
triode tid roe
triune tin rue
troupe top rue
truant tun rat
twirly til wry
</pre>


=={{header|Ring}}==
=={{header|Ring}}==

Revision as of 10:15, 29 November 2020

Alternade 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.

An alternade is a word whose alternating letters themselves form two words. For example, the word lounge contains the word lug (lounge) and the word one (lounge). For a word to be an alternade, all its letters must be used. The two words that form an alternade don't need to be the same length; for example, the word board is an alternade made up of the words bad (board) and or (board).

Task

Print out every alternade in unixdict.txt whose sub-words have length 3 or greater, also showing both the words that make up the alternade.

See also

Factor

<lang factor>USING: formatting io.encodings.ascii io.files kernel literals math sequences sequences.extras sets strings ;

<< CONSTANT: words $[ "unixdict.txt" ascii file-lines ] >>

CONSTANT: wordset $[ words HS{ } set-like ]

word? ( str -- ? ) wordset in? ;
subwords ( str -- str str )
   dup <evens> >string swap <odds> >string ;
alternade? ( str -- ? ) subwords [ word? ] both? ;

words [ alternade? ] filter [ length 5 > ] filter [ dup subwords "%-8s %-4s %-4s\n" printf ] each</lang>

Output:
accost   acs  cot 
accuse   acs  cue 
afield   ail  fed 
agleam   ala  gem 
alcott   act  lot 
allele   all  lee 
allied   ale  lid 
alpert   apr  let 
ambient  abet min 
annette  ante net 
apport   apr  pot 
ariadne  aide ran 
assist   ass  sit 
battle   btl  ate 
blaine   ban  lie 
brenda   bed  rna 
calliope clip aloe
choose   cos  hoe 
choosy   cos  hoy 
claire   car  lie 
collude  clue old 
effete   eft  fee 
fabric   fbi  arc 
fealty   fat  ely 
fluent   fun  let 
forwent  fret own 
friend   fin  red 
george   gog  ere 
inroad   ira  nod 
israel   ire  sal 
jaunty   jut  any 
joanne   jan  one 
lounge   lug  one 
oriole   oil  roe 
oswald   owl  sad 
parrot   pro  art 
peoria   poi  era 
pierre   per  ire 
poodle   pol  ode 
pounce   puc  one 
racial   rca  ail 
realty   rat  ely 
sordid   sri  odd 
spatial  sail pta 
sprain   sri  pan 
strain   sri  tan 
strait   sri  tat 
sturdy   sud  try 
sweaty   set  way 
tattle   ttl  ate 
theorem  term hoe 
though   tog  huh 
throaty  tray hot 
triode   tid  roe 
triune   tin  rue 
troupe   top  rue 
truant   tun  rat 
twirly   til  wry 

Ring

<lang ring> load "stdlib.ring"

cStr = read("unixdict.txt") wordList = str2list(cStr) sum = 0 see "working..." + nl + nl

for n = 1 to len(wordList)

   wordOdd = ""
   wordEven = ""
   for m = 1 to len(wordList[n]) step 2
       wordOdd = wordOdd + wordList[n][m] 
   next
   for m = 2 to len(wordList[n]) step 2
       wordEven = wordEven + wordList[n][m] 
   next
   indOdd = find(wordList,wordOdd)
   indEven = find(wordList,wordEven)
   if indOdd > 0 and indEven > 0 and len(wordList[indOdd]) > 2 and len(wordList[indEven]) > 2
      sum = sum + 1 
      see "" + sum + ". "
      see "word = " + wordList[n] + nl
      see "wordOdd = " + wordList[indOdd] + nl
      see "wordEven = " + wordList[indEven] + nl + nl
   ok

next see "done..." + nl </lang> Output:

working...

1. word = accost
wordOdd = acs
wordEven = cot

2. word = accuse
wordOdd = acs
wordEven = cue

3. word = afield
wordOdd = ail
wordEven = fed

4. word = agleam
wordOdd = ala
wordEven = gem

5. word = alcott
wordOdd = act
wordEven = lot

6. word = allele
wordOdd = all
wordEven = lee

7. word = allied
wordOdd = ale
wordEven = lid

8. word = alpert
wordOdd = apr
wordEven = let

9. word = ambient
wordOdd = abet
wordEven = min

10. word = annette
wordOdd = ante
wordEven = net

11. word = apport
wordOdd = apr
wordEven = pot

12. word = ariadne
wordOdd = aide
wordEven = ran

13. word = assist
wordOdd = ass
wordEven = sit

14. word = battle
wordOdd = btl
wordEven = ate

15. word = blaine
wordOdd = ban
wordEven = lie

16. word = brenda
wordOdd = bed
wordEven = rna

17. word = calliope
wordOdd = clip
wordEven = aloe

18. word = choose
wordOdd = cos
wordEven = hoe

19. word = choosy
wordOdd = cos
wordEven = hoy

20. word = claire
wordOdd = car
wordEven = lie

21. word = collude
wordOdd = clue
wordEven = old

22. word = effete
wordOdd = eft
wordEven = fee

23. word = fabric
wordOdd = fbi
wordEven = arc

24. word = fealty
wordOdd = fat
wordEven = ely

25. word = fluent
wordOdd = fun
wordEven = let

26. word = forwent
wordOdd = fret
wordEven = own

27. word = friend
wordOdd = fin
wordEven = red

28. word = george
wordOdd = gog
wordEven = ere

29. word = inroad
wordOdd = ira
wordEven = nod

30. word = israel
wordOdd = ire
wordEven = sal

31. word = jaunty
wordOdd = jut
wordEven = any

32. word = joanne
wordOdd = jan
wordEven = one

33. word = lounge
wordOdd = lug
wordEven = one

34. word = oriole
wordOdd = oil
wordEven = roe

35. word = oswald
wordOdd = owl
wordEven = sad

36. word = parrot
wordOdd = pro
wordEven = art

37. word = peoria
wordOdd = poi
wordEven = era

38. word = pierre
wordOdd = per
wordEven = ire

39. word = poodle
wordOdd = pol
wordEven = ode

40. word = pounce
wordOdd = puc
wordEven = one

41. word = racial
wordOdd = rca
wordEven = ail

42. word = realty
wordOdd = rat
wordEven = ely

43. word = sordid
wordOdd = sri
wordEven = odd

44. word = spatial
wordOdd = sail
wordEven = pta

45. word = sprain
wordOdd = sri
wordEven = pan

46. word = strain
wordOdd = sri
wordEven = tan

47. word = strait
wordOdd = sri
wordEven = tat

48. word = sturdy
wordOdd = sud
wordEven = try

49. word = sweaty
wordOdd = set
wordEven = way

50. word = tattle
wordOdd = ttl
wordEven = ate

51. word = theorem
wordOdd = term
wordEven = hoe

52. word = though
wordOdd = tog
wordEven = huh

53. word = throaty
wordOdd = tray
wordEven = hot

54. word = triode
wordOdd = tid
wordEven = roe

55. word = triune
wordOdd = tin
wordEven = rue

56. word = troupe
wordOdd = top
wordEven = rue

57. word = truant
wordOdd = tun
wordEven = rat

58. word = twirly
wordOdd = til
wordEven = wry

done...