-
Notifications
You must be signed in to change notification settings - Fork 0
/
bsub.py
144 lines (97 loc) · 3.23 KB
/
bsub.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
#!/usr/bin/python3
import sys, getopt
######################################################################
# reads in arguments
def main(argv):
number = ''
inputfile = ''
outputfile = ''
try:
opts, args = getopt.getopt(argv,"P:J:n:",["num=","ifile=","ofile="])
#print("Opts:", opts)
except getopt.GetoptError:
print ('test.py -n <number> -i <inputfile> -o <outputfile>')
sys.exit(2)
for opt, arg in opts:
if opt == '-h':
print ('test.py -n <number> -i <inputfile> -o <outputfile>')
sys.exit()
elif opt in ("-i", "--ifile"):
inputfile = arg
elif opt in ("-o", "--ofile"):
outputfile = arg
elif opt in ("-n", "--num"):
number = arg
print ('Input file is ', inputfile)
print ('Output file is ', outputfile)
print ('Number is ', number)
return number
######################################################################
# make the input string a number if possible
def make_number(x):
#print(x)
try:
x = int(x)
print ("Is an integer")
except ValueError as e:
print("Not an integer then. ")
# how about float
try:
x=float(x)
except ValueError as e:
print("Not a float either. Try inputting a number instead of ",x)
sys.exit()
return x
print("Let's get on")
######################################################################
# make float integer
def float_2_int(x):
# string split and multiply with the number of zeros
#words = text.split(",")
whole, small = str(x).split(".")
lens = int(len(str(small)))
zero = str("0")
multiplier = int(str(1)+lens*zero)
print(lens, multiplier)
newval = int(multiplier * x)
return newval,multiplier
######################################################################
def perfect_cube(x):
ans = 0
# step up ans by 1
while ans < (x/3)+0.5:
#print ans
if ans**3==x:
#print ("Perfect cube",ans,x)
return ans
else:
ans +=1
return "No perfect cube exists"
######################################################################
# allows you to both define a module and run it
if __name__ == "__main__":
number = main(sys.argv[1:])
######################################################################
######################################################################
# check the number is a number, and change float to val
number = make_number(number)
#print("Num:",number, type(number))
# if it is a float fix it, print results
if type(number)==float:
# make int
new, mult = float_2_int(number)
#print("Num:",number, type(number), new, mult)
#number=int(new)
res = perfect_cube(new)
if type(res)==str:
print ('\n',res, 'of', number )
else:
res = int(perfect_cube(new))/mult*100
print ('\nThe prefect cube of {0:.3f} is {1}'.format(res,number))
else:
#print("Is integer ",number)
res = perfect_cube(number)
if type(res)==str:
print ('\n',res, 'of', number )
else:
print ('\nThe prefect cube of {0} is {1}'.format(perfect_cube(number), number))