Alternade words
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 length is 6 or greater, also showing both the words that make up the alternade.
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
Julia
<lang julia>function alternade(fname, minlength)
words = split(read(fname, String), r"\s+") dict = Dict(w => 1 for w in words) results = NTuple{3, String}[] for word in words len = length(word) (len < minlength) && continue letters = collect(word) oddword, evenword = Char[], Char[] for i in 1:2:len push!(oddword, letters[i]) i < len && push!(evenword, letters[i + 1]) end wordOdd, wordEven = String(oddword), String(evenword) if haskey(dict, wordOdd) && haskey(dict, wordEven) push!(results, (word, wordOdd, wordEven)) end end return results
end
const results = alternade("unixdict.txt", 6) println("Word source: unixdict.txt") println("A total of ", length(results), " words of at least length 6 spell words in alternate letters.") println(" word wordOdd wordEven\n", "-"^48) for (i, tup) in enumerate(results)
println(rpad("$i.", 4), rpad(tup[1], 20), rpad(tup[2], 10), rpad(tup[3], 10))
end
</lang>
- Output:
Word source: unixdict.txt A total of 58 words of at least length 6 spell words in alternate letters. word wordOdd wordEven ------------------------------------------------ 1. accost acs cot 2. accuse acs cue 3. afield ail fed 4. agleam ala gem 5. alcott act lot 6. allele all lee 7. allied ale lid 8. alpert apr let 9. ambient abet min 10. annette ante net 11. apport apr pot 12. ariadne aide ran 13. assist ass sit 14. battle btl ate 15. blaine ban lie 16. brenda bed rna 17. calliope clip aloe 18. choose cos hoe 19. choosy cos hoy 20. claire car lie 21. collude clue old 22. effete eft fee 23. fabric fbi arc 24. fealty fat ely 25. fluent fun let 26. forwent fret own 27. friend fin red 28. george gog ere 29. inroad ira nod 30. israel ire sal 31. jaunty jut any 32. joanne jan one 33. lounge lug one 34. oriole oil roe 35. oswald owl sad 36. parrot pro art 37. peoria poi era 38. pierre per ire 39. poodle pol ode 40. pounce puc one 41. racial rca ail 42. realty rat ely 43. sordid sri odd 44. spatial sail pta 45. sprain sri pan 46. strain sri tan 47. strait sri tat 48. sturdy sud try 49. sweaty set way 50. tattle ttl ate 51. theorem term hoe 52. though tog huh 53. throaty tray hot 54. triode tid roe 55. triune tin rue 56. troupe top rue 57. truant tun rat 58. twirly til wry
Raku
<lang perl6>unit sub MAIN ($file = 'unixdict.txt', :$min = 6);
my %words = $file.IO.slurp.words.map: * => 1;
my @alternades;
for %words {
next if .key.chars < $min; my @letters = .key.comb; my @alts = @letters[0,2 … *].join, @letters[1,3 … *].join; @alternades.push(.key => @alts) if %words{@alts[0]} && %words{@alts[1]};
}
@alternades.=sort;
say "{+@alternades} alternades longer than {$min-1} characters found in $file:";
.say for @alternades > 10
?? (flat @alternades.head(5), '...', @alternades.tail(5)) !! @alternades;</lang>
- Output:
58 alternades longer than 5 characters found in unixdict.txt: accost => [acs cot] accuse => [acs cue] afield => [ail fed] agleam => [ala gem] alcott => [act lot] ... 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...