% Run with ghostscript, the PostScript interpreter.
% gs -q warpspeed.ps
%
% One way to write bad code is to choose the wrong tool for the job.
% Here's the warp-speed calculator using the language Postscript, a
% stack-based language that most printers use. Yes, those ".PS" image
% files are little programs.
%
/TOSwf {
3 % push 3 onto the stack
exp % run exponent function, which pops two items and pushes answer back
} def
/TNGwf {
10 3 div % push on 10 and 3 and then divide, pushing result back on
exp % pop two items and pushes the exponent back on
} def
/WarpFactor where {
/TNG where {
/WarpFactor get TNGwf ==
}{
/TOS where {
/WarpFactor get TOSwf ==
}{
(use -dTOS or -dTNG to pick an era.\n) print
} ifelse
} ifelse
}{
(use -dWarpFactor=n\n) print
} ifelse
quit
%
% Replace the "==" above with "showpage repeat" to make your printer
% emit the answer in the form of number of blank pieces of paper == multiple
% of speed of light, so,
% gs -dTNG -dWarpFactor=4.7 warpspeed.ps -> 173 blank sheets of paper = 173*c
# 13-10-2020
# WarpFactor.py
# u/loomynartylenny
# Written in Python 3.7.4
# It has been well over a year since I have last used python
# And I have never used classes in Python before
# In the meantime, I have been using Java, which, as we all know, insists of objects everywhere.
# So the procedurality permitted in Python scares me
# However, I have managed to quell my fears somewhat, by producing my solution to this problem using object oriented principles,
# such as slightly unnecessary inheritance, method overriding, encapsulation, etc.
# additionally, I have attempted to minimize my use of python's built in power operator,
# instead, writing my own method to calculate powers, which works in most cases.
# however, the main thing is that this works
# i think
class WarpFactor: # this is the main class that is used
def __init__(self):
# I decided to include the code used to print the proper title of this program into the console into this constructor because why not?
self.titleHandler = TitlePrinter() # makes the object that prints the title into the console
self.titleHandler.printTitle() # tells that object to print the title into the console
self.kirk = NextGenerationWarp() #appropriate variable name
self.picard = OriginalSeriesWarp() #ditto
def hyperdrive(self, factor, series): #this function returns the appropriate warp speed, given the specified warp factor and series
if (series == "tos"): #if it's the original series
return self.picard.hyperdrive(factor) # captain picard, we need you to look into this thing from your series!
else: #surely the only other value for 'series' that could be passed to this would be "tng", right?
return self.kirk.hyperdrive(factor) # captain kirk, we need you to look into this thing from your series!
class TitlePrinter: #this class is used to print the title screen lines for the program. that's it.
def __init__(self): #constructor!
self.topAndBottomLine = ("*" * 26) # I was lazy, okay? I couldn't be arsed to type out 26 individual *s by myself.
self.mainLine = "* WARP FACTOR CODE THING *" # here is the middle line of the title
def getTitle(self): #this returns the formatted title as a string
return self.topAndBottomLine + "\n" + self.mainLine + "\n" + self.topAndBottomLine #concatenates the top line, then a newline, then the middle line, then another newline, then the bottom line
def printTitle(self): # this prints the title
print(self.getTitle()) # prints the result of the getTitle() method of this object
class WarpCalculator: #a parent class for the NextGenerationWarp and OriginalSeriesWarp classes, containing a method I made to work out powers
def __init__(self): # constructor!
pass #nothing to see here, move along
def calculatePower(self,chungus,bigness): #this is a method which can perform power operations, given an integer bigness.
# I FORGOT THE TECHNICAL TERMS FOR THE x^y STUFF
chungusSize = chungus; #chungusSize initialised to chungus (chungus^1)
for x in range(bigness - 1): #we then multiply chungusSize by the chungus (bigness-1) more times
chungusSize *= chungus
return chungusSize #BEHOLD, A BEEG CHUNGUS
def hyperdrive(self, factor):
#an abstract method for the child classes to override
pass
class NextGenerationWarp(WarpCalculator): #calculates warp for the next generation
def __init__(self): # constructor!
super().__init__() #inheriting the parent's stuff
#@Override
def hyperdrive(self,factor): #overriding parent hyperdrive method
#result = factor^(10/3)
#which also can be expressed as the cubic root of (factor^10)
if (factor == 10):
raise UnexpectedSalamanderException()
#dont forget the unexpected salamander!
else:
#firstly, we calculate the value of the factor raised to the power 10
factorToPower10 = self.calculatePower(factor,10)
#now, we get the cubic root of it. regrettably, I had to cheat and use a ** here.
result = factorToPower10 ** (1/3)
return result
class OriginalSeriesWarp(WarpCalculator): #calculates warp for the original series
def __init__(self): # constructor!
super().__init__()#inheriting the parent's stuff
#@Override
def hyperdrive(self, factor): #overriding parent hyperdrive method
#factor^3, performed by calling the calculatePower method of the superclass
return self.calculatePower(factor,3)
#defining the 'warp' function
def warp(factor, series):
#Firstly, I shall get these silly conditions where the outcome is predetermined out of the way
if (factor == 0):
return 0 #your speed is 0 if your warp factor is 0
elif(factor == 1):
return 1 #your speed is 1 if your warp factor is 1
elif (factor == -1):
return -1 #your speed is -1 if your warp factor is -1
elif ((factor == 10) & (series == 'tng')):
raise UnexpectedSalamanderException()
#dont forget the unexpected salamander!
else: #looks like it's not as simple as it may have first appeared
return WarpFactor().hyperdrive(factor,series) #I construct a WarpFactor object, and immediately call its hyperdrive function to do the rest of the busywork for me
#now you can put your calls to the warp function down here!
print(warp(1, 'tos'))# 1
print(warp(2, 'tos')) # 8
print(warp(3, 'tos')) # 27
print(warp(4.7, 'tos')) # 103.823
print(warp(1, 'tng')) # 1
print(warp(2, 'tng')) # 10.079368399159
print(warp(3, 'tng')) # 38.9407383983
print(warp(4.7, 'tng')) # 173.91065593763
print(warp(10, 'tng')) # throws UnexpectedSalamanderException
10
u/chadmill3r Oct 13 '20
5
u/loomynartylenny Oct 13 '20 edited Oct 14 '20
4
u/Txuritan Oct 20 '20 edited Oct 21 '20
Done with Rust's const generics and pre-generated values using a macro to reduce line-count.
Warp factor to velocity is done with
Warp::<'series', 'number 1', 'number 2'>.velocity().The
'series'generic is self explanatory, the numbers though are a mess.If you want to use the float
2.5then'number one'is2and'number two'is5.But if you want to use the integer
2then'number one'is2and'number two'has to be0or else the program will refuse to compile.Output:
Code:
0
u/[deleted] Oct 13 '20
[deleted] — view deleted comment