Guest User

sortbylastname.lua

a guest
Sep 24th, 2019
176
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.36 KB | None | 0 0
  1. #!/usr/bin/lua5.3
  2.  
  3. local bignumber
  4. do
  5.     local bignumbermeta={}
  6.    
  7.     -- comparison operators
  8.     function bignumbermeta:__lt(other)
  9.         if type(other)=='number' or type(other)=='string' then
  10.             other=bignumber(other)
  11.         end
  12.         local a, b=rawget(self, 'val'), rawget(other, 'val')
  13.         if #a<#b then
  14.             return true
  15.         elseif #a>#b then
  16.             return false
  17.         else
  18.             return a<b
  19.         end
  20.     end
  21.    
  22.     -- mathematical operators
  23.     function bignumbermeta:__add(other)
  24.         -- retrieve values
  25.         if type(other)=='number' or type(other)=='string' then
  26.             other=bignumber(other)
  27.         end
  28.         local a, b=rawget(self, 'val'), rawget(other, 'val')
  29.        
  30.         -- align numbers
  31.         if #a<#b then
  32.             a=('0'):rep(#b-#a)..a
  33.         elseif #b<#a then
  34.             b=('0'):rep(#a-#b)..b
  35.         end
  36.        
  37.         -- perform the addition
  38.         local c, carry='', 0
  39.         for i=1, #a do
  40.             local ca, cb=tonumber(a:sub(-i, -i)), tonumber(b:sub(-i, -i))
  41.             local cc=ca+cb+carry
  42.             carry, c=math.floor(cc/10), (cc%10)..c
  43.         end
  44.         if carry~=0 then
  45.             c=carry..c
  46.         end
  47.         return bignumber(c)
  48.     end
  49.    
  50.     -- mathematical operators
  51.     function bignumbermeta:__mul(other)
  52.         -- retrieve values
  53.         if type(other)=='number' or type(other)=='string' then
  54.             other=bignumber(other)
  55.         end
  56.         local a, b=rawget(self, 'val'), rawget(other, 'val')
  57.        
  58.         -- perform the multiplication
  59.         local c=bignumber(0)
  60.         for i=1, #a do
  61.             local ca, cb=tonumber(a:sub(-i, -i)), b..('0'):rep(i-1)
  62.             for i=1, ca do -- forgive me for this
  63.                 c=c+cb
  64.             end
  65.         end
  66.         return c
  67.     end
  68.    
  69.     -- constructor
  70.     function bignumber(val)
  71.         return setmetatable({val=tostring(val)}, bignumbermeta)
  72.     end
  73. end
  74.  
  75. local function sortbylastname(...)
  76.     -- read all varargs into a table
  77.     local elements={...}
  78.    
  79.     -- determine max lastname length, and max, min and delta values for characters
  80.     local longest=0
  81.     local minscoreunit, maxscoreunit, scoreunitdelta=math.huge, 0
  82.     for i, element in ipairs(elements) do
  83.         local length=#element
  84.         if length>longest then
  85.             longest=length
  86.         end
  87.         for j=1, length do
  88.             local val=element:byte(j, j)
  89.             if val>maxscoreunit then
  90.                 maxscoreunit=val
  91.             elseif val<minscoreunit then
  92.                 minscoreunit=val
  93.             end
  94.         end
  95.     end
  96.     scoreunitdelta=maxscoreunit-minscoreunit
  97.    
  98.     -- determine lastname scores
  99.     local scores={}
  100.     for i, element in ipairs(elements) do
  101.         local lastname=element:match "(%S+)$":lower()
  102.         local score=bignumber(0) -- scores can get pretty big so lua numbers will overflow
  103.         for j=1, longest do
  104.             local charval=lastname:byte(j, j) or minscoreunit
  105.             score=score*scoreunitdelta+(charval-minscoreunit)
  106.         end
  107.         scores[i]=score
  108.     end
  109.    
  110.     -- build output table
  111.     local sorted={}
  112.     while #scores~=0 do
  113.         -- add the biggest one to the end each time
  114.         local maxscore, maxscoreidx=bignumber(0), 0
  115.         for i, score in ipairs(scores) do
  116.             if maxscore<score then
  117.                 maxscore=score
  118.                 maxscoreidx=i
  119.             end
  120.         end
  121.         table.insert(sorted, 1, elements[maxscoreidx])
  122.         table.remove(scores, maxscoreidx)
  123.         table.remove(elements, maxscoreidx)
  124.     end
  125.    
  126.     -- return the sorted names
  127.     return table.unpack(sorted)
  128. end
  129.  
  130. print(sortbylastname(
  131.     'James Kirk', 'Spock', 'Leonard McCoy', 'Montgomery Scott',
  132.     'Jean-Luc Picard', 'William Riker', 'Worf', 'Beverly Crusher',
  133.     'Benjamin Sisko', 'Julian Bashir', 'Jadzia Dax', 'Miles O\'Brien', 'Kira Nerys',
  134.     'Kathryn Janeway', 'Chakotay', 'Seven of Nine', 'Tuvok',
  135.     'Jonathan Archer', 'T\'Pol', 'Charles Tucker III', 'Phlox'
  136. ))
Advertisement
Add Comment
Please, Sign In to add comment