Author Topic: String Collapse Function - Rosetta Code  (Read 4642 times)

0 Members and 1 Guest are viewing this topic.

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
String Collapse Function - Rosetta Code
« on: July 28, 2021, 08:27:57 pm »
Code: QB64: [Select]
  1. _Title "Collapse String Function - Rosetta Code" 'b+ 2021-07-28
  2. ' ref: http://rosettacode.org/wiki/Determine_if_a_string_is_collapsible
  3. t$(1) = "" ' a null string  (length zero)
  4. t$(2) = Chr$(34) + "If I were two-faced, would I be wearing this one?" + Chr$(34) + " --- Abraham Lincoln "
  5. t$(3) = "..1111111111111111111111111111111111111111111111111111111111111117777888"
  6. t$(4) = "I never give 'em hell, I just tell the truth, and they think it's hell. "
  7. t$(5) = "                                                    --- Harry S Truman  "
  8. For i = 1 To 5
  9.     Print t$(i); Len(t$(i))
  10.     Print collapse$(t$(i)); Len(collapse$(t$(i)))
  11.  
  12. Function collapse$ (s$)
  13.     If Len(s$) < 2 Then collapse$ = s$: Exit Function
  14.     c$ = Mid$(s$, 1, 1)
  15.     collapse$ = c$
  16.     For i = 2 To Len(s$)
  17.         If Mid$(s$, i, 1) <> c$ Then c$ = Mid$(s$, i, 1): collapse$ = collapse$ + c$
  18.     Next
  19.  

Hey, about 10 lines less than FreeBASIC's entry. ;-))

Offline DANILIN

  • Forum Regular
  • Posts: 128
    • View Profile
    • Danilin youtube
Re: String Collapse Function - Rosetta Code
« Reply #1 on: July 29, 2021, 04:01:08 am »
register there and supplement
it and I assume: variables there will be unnecessary

in meantime: perhaps a similar humor topic

Shuffling Letters

https://qb64.org/forum/index.php?topic=3982.0
Russia looks world from future. big data is peace data.
https://youtube.com/playlist?list=PLBBTP9oVY7IagpH0g9FNUQ8JqmHwxDDDB
i never recommend anything to anyone and always write only about myself

Offline Ashish

  • Forum Resident
  • Posts: 630
  • Never Give Up!
    • View Profile
Re: String Collapse Function - Rosetta Code
« Reply #2 on: July 29, 2021, 04:04:06 am »
@bplus
Nice! Since you have used two colons ( : ), here is my version using amount of colons. :)
Code: QB64: [Select]
  1. 'Ashish attempt to do collapse$ function
  2. Function collapse$ (s$)
  3.     If Len(s$) = 0 Then Exit Function Else a0 = Asc(Mid$(s$, 1, 1)): collapse$ = Chr$(a0)
  4.     For i = 2 To Len(s$)
  5.         If Asc(Mid$(s$, i, 1)) Xor a0 Then a0 = Asc(Mid$(s$, i, 1)): collapse$ = collapse$ + Chr$(a0)
  6.     Next
  7.  
if (Me.success) {Me.improve()} else {Me.tryAgain()}


My Projects - https://github.com/AshishKingdom?tab=repositories
OpenGL tutorials - https://ashishkingdom.github.io/OpenGL-Tutorials

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: String Collapse Function - Rosetta Code
« Reply #3 on: July 29, 2021, 06:12:46 am »
@DANILIN I tried a spinning letters in middle too!

OK! @Ashish

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: String Collapse Function - Rosetta Code
« Reply #4 on: July 29, 2021, 06:24:51 am »
Wait, @Ashish, wouldn't it be simpler to avoid ASC function because you just have to reverse it back with Chr$()?

Update: Oh you wanted to be fancy pants with the XOR ;-))
« Last Edit: July 29, 2021, 06:27:51 am by bplus »

Offline euklides

  • Forum Regular
  • Posts: 128
    • View Profile
Re: String Collapse Function - Rosetta Code
« Reply #5 on: July 29, 2021, 08:10:05 am »
An other way:

Function collapse$ (s$)
    collapse$ = Left$(s$ + Chr$(1), 1): For x = 2 To Len(s$): If Mid$(s$, x, 1) <> Mid$(s$, x - 1, 1) Then collapse$ = collapse$ + Mid$(s$, x, 1)
    Next x: If collapse$ = Chr$(1) Then collapse$ = ""
End Function
Why not yes ?

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: String Collapse Function - Rosetta Code
« Reply #6 on: July 29, 2021, 08:58:17 am »
Oh ha! mine works without the first line in function. Ashish is not working for me. Euklides is same amount of lines as mine when I drop first line and don't allow colons:
Code: QB64: [Select]
  1. _Title "Collapse String Function - Rosetta Code" 'b+ 2021-07-28
  2. ' ref: http://rosettacode.org/wiki/Determine_if_a_string_is_collapsible
  3. t$(1) = "" ' a null string  (length zero)
  4. t$(2) = Chr$(34) + "If I were two-faced, would I be wearing this one?" + Chr$(34) + " --- Abraham Lincoln "
  5. t$(3) = "..1111111111111111111111111111111111111111111111111111111111111117777888"
  6. t$(4) = "I never give 'em hell, I just tell the truth, and they think it's hell. "
  7. t$(5) = "                                                    --- Harry S Truman  "
  8.  
  9. t$(6) = "A"
  10. t$(7) = "AA"
  11. t$(8) = " AA"
  12.  
  13. For i = 1 To 8
  14.     Print t$(i); Len(t$(i))
  15.     Print collapse$(t$(i)); Len(collapse$(t$(i)))
  16.  
  17. Function collapse$ (s$)
  18.     'If Len(s$) < 2 Then collapse$ = s$ ' : Exit Function
  19.     c$ = Mid$(s$, 1, 1)
  20.     collapse$ = c$
  21.     For i = 2 To Len(s$)
  22.         If Mid$(s$, i, 1) <> c$ Then c$ = Mid$(s$, i, 1): collapse$ = collapse$ + c$
  23.     Next
  24.  
  25. 'Function collapse$ (s$) ' euklides
  26. '    collapse$ = Left$(s$ + Chr$(1), 1)
  27. '    For x = 2 To Len(s$)
  28. '        If Mid$(s$, x, 1) <> Mid$(s$, x - 1, 1) Then collapse$ = collapse$ + Mid$(s$, x, 1)
  29. '    Next x
  30. '    If collapse$ = Chr$(1) Then collapse$ = ""
  31. 'End Function
  32.  
  33. 'Ashish attempt to do collapse$ function
  34. 'Function collapse$ (s$)
  35. '    If Len(s$) = 0 Then Exit Function Else a0 = Asc(Mid$(s$, 1, 1))
  36. '    collapse$ = Chr$(a0)
  37. '    For i = 2 To Len(s$)
  38. '        If Asc(Mid$(s$, i, 1)) Xor a0 Then a0 = Asc(Mid$(s$, i, 1))
  39. '        collapse$ = collapse$ + Chr$(a0)
  40. '    Next
  41. 'End Function
  42.  
  43.  
  44.  

Oops! nope I still have a colon, euklides wins! so far :)
« Last Edit: July 29, 2021, 09:04:14 am by bplus »

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: String Collapse Function - Rosetta Code
« Reply #7 on: July 29, 2021, 09:12:41 am »
OK following euklides example, I whittled off another line:
Code: QB64: [Select]
  1. _Title "Collapse String Function - Rosetta Code" 'b+ 2021-07-28
  2. ' ref: http://rosettacode.org/wiki/Determine_if_a_string_is_collapsible
  3. t$(1) = "" ' a null string  (length zero)
  4. t$(2) = Chr$(34) + "If I were two-faced, would I be wearing this one?" + Chr$(34) + " --- Abraham Lincoln "
  5. t$(3) = "..1111111111111111111111111111111111111111111111111111111111111117777888"
  6. t$(4) = "I never give 'em hell, I just tell the truth, and they think it's hell. "
  7. t$(5) = "                                                    --- Harry S Truman  "
  8.  
  9. t$(6) = "A"
  10. t$(7) = "AA"
  11. t$(8) = " AA"
  12.  
  13. For i = 1 To 8
  14.     Print t$(i); Len(t$(i))
  15.     Print collapse$(t$(i)); Len(collapse$(t$(i)))
  16.  
  17. Function collapse$ (s$) ' bplus fixed by euklides example
  18.     collapse$ = Mid$(s$, 1, 1)
  19.     For i = 2 To Len(s$)
  20.         If Mid$(s$, i, 1) <> Mid$(s$, i - 1, 1) Then collapse$ = collapse$ + Mid$(s$, i, 1)
  21.     Next
  22.  
  23. 'Function collapse$ (s$) ' euklides
  24. '    collapse$ = Left$(s$ + Chr$(1), 1)
  25. '    For x = 2 To Len(s$)
  26. '        If Mid$(s$, x, 1) <> Mid$(s$, x - 1, 1) Then collapse$ = collapse$ + Mid$(s$, x, 1)
  27. '    Next x
  28. '    If collapse$ = Chr$(1) Then collapse$ = ""
  29. 'End Function
  30.  
  31. 'Ashish attempt to do collapse$ function
  32. 'Function collapse$ (s$)
  33. '    If Len(s$) = 0 Then Exit Function Else a0 = Asc(Mid$(s$, 1, 1))
  34. '    collapse$ = Chr$(a0)
  35. '    For i = 2 To Len(s$)
  36. '        If Asc(Mid$(s$, i, 1)) Xor a0 Then a0 = Asc(Mid$(s$, i, 1))
  37. '        collapse$ = collapse$ + Chr$(a0)
  38. '    Next
  39. 'End Function
  40.  
  41.  

Offline Ashish

  • Forum Resident
  • Posts: 630
  • Never Give Up!
    • View Profile
Re: String Collapse Function - Rosetta Code
« Reply #8 on: July 29, 2021, 11:42:22 am »
@bplus you edited my code in incorrect way -
This is how it must be -
Code: QB64: [Select]
  1. 'Ashish attempt to do collapse$ function
  2. Function collapse$ (s$)
  3.     If Len(s$) = 0 Then Exit Function Else a0 = Asc(Mid$(s$, 1, 1))
  4.     collapse$ = Chr$(a0)
  5.     For i = 2 To Len(s$)
  6.         If Asc(Mid$(s$, i, 1)) Xor a0 Then
  7.             a0 = Asc(Mid$(s$, i, 1))
  8.             collapse$ = collapse$ + Chr$(a0)
  9.         End If
  10.     Next
  11.  
  12.  
if (Me.success) {Me.improve()} else {Me.tryAgain()}


My Projects - https://github.com/AshishKingdom?tab=repositories
OpenGL tutorials - https://ashishkingdom.github.io/OpenGL-Tutorials

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: String Collapse Function - Rosetta Code
« Reply #9 on: July 29, 2021, 01:10:49 pm »
Sorry @Ashish

I see what I did wrong now, will try to be more careful with colons on IF lines.