Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/lua5.3
- local bignumber
- do
- local bignumbermeta={}
- -- comparison operators
- function bignumbermeta:__lt(other)
- if type(other)=='number' or type(other)=='string' then
- other=bignumber(other)
- end
- local a, b=rawget(self, 'val'), rawget(other, 'val')
- if #a<#b then
- return true
- elseif #a>#b then
- return false
- else
- return a<b
- end
- end
- -- mathematical operators
- function bignumbermeta:__add(other)
- -- retrieve values
- if type(other)=='number' or type(other)=='string' then
- other=bignumber(other)
- end
- local a, b=rawget(self, 'val'), rawget(other, 'val')
- -- align numbers
- if #a<#b then
- a=('0'):rep(#b-#a)..a
- elseif #b<#a then
- b=('0'):rep(#a-#b)..b
- end
- -- perform the addition
- local c, carry='', 0
- for i=1, #a do
- local ca, cb=tonumber(a:sub(-i, -i)), tonumber(b:sub(-i, -i))
- local cc=ca+cb+carry
- carry, c=math.floor(cc/10), (cc%10)..c
- end
- if carry~=0 then
- c=carry..c
- end
- return bignumber(c)
- end
- -- mathematical operators
- function bignumbermeta:__mul(other)
- -- retrieve values
- if type(other)=='number' or type(other)=='string' then
- other=bignumber(other)
- end
- local a, b=rawget(self, 'val'), rawget(other, 'val')
- -- perform the multiplication
- local c=bignumber(0)
- for i=1, #a do
- local ca, cb=tonumber(a:sub(-i, -i)), b..('0'):rep(i-1)
- for i=1, ca do -- forgive me for this
- c=c+cb
- end
- end
- return c
- end
- -- constructor
- function bignumber(val)
- return setmetatable({val=tostring(val)}, bignumbermeta)
- end
- end
- local function sortbylastname(...)
- -- read all varargs into a table
- local elements={...}
- -- determine max lastname length, and max, min and delta values for characters
- local longest=0
- local minscoreunit, maxscoreunit, scoreunitdelta=math.huge, 0
- for i, element in ipairs(elements) do
- local length=#element
- if length>longest then
- longest=length
- end
- for j=1, length do
- local val=element:byte(j, j)
- if val>maxscoreunit then
- maxscoreunit=val
- elseif val<minscoreunit then
- minscoreunit=val
- end
- end
- end
- scoreunitdelta=maxscoreunit-minscoreunit
- -- determine lastname scores
- local scores={}
- for i, element in ipairs(elements) do
- local lastname=element:match "(%S+)$":lower()
- local score=bignumber(0) -- scores can get pretty big so lua numbers will overflow
- for j=1, longest do
- local charval=lastname:byte(j, j) or minscoreunit
- score=score*scoreunitdelta+(charval-minscoreunit)
- end
- scores[i]=score
- end
- -- build output table
- local sorted={}
- while #scores~=0 do
- -- add the biggest one to the end each time
- local maxscore, maxscoreidx=bignumber(0), 0
- for i, score in ipairs(scores) do
- if maxscore<score then
- maxscore=score
- maxscoreidx=i
- end
- end
- table.insert(sorted, 1, elements[maxscoreidx])
- table.remove(scores, maxscoreidx)
- table.remove(elements, maxscoreidx)
- end
- -- return the sorted names
- return table.unpack(sorted)
- end
- print(sortbylastname(
- 'James Kirk', 'Spock', 'Leonard McCoy', 'Montgomery Scott',
- 'Jean-Luc Picard', 'William Riker', 'Worf', 'Beverly Crusher',
- 'Benjamin Sisko', 'Julian Bashir', 'Jadzia Dax', 'Miles O\'Brien', 'Kira Nerys',
- 'Kathryn Janeway', 'Chakotay', 'Seven of Nine', 'Tuvok',
- 'Jonathan Archer', 'T\'Pol', 'Charles Tucker III', 'Phlox'
- ))
Advertisement
Add Comment
Please, Sign In to add comment