Author Topic: Word ladder - Rosetta Code  (Read 21193 times)

0 Members and 1 Guest are viewing this topic.

Offline Jaze

  • Newbie
  • Posts: 86
    • View Profile
Re: Word ladder - Rosetta Code
« Reply #15 on: September 08, 2021, 04:59:48 pm »
Where can I find the "unixdict.txt" file?

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Word ladder - Rosetta Code
« Reply #16 on: September 08, 2021, 05:03:04 pm »
It is attached to the first post in this thread.

Offline Jaze

  • Newbie
  • Posts: 86
    • View Profile
Re: Word ladder - Rosetta Code
« Reply #17 on: September 08, 2021, 07:50:23 pm »
I skipped by it twice. Finally found it

Offline david_uwi

  • Newbie
  • Posts: 71
    • View Profile
Re: Word ladder - Rosetta Code
« Reply #18 on: September 09, 2021, 04:32:28 am »
I've removed the duplicates in each list. I have a feeling that I could be accidentally filtering out some genuine ones - I need to check this!
anyway job-->fun 0.33 sec (found 8 solutions - seems to have missed fun, bun,bub,bob, job)
girl-->lady 4.9 sec and the word list goes up as follows 2,18,71,278,756,1394,1716 and comes up with the same sequence as you got. So I'm getting there!

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Word ladder - Rosetta Code
« Reply #19 on: September 09, 2021, 11:45:37 am »
@david_uwi

girl > lady in 4.9 is looking really good, roughly .1 * my time @46.5 secs.
Could you get from alien > drool?

I've exhausted variations on code from first thread, I am going to try a different approach.

Offline david_uwi

  • Newbie
  • Posts: 71
    • View Profile
Re: Word ladder - Rosetta Code
« Reply #20 on: September 09, 2021, 02:32:20 pm »
Yes I got alien --> drool in 49 sec. I'm not sure I've done anything clever, maybe I just hit on a good approach.

At the moment I am trying to work out how to prove that child-->adult is not possible.
I was thinking that the chain of words, when it gets long enough, must start to repeat and I've attempted to write a routine that will look for that. Problem is that I'm up to chains 100 words long and there still seems to be 1000 word chains without repeats.
Maybe I've programmed it wrong!

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Word ladder - Rosetta Code
« Reply #21 on: September 09, 2021, 08:11:32 pm »
Yes I got alien --> drool in 49 sec. I'm not sure I've done anything clever, maybe I just hit on a good approach.

At the moment I am trying to work out how to prove that child-->adult is not possible.
I was thinking that the chain of words, when it gets long enough, must start to repeat and I've attempted to write a routine that will look for that. Problem is that I'm up to chains 100 words long and there still seems to be 1000 word chains without repeats.
Maybe I've programmed it wrong!

alien to drool in 49 secs is great! You've certainly programmed something right!

My code is done eg for child to adult when it runs out of connections in my TheList array which is not so different from a stack except I work it off from the front end instead of the back. TheList() array only grows but TheList position index only goes up in looking for connections.

Offline david_uwi

  • Newbie
  • Posts: 71
    • View Profile
Re: Word ladder - Rosetta Code
« Reply #22 on: September 10, 2021, 03:00:10 am »
The solution just suddenly came to me last evening (while taking a beer!).

Once a word has been used it can be eliminated from all future consideration.
With that insight alien-->drool takes 6 seconds and no path child-->adult is also found in 6 seconds.

Here's the code (it's a bit messy and I've only bothered with the 5 letter words).

Code: QB64: [Select]
  1. OPEN "c:\cw1\unixdict.txt" FOR INPUT AS #1
  2. 'OPEN "c:\cw1\english3.txt" FOR INPUT AS #1 'bigger dictionary
  3. DIM w(10000) AS STRING * 5 'make sure we have enough storage!!
  4. DIM q4(10000, 100) AS STRING * 5
  5. DIM k1(100)
  6. DIM z4(10000, 100) AS STRING
  7. tt = TIMER 'include time taken to load to RAM
  8.  
  9. q1$ = "alien": q2$ = "drool"
  10. 'q1$ = "child": q2$ = "adult"
  11.  
  12.     INPUT #1, a$
  13.     IF LEN(a$) = 5 THEN
  14.         k = k + 1
  15.         w(k) = a$
  16.         IF a$ = q1$ THEN w(k) = "*****"
  17.     END IF
  18. 'tt = TIMER
  19. jk = 1
  20. k1(jk) = 1
  21. q4(1, 1) = q1$
  22.     FOR i = 1 TO k
  23.         cnt = 0
  24.         FOR kk = 1 TO k1(jk)
  25.             cnt = 0
  26.             FOR j = 1 TO 5
  27.                 IF MID$(w(i), j, 1) = MID$(q4(kk, jk), j, 1) THEN cnt = cnt + 1 ELSE zz = j
  28.             NEXT j
  29.             IF cnt = 4 THEN
  30.                 k1(jk + 1) = k1(jk + 1) + 1
  31.                 q4(k1(jk + 1), jk + 1) = w(i)
  32.                 z4(k1(jk + 1), jk + 1) = z4(kk, jk) + MID$(w(i), zz, 1) + CHR$(zz + 48) + " "
  33.                 w(i) = "*****"
  34.             END IF
  35.        20 NEXT kk
  36.     NEXT i
  37.     kflag = 0
  38.     FOR i = 1 TO k1(jk + 1)
  39.         IF q4(i, jk + 1) = q2$ THEN kflag = 99: final$ = z4(i, jk + 1)
  40.     NEXT i
  41.     PRINT
  42.     PRINT jk; k1(jk + 1)
  43.     IF k1(jk + 1) = 0 THEN kflag = 99
  44.     jk = jk + 1
  45.     IF kflag = 99 THEN EXIT DO
  46. IF k1(jk) = 0 THEN PRINT: PRINT "No path found!"
  47. IF k1(jk) > 0 THEN
  48.     xlen = LEN(final$)
  49.     PRINT: PRINT q1$; " ";
  50.     FOR i = 1 TO xlen STEP 3
  51.         c1$ = MID$(final$, i, 1)
  52.         c2$ = MID$(final$, i + 1, 1)
  53.         MID$(q1$, VAL(c2$), 1) = c1$
  54.         PRINT q1$; " ";
  55.     NEXT i
  56. PRINT: PRINT "time taken = "; TIMER - tt; " seconds"

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Word ladder - Rosetta Code
« Reply #23 on: September 10, 2021, 11:04:54 am »
@david_uwi Really nice time:
 
Word ladder (RC) v david_uwi.PNG


Why is the _Title I gave it not showing???
Code: QB64: [Select]
  1. _Title "Word ladder - RC v david_uwi"
  2. ' 2021-09-10 ref https://www.qb64.org/forum/index.php?topic=4157.msg135285#msg135285
  3. Open "unixdict.txt" For Input As #1 ' bplus mod path
  4. 'OPEN "c:\cw1\english3.txt" FOR INPUT AS #1 'bigger dictionary
  5. Dim w(10000) As String * 5 'make sure we have enough storage!!
  6. Dim q4(10000, 100) As String * 5
  7. Dim k1(100)
  8. Dim z4(10000, 100) As String
  9. tt = Timer 'include time taken to load to RAM
  10.  
  11. q1$ = "alien": q2$ = "drool" '  6.70.. secs on bplus system
  12. 'q1$ = "child": q2$ = "adult" ' 6.89.. secs on bplus system
  13.  
  14.     Input #1, a$
  15.     If Len(a$) = 5 Then
  16.         k = k + 1
  17.         w(k) = a$
  18.         If a$ = q1$ Then w(k) = "*****"
  19.     End If
  20. 'tt = TIMER
  21. jk = 1
  22. k1(jk) = 1
  23. q4(1, 1) = q1$
  24.     For i = 1 To k
  25.         cnt = 0
  26.         For kk = 1 To k1(jk)
  27.             cnt = 0
  28.             For j = 1 To 5
  29.                 If Mid$(w(i), j, 1) = Mid$(q4(kk, jk), j, 1) Then cnt = cnt + 1 Else zz = j
  30.             Next j
  31.             If cnt = 4 Then
  32.                 k1(jk + 1) = k1(jk + 1) + 1
  33.                 q4(k1(jk + 1), jk + 1) = w(i)
  34.                 z4(k1(jk + 1), jk + 1) = z4(kk, jk) + Mid$(w(i), zz, 1) + Chr$(zz + 48) + " "
  35.                 w(i) = "*****"
  36.             End If
  37.        20 Next kk
  38.     Next i
  39.     kflag = 0
  40.     For i = 1 To k1(jk + 1)
  41.         If q4(i, jk + 1) = q2$ Then kflag = 99: final$ = z4(i, jk + 1)
  42.     Next i
  43.     Print
  44.     Print jk; k1(jk + 1)
  45.     If k1(jk + 1) = 0 Then kflag = 99
  46.     jk = jk + 1
  47.     If kflag = 99 Then Exit Do
  48. If k1(jk) = 0 Then Print: Print "No path found!"
  49. If k1(jk) > 0 Then
  50.     xlen = Len(final$)
  51.     Print: Print q1$; " ";
  52.     For i = 1 To xlen Step 3
  53.         c1$ = Mid$(final$, i, 1)
  54.         c2$ = Mid$(final$, i + 1, 1)
  55.         Mid$(q1$, Val(c2$), 1) = c1$
  56.         Print q1$; " ";
  57.     Next i
  58. Print: Print "time taken = "; Timer - tt; " seconds"
  59.  
  60.  
« Last Edit: September 10, 2021, 11:06:09 am by bplus »

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Word ladder - Rosetta Code
« Reply #24 on: September 10, 2021, 11:26:02 am »
Weird! Is anyone getting a title for above code?

I was wondering if Windows changed it's rules but _Title is still working in my other Word ladder codes.
« Last Edit: September 10, 2021, 11:27:31 am by bplus »

FellippeHeitor

  • Guest
Re: Word ladder - Rosetta Code
« Reply #25 on: September 10, 2021, 11:32:43 am »
Setting _TITLE may fail if the command is run before the window has time to be created. As per the wiki, add this to the beginning of your program:

Code: QB64: [Select]

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Word ladder - Rosetta Code
« Reply #26 on: September 10, 2021, 11:44:39 am »
Thanks @FellippeHeitor  so another race condition with screen load like moving it to _Middle.

Offline david_uwi

  • Newbie
  • Posts: 71
    • View Profile
Re: Word ladder - Rosetta Code
« Reply #27 on: September 10, 2021, 11:52:06 am »
Should you want to do shorter words here is a bodge.
I've also shaved about a second off the time and as a bit of variety I am searching from Z to A.
I notice that on the "Rosetta Code page" there are no execution times given.
As always it is about the algorithm being efficient and nothing to do with the programming language (despite what the C++ bores would like you to believe).

Code: QB64: [Select]
  1. OPEN "c:\cw1\unixdict.txt" FOR INPUT AS #1
  2. 'OPEN "c:\cw1\english3.txt" FOR INPUT AS #1 'bigger dictionary
  3. DIM w(10000) AS STRING * 5 'make sure we have enough storage!!
  4. DIM q4(10000, 100) AS STRING * 5
  5. DIM k1(100)
  6. DIM z4(10000, 100) AS STRING
  7. tt = TIMER 'include time taken to load to RAM
  8.  
  9. q1$ = "alien": q2$ = "drool"
  10. 'q1$ = "child": q2$ = "adult"
  11. 'q1$ = "girl": q2$ = "lady"
  12. 'q1$ = "john": q2$ = "jane"
  13. 'q1$ = "play": q2$ = "ball"
  14. n = LEN(q1$)
  15. IF n < 5 THEN q1$ = q1$ + SPACE$(5 - n): q2$ = q2$ + SPACE$(5 - n)
  16.     INPUT #1, a$
  17.     IF LEN(a$) = n THEN
  18.         k = k + 1
  19.         w(k) = a$
  20.         IF a$ = q1$ THEN w(k) = "*****"
  21.     END IF
  22. 'tt = TIMER
  23. jk = 1
  24. k1(jk) = 1
  25. q4(1, 1) = q1$
  26.     FOR i = k TO 1 STEP -1
  27.         IF w(i) = "*****" THEN 500
  28.         cnt = 0
  29.         FOR kk = 1 TO k1(jk)
  30.             cnt = 0
  31.  
  32.             FOR j = 1 TO n
  33.                 IF MID$(w(i), j, 1) = MID$(q4(kk, jk), j, 1) THEN cnt = cnt + 1 ELSE zz = j
  34.             NEXT j
  35.             IF cnt = n - 1 THEN
  36.                 k1(jk + 1) = k1(jk + 1) + 1
  37.                 q4(k1(jk + 1), jk + 1) = w(i)
  38.                 z4(k1(jk + 1), jk + 1) = z4(kk, jk) + MID$(w(i), zz, 1) + CHR$(zz + 48) + " "
  39.                 w(i) = "*****"
  40.             END IF
  41.        20 NEXT kk
  42.    500 NEXT i
  43.     kflag = 0
  44.     FOR i = 1 TO k1(jk + 1)
  45.         IF q4(i, jk + 1) = q2$ THEN kflag = 99: final$ = z4(i, jk + 1)
  46.     NEXT i
  47.     PRINT
  48.     PRINT jk; k1(jk + 1)
  49.     IF k1(jk + 1) = 0 THEN kflag = 99
  50.     jk = jk + 1
  51.     IF kflag = 99 THEN EXIT DO
  52. IF k1(jk) = 0 THEN PRINT: PRINT "No path found!"
  53. IF k1(jk) > 0 THEN
  54.     xlen = LEN(final$)
  55.     PRINT: PRINT q1$; " ";
  56.     FOR i = 1 TO xlen STEP 3
  57.         c1$ = MID$(final$, i, 1)
  58.         c2$ = MID$(final$, i + 1, 1)
  59.         MID$(q1$, VAL(c2$), 1) = c1$
  60.         PRINT q1$; " ";
  61.     NEXT i
  62. PRINT: PRINT "time taken = "; TIMER - tt; " seconds"

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Word ladder - Rosetta Code
« Reply #28 on: September 10, 2021, 12:41:08 pm »
running my tests
Quote
'                                                     2nd times with 20 removed from my line 49 here
q1$ = "alien": q2$ = "drool" ' 5.21... bplus system  5.11..
'q1$ = "child": q2$ = "adult" ' 5.21... bplus system  4.94..
'q1$ = "girl": q2$ = "lady" '   2.47... bplus system  2.58..
'q1$ = "john": q2$ = "jane" '   1.48... bplus system  1.42..
'q1$ = "play": q2$ = "ball" '   1.97... bplus system  2.08..


Wow another second + off the longest, that's a huge percent at 6.9 or so to start.

I am still trying to figure out Iterating digits squared now I have another to study:
*The 20 on line 45 is artifact from some other test code.
*500 NEXT i     _Continue does not work for you also (maybe I screwed up when I tried) or just old habit? Oh I can check it here!
*I see 2 arrays used to store hit info the word of course but also the letter place they differ.
Well that's from first read through... got to go but hope to figure it out this afternoon for big aha!
« Last Edit: September 10, 2021, 12:44:05 pm by bplus »

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Word ladder - Rosetta Code
« Reply #29 on: September 10, 2021, 01:42:19 pm »
Update: _Continue does work in this line (instead of 500)
Code: QB64: [Select]
  1. IF w(i) = "*****" THEN 500
  2.