Lua Challenges

  • MiningMarsh
    13th Oct 2013 Member 0 Permalink

    Well this died, but for anyone interested here is my solution: http://bpaste.net/show/141114/

    Here is my original solution: http://bpaste.net/show/141115/

     

    ------

     

    So here is an experiment for anyone bored looking for something to do:

     

    I post random programming challenges to be solved in lua, you post your implementation (preferably via pastebin).

     

    I will try to wait for a few solutions before putting up a new challenge.

     

    I shall check solutions on my machine and post the names of those who solved them correctly by order of how long their algorithm takes.

     

    Here is problem 1:

     

    Create a function dice(s,n,k) that returns the number of unique rolls that you can make with n number of dice, where each dice has k sides (so if k is six, that means your dice has 6 sides, starting with 1, ending at 6) where all the dice added up gives you s. The order of the dice does matter, a roll of 2 1 1 is different than a roll of 1 1 2, for example.

     

    A few sample runs:

     

    dice(15, 5, 5) -> 381

    dice(1, 1, 1) -> 1

    dice(20, 1, 1) -> 0

    dice(5, 2, 6) -> 4

    dice(50, 20, 20) -> 18851284297384

    Edited once by MiningMarsh. Last: 16th Oct 2013
  • bimmo_devices
    13th Oct 2013 Member 0 Permalink

    Heres a function (not in lua) that requires quite a fast computer:

     

    dice(s, n, k)=    dice(s, n, k)

     

    recusion is fine right? not sure if it has any bugs though.

    Edited once by bimmo_devices. Last: 13th Oct 2013
  • mniip
    13th Oct 2013 Developer 0 Permalink
    @MiningMarsh (View Post)
    oh it's still that dice task?
  • MiningMarsh
    13th Oct 2013 Member 0 Permalink

    @mniip (View Post)

    I figured we might as well use that one to start since I already know two fast solutions, and it happens to be a fun one to solve.

  • edza101
    13th Oct 2013 Member 0 Permalink

    Does it have to be lua? I prefer python.

    Edited once by edza101. Last: 13th Oct 2013
  • MiningMarsh
    13th Oct 2013 Member 0 Permalink

    @edza101 (View Post)

    If no one wants to do it in lua I can change it to be any language, but I figured that since TPT uses lua, why not lua?

  • china-richway2
    13th Oct 2013 Member 0 Permalink

    Local diced = {}

    function dice(s,n,k)

        local sum,i

         If n=0

               if s=0

                     Return 1

                Else

                      Return 0

                End

          end

         If typename(diced[s][n]) == "number"

             Return diced[s][n]

         End

        For I = 1 to k

            Sum = sum + dice(s-I,n-1,k)

         End

         Diced[s][n] = sum

         Return sum

    end

     

    Sorry, I am not on my computer, cannot test, please correct the syntax for me, thanks.

    Edited 3 times by china-richway2. Last: 13th Oct 2013
  • china-richway2
    18th Oct 2013 Member 0 Permalink

    What is my score?

  • mniip
    18th Oct 2013 Developer 1 Permalink
    it doesn't even work