Odd words

From Rosetta Code
Revision as of 14:48, 5 December 2020 by Petelomax (talk | contribs) (Undo revision 317723 by Petelomax (talk))
Odd 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.

Given a list of words. (unixdict.txt)
Take odd indices letters from the word and if it is in the list then display on this page.
The length of new word > 4

FreeBASIC

<lang freebasic>#define NULL 0

type node

   word as string*32   'enough space to store any word in the dictionary
   nxt as node ptr

end type

function addword( tail as node ptr, word as string ) as node ptr

   'allocates memory for a new node, links the previous tail to it,
   'and returns the address of the new node
   dim as node ptr newnode = allocate(sizeof(node))
   tail->nxt = newnode
   newnode->nxt = NULL
   newnode->word = word
   return newnode

end function

function crunch( word as string ) as string

   dim as string ret = ""
   for i as uinteger = 1 to len(word) step 2
       ret += mid(word,i,1)
   next i
   return ret

end function

function length( word as string ) as uinteger

   'necessary replacement for the built-in len function, which in this
   'case would always return 32
   for i as uinteger = 1 to 32
       if asc(mid(word,i,1)) = 0 then return i-1
   next i
   return 999

end function

dim as string word dim as node ptr tail = allocate( sizeof(node) ) dim as node ptr head = tail, curr = head, currj tail->nxt = NULL tail->word = "XXXXHEADER"

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

   line input #1, word
   if word = "" then exit while
   tail = addword( tail, word )

wend close #1

while curr->nxt <> NULL

   if length(curr->word) > 6 then word = crunch( curr->word ) else goto nextword
   currj = head
   while currj->nxt <> NULL
       if word = currj->word then print left(curr->word,length(curr->word));"   --->   ";word
       currj = currj->nxt
   wend
   nextword:
   curr = curr->nxt

wend</lang>

Output:
This example is incorrect. Please fix the code and remove this message.

Details: "over 4" aka "length 5 or more"

There are 210 solutions. I will give the first and last ten.

ablution   --->   alto
agnostic   --->   ansi
allegra   --->   alga
allusion   --->   also
alluvial   --->   alva
ambient   --->   abet
ammerman   --->   amra
annette   --->   ante
ariadne   --->   aide
ballast   --->   blat
<SNIP>
twiddle   --->   tide
twombly   --->   toby
weekend   --->   weed
whatnot   --->   want
where'd   --->   weed
whether   --->   wehr
whipple   --->   wipe
whitney   --->   winy
wrangle   --->   wage
wrigley   --->   wily

Phix

<lang Phix>sequence words = split_any(get_text("demo/unixdict.txt")," \r\n") function odd(integer /*ch*/, idx) return remainder(idx,2)=1 end function function oddch(string word) return filter(word,odd) end function function over4(string word) return length(word)>4 end function words = filter(filter(apply(words,oddch),over4),"in",words) printf(1,"%d odd words found: %s\n",{length(words),join(shorten(words,"",3),", ")})</lang>

Output:
14 odd words found: brain, cider, cried, ..., spree, spree, trial

Ring

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

see "Odd words are:" + nl

for n = 1 to len(wordList)

   strWord = ""
   len = len(wordList[n])
   for m = 1 to len step 2
       strWord = strWord + wordList[n][m]
   next
   ind = find(wordList,strWord) 
   if ind > 0  and len(strWord) > 4
      num = num + 1
      see "" + num + ". " + wordList[n] + " >> " + strWord + nl
   ok

next </lang> Output:

Odd words are:
1. barbarian >> brain
2. childbear >> cider
3. corrigenda >> cried
4. gargantuan >> grata
5. headdress >> hades
6. palladian >> plain
7. propionate >> point
8. salvation >> slain
9. siltation >> slain
10. slingshot >> sight
11. statuette >> saute
12. supersede >> spree
13. supervene >> spree
14. terminable >> trial