Skip to content

Instantly share code, notes, and snippets.

@LuizPanariello
Last active September 8, 2021 18:17
Show Gist options
  • Save LuizPanariello/6fd1a2cbe6fa3d573a39f39a310c6550 to your computer and use it in GitHub Desktop.
Save LuizPanariello/6fd1a2cbe6fa3d573a39f39a310c6550 to your computer and use it in GitHub Desktop.
<%
Class SeatItem
Private IndexVar
Private SeatVar
Private RemainderVar
public property get Index()
Index = IndexVar
end property
public property let Index(indexParam)
IndexVar = indexParam
end property
public property get Seat()
Seat = SeatVar
end property
public property let Seat(seatParam)
SeatVar = seatParam
end property
public property get Remainder()
Remainder = RemainderVar
end property
public property let Remainder(remainderParam)
RemainderVar = remainderParam
end property
end Class
function GetRemainder(n)
dim v, d, l
d = n - Int(n)
l = Int(d * 10000) / 10000
v = FormatNumber(l, 4)
GetRemainder = v
end function
function Calculation(numArr, totalSeats, decimalNum)
If IsArray(numArr) = False Or Len(Join(numArr, "")) = 0 Then
Calculation = numArr
exit function
end if
dim result(), d, i, j, TempValue
d = uBound(numArr)
redim result(d)
if decimalNum > 0 then
dim multi, powSpread, tot
multi = 10^decimalNum
tot = totalSeats * multi
powSpread = Calculation(numArr, tot, 0)
for i = 0 to uBound(powSpread)
result(i) = powSpread(i) / multi
next
else
dim sum, seatDistribution()
redim seatDistribution(d)
' get total seats already taken
sum = 0
for i = 0 to d
sum = sum + numArr(i)
next
' prioritize
for i = 0 to d
dim seats, sitem
set sitem = new SeatItem
seats = (numArr(i) / sum) * totalSeats
sitem.Index = i
sitem.Seat = Int(seats) ' behaves like floor
sitem.Remainder = GetRemainder(seats)
set seatDistribution(i) = sitem
next
' sorting data by remainder
for i = LBound(seatDistribution) to UBound(seatDistribution)
for j = LBound(seatDistribution) to UBound(seatDistribution) - 1
if seatDistribution(j).Remainder < seatDistribution(j + 1).Remainder then
set TempValue = seatDistribution(j + 1)
set seatDistribution(j + 1) = seatDistribution(j)
set seatDistribution(j) = TempValue
End If
next
next
dim takenSeats, totalRemains
takenSeats = 0
for i=0 to uBound(seatDistribution)
takenSeats = takenSeats + seatDistribution(i).Seat
next
totalRemains = totalSeats - takenSeats
for i = 0 to totalRemains - 1
seatDistribution(i).Seat = seatDistribution(i).Seat + 1
next
'order by index
for i = LBound(seatDistribution) to UBound(seatDistribution)
for j = LBound(seatDistribution) to UBound(seatDistribution) - 1
if seatDistribution(j).Index > seatDistribution(j + 1).Index then
set TempValue = seatDistribution(j + 1)
set seatDistribution(j + 1) = seatDistribution(j)
set seatDistribution(j) = TempValue
End If
next
next
'return seats
for i=0 to uBound(seatDistribution)
result(i) = seatDistribution(i).Seat
next
end if
Calculation = result
end function
' use example
'dim arr(5), resultingarr
'arr(0) = 160
'arr(1) = 23
'arr(2) = 62
'arr(3) = 108
'arr(4) = 843
'arr(5) = 0
'[13.38, 1.92, 5.18, 9.03, 70.49, 0] correct values
'dim arr(3), resultingarr
'arr(0) = 2
'arr(1) = 1
'arr(2) = 0
'arr(3) = 0
'[66.67, 33.33, 0] correct values
'dim arr(0), resultingarr, i
'arr(0) = 2
'[100] correct values
'dim arr(), resultingarr
'resultingarr = Calculation(arr, 100, 2)
'for i=0 to ubound(resultingarr)
' response.write resultingarr(i)
'next
%>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment