Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --Scifi notif in LuaU
- -- functions
- function splitChars(str)
- str = tostring(str)
- return str:split''
- end
- function flipTable(t)
- local t2 = {}
- for i, v in next, t do
- t2[v] = i
- end
- return t2
- end
- function arrayToCharDict(arr)
- local dict = {}
- for i, v in next, arr do
- dict[i - 1] = v
- end
- return dict
- end
- function stringToCharDict(str)
- return arrayToCharDict(splitChars(str))
- end
- -- base system
- function factNumToStrFromBase(charsDict)
- local base = #charsDict + 1
- return function(num)
- local res = ''
- local iterations = 1 + math.max(0, math.floor( math.log(num, base)))
- for i = iterations, 1, -1 do -- going down number places
- local factor = base ^ (i - 1)
- local index = base - 1
- for largestDigit = base, 0, -1 do -- going down the base to 1
- local num2 = factor * largestDigit
- if num - num2 < 0 then continue end
- index = largestDigit
- num -= num2
- break
- end
- res ..= charsDict[index]
- end
- return res
- end
- end
- function factStrToNumFromBase(charsDict)
- local base = #charsDict + 1
- local flippedCharacters = flipTable(charsDict)
- return function(str)
- assert(type(str) == 'string')
- local res = 0
- for index = 1, #str do -- iterate each letter
- local letter = str:sub(index, index)
- local num = flippedCharacters[letter] * base ^ (#str - index)
- res += num
- end
- return res
- end
- end
- -- Scifi notif
- function getScifiNotatSet(num, customBase)
- customBase = customBase or 10
- assert(
- type(num) == 'number' and
- type(customBase) == 'number'
- )
- local res = {
- exponent = 0;
- rational = num;
- sign = math.sign(num)
- }
- local absNum = math.abs(num)
- if absNum ~= 0 then
- -- check out function later
- -- get how many digits - 1 they have
- local logN = math.floor(math.log(absNum, customBase))
- res.exponent = logN
- local rational = absNum * customBase ^ (-logN)
- res.rational = rational
- else
- res.sign = 1
- end
- return res
- end
- function factNumToSciFiNotif(charsDict)
- local base = #charsDict + 1
- local numToStrConvert = factNumToStrFromBase(charsDict)
- return function(num)
- assert(type(num) == 'number')
- local res = {
- sign = charsDict[0];
- rational = '';
- exponent = charsDict[1]
- }
- -- get set
- local scifiNotifSet = getScifiNotatSet(num, base)
- -- set results
- res.sign = charsDict[scifiNotifSet.sign == -1 and 0 or 1]
- res.exponent = numToStrConvert(scifiNotifSet.exponent + 150)
- -- set rational
- local rationalStr = ''
- local rational = scifiNotifSet.rational
- local str = tostring(rational):gsub('%.','')
- local digit = 0
- -- repeat until we eventually reach near zero, note, we round instead of checking exactly because
- -- there is uncountably infinite amount of numbers between 0 and 1 and hitting 0 in a sea of
- -- numbers is touch. Also we offset the rational with 1e12 to try to preserve the decimals.
- while math.round(rational * 1e12) ~= 0 do
- -- go down the vase
- for index = 0, base do
- local num1 = index * (base ^ -digit)
- local num2 = rational - num1
- if math.sign(num2) == -1 then
- rational -= ((index - 1) * (base ^ -digit))
- rationalStr ..= charsDict[index - 1]
- break
- end
- end
- digit += 1
- end
- res.rational = rationalStr
- -- return
- return res
- end
- end
- function factSciFiNotifToNum(charsDict)
- local base = #charsDict + 1
- local strToNumBaseConvert = factStrToNumFromBase(charsDict)
- local flippedDict = flipTable(charsDict)
- return function(scifiNotif)
- assert(
- type(scifiNotif) == 'table' and
- type(scifiNotif.exponent) == 'string' and
- type(scifiNotif.rational) == 'string' and
- type(scifiNotif.sign) == 'string'
- )
- local res = 0
- -- exponent and sign
- local exponent = strToNumBaseConvert(scifiNotif.exponent) - 150
- local sign = flippedDict[scifiNotif.sign] == 0 and -1 or 1
- -- give the int and mantissa their own section
- local rationalStr = scifiNotif.rational
- local intStr = rationalStr:sub(1,1)
- local mantissaStr = rationalStr:sub(2)
- local number = 0
- for exponentB = 0, #rationalStr - 1 do
- local charIndex = exponentB + 1
- local char = rationalStr:sub(charIndex, charIndex)
- number += flippedDict[char] * (base ^ -exponentB)
- end
- -- construct result
- res = sign * (number * (base ^ exponent))
- return res
- end
- end
- local dict = stringToCharDict'zxcvbnm,./asdfghjkl;qwertyuiop[]1234567890-=ZXCVBNM<>?ASDFGHJKL:"QWERTYUIOP{}|!@#$%^&*()_+'
- local n = -1
- local n2 = factNumToSciFiNotif(dict)(n)
- print(n, n2, factSciFiNotifToNum(dict)(n2))
Add Comment
Please, Sign In to add comment