Please log in to view the forum threads.

Be warned, NEVER give your account details to anyone!
Double accounts are unwanted and strictly prohibited!




If your account got hacked please immediately report this to the following Discord id's: Pr0Master#7146 or Snake#0582

You can join our DISCORD support group by using the following link: https://discordapp.com/invite/fgbMxZG


Your Infinity Team

    Parenthesis Sorter

    Share
    avatar
    Starkkz
    Developer
    Developer

    Posts : 55
    Points : 75
    Likes : 6
    Age : 21
    Country : Chile/Santiago
    Join date : 2013-05-04

    Parenthesis Sorter

    Post by Starkkz on Sat Mar 01, 2014 3:43 am

    It took me two hours to make this algorithm but I managed to make a list of operations that sort the parenthesis by priority, however it reads from first parenthesis that are opened to the last parenthesis inside other parenthesis. Here's the code:

    Code:

    function NextCharacter(Character)
      local CharacterArray = {}
      for i = 1, #Character do
        CharacterArray[i] = Character:byte(i)
      end
     
      CharacterArray[#CharacterArray] = CharacterArray[#CharacterArray] + 1
      for i = #CharacterArray, 1, -1 do
        if CharacterArray[i] == 123 then
          if i == 1 then
            table.insert(CharacterArray, 97)
          else
            CharacterArray[i - 1] = CharacterArray[i - 1] + 1
          end
          CharacterArray[i] = 97
        end
      end

      local CharacterString = ""
      for i = 1, #CharacterArray do
        CharacterString = CharacterString .. string.char(CharacterArray[i])
      end
      return CharacterString
    end

    function gsubCharacter()
      local Character = "a"
      return function ()
        local Char = Character
        Character = NextCharacter(Character)
        return Char
      end
    end

    function SolveParenthesis(String)
      local LastString = {""}
      local Parenthesis = {{}}
      local ParenthesisLevel = 1
     
      for Position = 1, #String do
        local Match = string.sub(String, Position, Position)
        if Match == "(" then
          -- Optional
          LastString[#LastString] = LastString[#LastString] .. "x"
         
          ParenthesisLevel = ParenthesisLevel + 1
          Parenthesis[ParenthesisLevel] = Parenthesis[ParenthesisLevel] or {}
          LastString[#LastString + 1] = ""
        elseif Match == ")" then
          ParenthesisLevel = ParenthesisLevel - 1
          Parenthesis[ParenthesisLevel] = Parenthesis[ParenthesisLevel] or {}
          table.insert(Parenthesis[ParenthesisLevel], LastString[#LastString])
          LastString[#LastString] = nil
         
          if Position == #String then
            table.insert(Parenthesis[ParenthesisLevel], LastString[#LastString])
            LastString[#LastString] = nil
          end
        elseif Position == #String then
          table.insert(Parenthesis[ParenthesisLevel], LastString[#LastString] .. Match)
          LastString[#LastString] = nil
        else
          LastString[#LastString] = LastString[#LastString] .. Match
        end
      end
     
      local ParenthesisList = {}
      local gsubF = gsubCharacter()
      local gsubF2 = gsubCharacter()
      for i = #Parenthesis, 1, -1 do
        for _, str in pairs(Parenthesis[i]) do
          local str = gsubF2().." = "..str:gsub("x", gsubF)
          table.insert(ParenthesisList, str)
        end
      end
      return table.concat(ParenthesisList, "\n")
    end

    After trying the code to solve 2 * (1 + 1) * (5 + 10 * (1 + 2)) + 2
    Code:

    print(SolveParenthesis("2 * (1 + 1) * (5 + 10 * (1 + 2)) + 2"))

    It turned into this simple operation
    Code:

    a = 1 + 2
    b = 1 + 1
    c = 5 + 10 * a
    d = 2 * b * c + 2

    Note that d is the final result of the operation, this will probably help me to solve my problems with my scripting language.
    avatar
    Pickabu
    User

    Posts : 18
    Points : 27
    Likes : 1
    Age : 20
    Country : Russia
    Join date : 2014-02-22

    Re: Parenthesis Sorter

    Post by Pickabu on Tue Mar 04, 2014 3:38 pm

    is that fucking pascal?
    avatar
    Starkkz
    Developer
    Developer

    Posts : 55
    Points : 75
    Likes : 6
    Age : 21
    Country : Chile/Santiago
    Join date : 2013-05-04

    Re: Parenthesis Sorter

    Post by Starkkz on Sun Mar 23, 2014 10:44 pm

    It's Lua.
    avatar
    NiaoYman
    Moderator
    Moderator

    Posts : 297
    Points : 322
    Likes : 2
    Country : greece
    Hobbies : playing video games and taekwonto+paintball
    Join date : 2013-12-29

    Re: Parenthesis Sorter

    Post by NiaoYman on Sun Mar 23, 2014 11:49 pm

    Its a gift from alien....



    “When life gives you lemons, don’t make lemonade. Make life take the lemons back! Get mad! I don’t want your damn lemons, what the hell am I supposed to do with these? Demand to see life’s manager! Make life rue the day it thought it could give niaouman lemons! Do you know who I am? I’m the man who’s gonna burn your house down! With the lemons! I’m gonna get my engineers to invent a combustible lemon that burns your house down!”
    JOIN THE DARK SIDE OR DIE

    dawood(the Azul)
    Member
    Member

    Posts : 379
    Points : 433
    Likes : 6
    Age : 19
    Country : jordan
    Hobbies : football basketball skiing swimming tennis badminton
    Join date : 2014-01-16

    Re: Parenthesis Sorter

    Post by dawood(the Azul) on Mon Mar 24, 2014 1:50 pm

    Dont trust aliens cuz they want the earth (joking) .



    Loosing a friend is too hard and bad
    I will remember you forever Sai
    Sai the phantom

    Sponsored content

    Re: Parenthesis Sorter

    Post by Sponsored content


      Current date/time is Wed Aug 15, 2018 8:35 am