VB / VBA Programming Contests?

Discussion in 'OT Technology' started by Early Apex, May 13, 2003.

  1. Early Apex

    Early Apex Guest

    Whaddya think about a series of informal VB and/or VBA programming challenges? Not for bragging rights, really, but just to keep skills sharp and see other ways of solving problems. VB-based languages seem to be pretty common around here, and everyone with Excel has VBA, so I'll throw out the first challenge:

    Develop a small (< 200 lines), formless Excel VBA script that extends the series:

    1
    11
    21
    1211
    111221
    312211

    for 100 iterations. If you're not familiar with this series, read it aloud:

    "one"
    "one one"
    "two ones"
    "one two, one one"
    "one one, one two, two ones"
    "three ones, two twos, one one"

    where each line describes the digits of the line above it. Have your script either print each line in the debug window or write it to an Excel cell, your choice.

    The winner will be the script that accomplishes the task in the fewest amount of VBA lines. Declarations will not count toward total lines, so feel free to be explicit. No colons allowed, no external functions or subs.

    Challenge ends...let's say tomorrow at 4:00 pm.
     
  2. Black Light

    Black Light Guest

    i'd do it if i figured out the logic behind the pattern u suggest :o
     
  3. Black Light

    Black Light Guest

    wait, i got it now! it simply describes the line above by counting and naming each character/number being used, right?
     
  4. Black Light

    Black Light Guest

    how do u want to launch it? with a shortcut key or a button?
     
  5. Early Apex

    Early Apex Guest

    Don't worry about launching it. It's an Excel VBA sub or function, so it can be executed simply by running it in the VBA IDE. No interface, we're looking for algorithm only.
     
  6. Early Apex

    Early Apex Guest

    Hmm...if we're doing this in Excel VBA then I'll have to limit it to 38 iterations. The 38th number in this series is 32,767 digits long!
     
  7. 5Gen_Prelude

    5Gen_Prelude There might not be an "I" in the word "Team", but

    Joined:
    Mar 14, 2000
    Messages:
    14,519
    Likes Received:
    1
    Location:
    Vancouver, BC, CANADA
    I didn't shorten this very well, but I was bored:

    Code:
    Function Series(Start As Long, Iterations As Long)
    
    Dim IterationIndex, Position, DigitCounter As Long
    Dim Output, Old, CurrentDigit As String
    
    Output = Trim(Start)
    Debug.Print Output
    
    For IterationIndex = 2 To Iterations
        Old = Output
        Output = ""
        CurrentDigit = Left(Old, 1)
        DigitCounter = 0
        For Position = 1 To Len(Old)
            If Mid(Old, Position, 1) = CurrentDigit Then
                DigitCounter = DigitCounter + 1
            Else
                Output = Output & Trim(DigitCounter) & CurrentDigit
                CurrentDigit = Mid(Old, Position, 1)
                DigitCounter = 1
            End If
        Next
        Output = Output & Trim(DigitCounter) & CurrentDigit
        Debug.Print Output
    Next
    
    End Function
    
     
    Last edited: May 14, 2003
  8. Early Apex

    Early Apex Guest

    I can do it in four lines, excluding declarations and "Sub" and "End Sub" lines ;)
     
  9. 5Gen_Prelude

    5Gen_Prelude There might not be an "I" in the word "Team", but

    Joined:
    Mar 14, 2000
    Messages:
    14,519
    Likes Received:
    1
    Location:
    Vancouver, BC, CANADA
    That's super.
     
  10. Early Apex

    Early Apex Guest

    Sure is.

    Code:
    Sub ContinueSeries()
        Dim n As Integer
        
        Cells(1, 2) = "1"
        For n = 2 To 38
            Cells(n, 2) = Replace(Replace(Replace(Replace(Replace(Replace(Replace(Replace(Replace(Replace(Replace(Replace(Replace(Replace(Replace(Replace(CStr(Cells(n - 1, 2)), "111", "a"), "222", "b"), "11", "c"), "22", "d"), "33", "e"), "1", "f"), "2", "g"), "3", "h"), "a", "31"), "b", "32"), "c", "21"), "d", "22"), "e", "23"), "f", "11"), "g", "12"), "h", "13")
        Next n
    End Sub
    
     
  11. 5Gen_Prelude

    5Gen_Prelude There might not be an "I" in the word "Team", but

    Joined:
    Mar 14, 2000
    Messages:
    14,519
    Likes Received:
    1
    Location:
    Vancouver, BC, CANADA
    That has got to be the most :greddy: code ever. Now try not to hardcode it and have the flexibility of mine ;)
     
  12. SLED

    SLED build an idiot proof device and someone else will

    Joined:
    Sep 20, 2001
    Messages:
    28,118
    Likes Received:
    0
    Location:
    AZ, like a bauce!
    oh my gay! That may be four lines of code, but i promise you it's not the most efficient for Christ's sake!
     
  13. Early Apex

    Early Apex Guest

    Uh, what? Okay, Add a "number of iterations" argument, and it's as flexible as yours. Your start argument is irrelevant; the series is given as starting at 1.

    Oh, and put the two funtions on the same machine, have them run 200 iterations, and see which one finishes first. I guarantee it'll be mine. ;)

    Next challenge...?
     
  14. 5Gen_Prelude

    5Gen_Prelude There might not be an "I" in the word "Team", but

    Joined:
    Mar 14, 2000
    Messages:
    14,519
    Likes Received:
    1
    Location:
    Vancouver, BC, CANADA
    I don't have a problem with the efficiency, I have a problem with the fact that you had to hardcode the series. In other words, you actually had to solve the series problem before you wrote the code, which defeats the purpose of writing code. The Tower of Hanoi recursive routine is a beautiful example of small, efficient code that solves a problem that doesn't need solving by hand first. That's what I was expecting - my version is pretty straight forward and by no means efficient, but the only real difference between the two is that you give the computer set patterns to convert, I didn't have to tell the computer to convert a 111 to a 31.
     
  15. Early Apex

    Early Apex Guest


    :dunno: Depends on what you mean by "hardcode". Initially, I wrote a script almost indetical to yours, and was happy with it. But then when I
    examined the results, I found that given the parameters of the problem, there were a finite (and small) number of patterns that occurred in the
    output. Knowing this, I thought that a script that works on pattern recognition and conversion in strings would be much more efficient --
    and elegant -- than a script that counts number series.

    Indeed, when you get to very large results, the pattern replacement approach is exponentially faster than the number series/counting
    approach.

    My script couldn't handle a series that starts with, say, "4", where yours can. But that wasn't part of the problem. ;)

    Someone throw out the next algorithm challenge...
     
  16. Early Apex

    Early Apex Guest

    PS, and just FYI: You realize that

    Code:
    Dim IterationIndex, Position, DigitCounter As Long
    declares just "DigitCounter" as long? Without explicit type declarations for the other two variables, they're handled as variants.
     
  17. 5Gen_Prelude

    5Gen_Prelude There might not be an "I" in the word "Team", but

    Joined:
    Mar 14, 2000
    Messages:
    14,519
    Likes Received:
    1
    Location:
    Vancouver, BC, CANADA
    Yeah, bad habit. In fact normally I do one variable on each line for this exact reason, I can never remember what the rules are from language to language. And gone are the days of the C=64 memory limitations where if you weren't using shortcuts for commands, you were simply wasting space :rofl:

    I like your way as an "ends to a means" approach as it shows a different way of thinking, but I don't like that you had to supply the computer with the patterns to match. If you had come up with an algorithm that mathematically creates the pattern to compare, I would have been impressed.
     

Share This Page