Can this be improved? Or is there existing code/apps/etc. that do this?

jillnatale@Jills-MacBook-Air ~ % python3 getAveWorks-v1.py3 Enter numbers ito be averaged separated by commas: 050, 50.000, 100, .0, 198., 000302. initial array is: ['050', ' 50.000', ' 100', ' .0', ' 198.', ' 000302.'] adding dot to 050... --now it's 050. removing leading 0s from 050.... --now it's 50. theDotLocation for 50. is 2 theLen for 50. is 3 theScale for 50. is 0 theScaledNum is 50 theDotLocation for 50.000 is 2 removing trailing 0s from 50.000... --now it's 50.00 removing trailing 0s from 50.00... --now it's 50.0 removing trailing 0s from 50.0... --now it's 50. theLen for 50. is 3 theScale for 50. is 0 theScaledNum is 50 adding dot to 100... --now it's 100. theDotLocation for 100. is 3 theLen for 100. is 4 theScale for 100. is 0 theScaledNum is 100 theDotLocation for .0 is 0 removing trailing 0s from .0... --now it's . theLen for . is 1 theScale for . is 0 theScaledNum is 0 theDotLocation for 198. is 3 theLen for 198. is 4 theScale for 198. is 0 theScaledNum is 198 removing leading 0s from 000302.... --now it's 00302. removing leading 0s from 00302.... --now it's 0302. removing leading 0s from 0302.... --now it's 302. theDotLocation for 302. is 3 theLen for 302. is 4 theScale for 302. is 0 theScaledNum is 302 intermediate array is: [['50', 0], ['50', 0], ['100', 0], ['0', 0], ['198', 0], ['302', 0]] maxScale = 0 summing and scaling... --after summing and scaling: [['50', 0], ['50', 0], ['100', 0], ['0', 0], ['198', 0], ['302', 0]] sum is 700 getting sum's primes... found prime 2-- ...2 time(s), adding [2,2] found prime 5-- ...2 time(s), adding [5,2] found prime 7-- ...1 time(s), adding [7,1] --sumsPrimes: [[2, 2], [5, 2], [7, 1]] getting numberOfNumbers' primes... found prime 2-- ...1 time(s), adding [2,1] found prime 3-- ...1 time, adding [3,1] --sumsPrimes and numberOfNumbersPrimes are: [[2, 2], [5, 2], [7, 1]] [[2, 1], [3, 1]] Scale = 0 reducing... in outer while in outer while in outer while --after reducing, sumsPrimes and numberOfNumbersPrimes are: [[2, 1], [5, 2], [7, 1]] [[3, 1]] Scale = 0 denominator is not a product of only 2's and 5's ave = 116 2/3 jillnatale@Jills-MacBook-Air ~ % cat getAveMy4.py getAveWorks-v1.py3 import math input_string = input("Enter numbers ito be averaged separated by commas: ") numbers = input_string.split(",") print("initial array is:") print (numbers) todo sanity check input (e.g. no ", ," allowed, etc.) todo sanity check input (e.g. no "nn nn" (missing ",") allowed, etc.) todo sanity check input (e.g. no "l" (must be #s) allowed, etc.) todo sanity check input (e.g. allow "-", etc.) todo sanity check input (e.g. no "nn.nn.nn" allowed, etc.) numberOfNumbers = len(numbers) oar = [] maxScale = 0 for theNum in numbers: iar = [] theNum = theNum.replace(" ","") theDotLocation = theNum.find('.') if theDotLocation == -1: print("adding dot to %s..." % theNum) theNum = theNum + "." print("--now it's %s" % theNum) while theNum[0] == '0' and theNum != "0.": print("removing leading 0s from %s..." % theNum) theNum = theNum[1:] print("--now it's %s" % theNum) if theNum == ".": # ??? dead code? theNum = "0." theDotLocation = theNum.find('.') print("theDotLocation for %s is %d" % (theNum, theDotLocation)) i = len(theNum) - 1 while theNum[i] == '0': print("removing trailing 0s from %s..." % theNum) theNum = theNum[:-1] print("--now it's %s" % theNum) i -= 1 #todo handle .0 input theLen = len(theNum) print("theLen for %s is %d" % (theNum, theLen)) theScale = theLen - theDotLocation - 1 print("theScale for %s is %d" % (theNum, theScale)) theScaledNum = theNum.replace('.','') if theScaledNum == "": theScaledNum = "0" print("theScaledNum is %s" % theScaledNum) while theScaledNum[0] == '0' and theScaledNum != "0": print("removing leading 0s from %s..." % theScaledNum) theScaledNum = theScaledNum[1:] print("--now it's %s" % theScaledNum) if theScaledNum == "": # ??? dead code? theScaledNum = "0" iar.append(theScaledNum) iar.append(theScale) if(maxScale < theScale): print("changing maxScale from %d..." % maxScale) maxScale = theScale print("--to %d" % maxScale) oar.append(iar) print("intermediate array is:") print(oar) print("maxScale = %d" % maxScale) print("summing and scaling...") sum = 0 for ar in oar: if ar[1] < maxScale: for n in range(maxScale - ar[1]): #print(ar) #print(n) ar[0] += "0" ar[1] += 1 #print(ar) sum += int(ar[0]) print("--after summing and scaling:") print(oar) print("sum is %d" % sum) print("getting sum's primes...") sumsPrimes = [] n = sum if n % 2 == 0: print("found prime 2--") hits = 0 while n % 2 == 0: hits += 1 n = n // 2 print("...%

Jan 14, 2025 - 20:17
Can this be improved? Or is there existing code/apps/etc. that do this?

jillnatale@Jills-MacBook-Air ~ % python3 getAveWorks-v1.py3

Enter numbers ito be averaged separated by commas: 050, 50.000, 100, .0, 198., 000302.
initial array is:
['050', ' 50.000', ' 100', ' .0', ' 198.', ' 000302.']
adding dot to 050...
--now it's 050.
removing leading 0s from 050....
--now it's 50.
theDotLocation for 50. is 2
theLen for 50. is 3
theScale for 50. is 0
theScaledNum is 50
theDotLocation for 50.000 is 2
removing trailing 0s from 50.000...
--now it's 50.00
removing trailing 0s from 50.00...
--now it's 50.0
removing trailing 0s from 50.0...
--now it's 50.
theLen for 50. is 3
theScale for 50. is 0
theScaledNum is 50
adding dot to 100...
--now it's 100.
theDotLocation for 100. is 3
theLen for 100. is 4
theScale for 100. is 0
theScaledNum is 100
theDotLocation for .0 is 0
removing trailing 0s from .0...
--now it's .
theLen for . is 1
theScale for . is 0
theScaledNum is 0
theDotLocation for 198. is 3
theLen for 198. is 4
theScale for 198. is 0
theScaledNum is 198
removing leading 0s from 000302....
--now it's 00302.
removing leading 0s from 00302....
--now it's 0302.
removing leading 0s from 0302....
--now it's 302.
theDotLocation for 302. is 3
theLen for 302. is 4
theScale for 302. is 0
theScaledNum is 302
intermediate array is:
[['50', 0], ['50', 0], ['100', 0], ['0', 0], ['198', 0], ['302', 0]]
maxScale = 0
summing and scaling...
--after summing and scaling:
[['50', 0], ['50', 0], ['100', 0], ['0', 0], ['198', 0], ['302', 0]]
sum is 700
getting sum's primes...
found prime 2--
...2 time(s), adding [2,2]
found prime 5--
...2 time(s), adding [5,2]
found prime 7--
...1 time(s), adding [7,1]
--sumsPrimes:
[[2, 2], [5, 2], [7, 1]]
getting numberOfNumbers' primes...
found prime 2--
...1 time(s), adding [2,1]
found prime 3--
...1 time, adding [3,1]
--sumsPrimes and
numberOfNumbersPrimes are:
[[2, 2], [5, 2], [7, 1]]
[[2, 1], [3, 1]]
Scale = 0
reducing...
in outer while
in outer while
in outer while
--after reducing,
sumsPrimes and
numberOfNumbersPrimes are:
[[2, 1], [5, 2], [7, 1]]
[[3, 1]]
Scale = 0
denominator is not a product of only 2's and 5's
ave = 116 2/3
jillnatale@Jills-MacBook-Air ~ % cat getAveMy4.py getAveWorks-v1.py3
import math
input_string = input("Enter numbers ito be averaged separated by commas: ")
numbers = input_string.split(",")
print("initial array is:")
print (numbers)

todo sanity check input (e.g. no ", ," allowed, etc.)

todo sanity check input (e.g. no "nn nn" (missing ",") allowed, etc.)

todo sanity check input (e.g. no "l" (must be #s) allowed, etc.)

todo sanity check input (e.g. allow "-", etc.)

todo sanity check input (e.g. no "nn.nn.nn" allowed, etc.)

numberOfNumbers = len(numbers)
oar = []
maxScale = 0
for theNum in numbers:
iar = []
theNum = theNum.replace(" ","")
theDotLocation = theNum.find('.')
if theDotLocation == -1:
print("adding dot to %s..." % theNum)
theNum = theNum + "."
print("--now it's %s" % theNum)
while theNum[0] == '0' and theNum != "0.":
print("removing leading 0s from %s..." % theNum)
theNum = theNum[1:]
print("--now it's %s" % theNum)
if theNum == ".":
# ??? dead code?
theNum = "0."
theDotLocation = theNum.find('.')
print("theDotLocation for %s is %d" % (theNum, theDotLocation))
i = len(theNum) - 1
while theNum[i] == '0':
print("removing trailing 0s from %s..." % theNum)
theNum = theNum[:-1]
print("--now it's %s" % theNum)
i -= 1
#todo handle .0 input
theLen = len(theNum)
print("theLen for %s is %d" % (theNum, theLen))
theScale = theLen - theDotLocation - 1
print("theScale for %s is %d" % (theNum, theScale))
theScaledNum = theNum.replace('.','')
if theScaledNum == "":
theScaledNum = "0"
print("theScaledNum is %s" % theScaledNum)
while theScaledNum[0] == '0' and theScaledNum != "0":
print("removing leading 0s from %s..." % theScaledNum)
theScaledNum = theScaledNum[1:]
print("--now it's %s" % theScaledNum)
if theScaledNum == "":
# ??? dead code?
theScaledNum = "0"
iar.append(theScaledNum)
iar.append(theScale)
if(maxScale < theScale):
print("changing maxScale from %d..." % maxScale)
maxScale = theScale
print("--to %d" % maxScale)
oar.append(iar)
print("intermediate array is:")
print(oar)
print("maxScale = %d" % maxScale)
print("summing and scaling...")
sum = 0
for ar in oar:
if ar[1] < maxScale:
for n in range(maxScale - ar[1]):
#print(ar)
#print(n)
ar[0] += "0"
ar[1] += 1
#print(ar)
sum += int(ar[0])
print("--after summing and scaling:")
print(oar)
print("sum is %d" % sum)
print("getting sum's primes...")
sumsPrimes = []
n = sum
if n % 2 == 0:
print("found prime 2--")
hits = 0
while n % 2 == 0:
hits += 1
n = n // 2
print("...%d time(s), adding [2,%d]" % (hits, hits))
sumsPrimes.append([2,hits])
for i in range(3,int(math.sqrt(n))+1,2):
if n % i == 0:
print("found prime %d--" % i)
hits = 0
while n % i == 0:
hits += 1
n = n // i
print("...%d time(s), adding [%d,%d]" % (hits, i, hits))
sumsPrimes.append([i,hits])
if n > 2:
print("found prime %d--" % n)
print("...1 time, adding [%d,1]" % n)
sumsPrimes.append([n,1])
print("--sumsPrimes:")
print(sumsPrimes)
print("getting numberOfNumbers' primes...")
numberOfNumbersPrimes = []
n = numberOfNumbers
if n % 2 == 0:
print("found prime 2--")
hits = 0
while n % 2 == 0:
hits += 1
n = n // 2
print("...%d time(s), adding [2,%d]" % (hits, hits))
numberOfNumbersPrimes.append([2,hits])
for i in range(3,int(math.sqrt(n))+1,2):
if n % i == 0:
print("found prime %d--" % i)
hits = 0
while n % i == 0:
hits += 1
n = n // i
print("...%d time(s), adding [%d,%d]" % (hits, i, hits))
numberOfNumbersPrimes.append([i,hits])
print("found prime %d--" % n)
if n > 2:
print("...1 time, adding [%d,1]" % n)
numberOfNumbersPrimes.append([n,1])
print("--sumsPrimes and")
print(" numberOfNumbersPrimes are:")
print(sumsPrimes)
print(numberOfNumbersPrimes)
print("Scale = %d" % maxScale)
print("reducing...")
n = 0
while(len(sumsPrimes) > n):
print("in outer while")
m = 0
while(len(numberOfNumbersPrimes) > m):
if(sumsPrimes[n][0] == numberOfNumbersPrimes[m][0]):
if(sumsPrimes[n][1] == numberOfNumbersPrimes[m][1]):
val = sumsPrimes.pop(n)
n -= 1
val = numberOfNumbersPrimes.pop(m)
m -= 1
break
elif(sumsPrimes[n][1] > numberOfNumbersPrimes[m][1]):
sumsPrimes[n][1] -= numberOfNumbersPrimes[m][1]
val = numberOfNumbersPrimes.pop(m)
m -= 1
break
elif(sumsPrimes[n][1] < numberOfNumbersPrimes[m][1]):
numberOfNumbersPrimes[m][1] -= sumsPrimes[n][1]
val = sumsPrimes.pop(n)
n -= 1
m += 1
n += 1
print("--after reducing,")
print("sumsPrimes and")
print("numberOfNumbersPrimes are:")
print(sumsPrimes)
print(numberOfNumbersPrimes)
print("Scale = %d" % maxScale)
allTwosOrFives = True
for ar in numberOfNumbersPrimes:
if ar[0] != 2 and ar[0] != 5:
allTwosOrFives = False
break

todo handle "0" ???

if allTwosOrFives == True:
print("sum = %d" % sum)
print("numberOfNumbers = %d" % numberOfNumbers)
print("maxScale = %d" % maxScale)
print("sum/numberOfNumbers = %d" % (sum/numberOfNumbers))
print("10*maxScale = %d" % 10-maxScale)
res = sum/numberOfNumbers * 10
-maxScale
print("ave = %f" % res)
else:
print("denominator is not a product of only 2's and 5's")
N = 1
for ar in sumsPrimes:
N *= ar[0]
ar[1]
D = 1
for ar in numberOfNumbersPrimes:
D *= ar[0]
*ar[1]
intRes = int(N/D)
remRes = N%D
print("ave = %d %d/%d" % (intRes, remRes, D))
import math
input_string = input("Enter numbers ito be averaged separated by commas: ")
numbers = input_string.split(",")
print("initial array is:")
print (numbers)

todo sanity check input (e.g. no ", ," allowed, etc.)

todo sanity check input (e.g. no "nn nn" (missing ",") allowed, etc.)

todo sanity check input (e.g. no "l" (must be #s) allowed, etc.)

todo sanity check input (e.g. allow "-", etc.)

todo sanity check input (e.g. no "nn.nn.nn" allowed, etc.)

numberOfNumbers = len(numbers)
oar = []
maxScale = 0
for theNum in numbers:
iar = []
theNum = theNum.replace(" ","")
theDotLocation = theNum.find('.')
if theDotLocation == -1:
print("adding dot to %s..." % theNum)
theNum = theNum + "."
print("--now it's %s" % theNum)
while theNum[0] == '0' and theNum != "0.":
print("removing leading 0s from %s..." % theNum)
theNum = theNum[1:]
print("--now it's %s" % theNum)
if theNum == ".":
# ??? dead code?
theNum = "0."
theDotLocation = theNum.find('.')
print("theDotLocation for %s is %d" % (theNum, theDotLocation))
i = len(theNum) - 1
while theNum[i] == '0':
print("removing trailing 0s from %s..." % theNum)
theNum = theNum[:-1]
print("--now it's %s" % theNum)
i -= 1
#todo handle .0 input
theLen = len(theNum)
print("theLen for %s is %d" % (theNum, theLen))
theScale = theLen - theDotLocation - 1
print("theScale for %s is %d" % (theNum, theScale))
theScaledNum = theNum.replace('.','')
if theScaledNum == "":
theScaledNum = "0"
print("theScaledNum is %s" % theScaledNum)
while theScaledNum[0] == '0' and theScaledNum != "0":
print("removing leading 0s from %s..." % theScaledNum)
theScaledNum = theScaledNum[1:]
print("--now it's %s" % theScaledNum)
if theScaledNum == "":
# ??? dead code?
theScaledNum = "0"
iar.append(theScaledNum)
iar.append(theScale)
if(maxScale < theScale):
print("changing maxScale from %d..." % maxScale)
maxScale = theScale
print("--to %d" % maxScale)
oar.append(iar)
print("intermediate array is:")
print(oar)
print("maxScale = %d" % maxScale)
print("summing and scaling...")
sum = 0
for ar in oar:
if ar[1] < maxScale:
for n in range(maxScale - ar[1]):
#print(ar)
#print(n)
ar[0] += "0"
ar[1] += 1
#print(ar)
sum += int(ar[0])
print("--after summing and scaling:")
print(oar)
print("sum is %d" % sum)
print("getting sum's primes...")
sumsPrimes = []
n = sum
if n % 2 == 0:
print("found prime 2--")
hits = 0
while n % 2 == 0:
hits += 1
n = n // 2
print("...%d time(s), adding [2,%d]" % (hits, hits))
sumsPrimes.append([2,hits])
for i in range(3,int(math.sqrt(n))+1,2):
if n % i == 0:
print("found prime %d--" % i)
hits = 0
while n % i == 0:
hits += 1
n = n // i
print("...%d time(s), adding [%d,%d]" % (hits, i, hits))
sumsPrimes.append([i,hits])
if n > 2:
print("found prime %d--" % n)
print("...1 time, adding [%d,1]" % n)
sumsPrimes.append([n,1])
print("--sumsPrimes:")
print(sumsPrimes)
print("getting numberOfNumbers' primes...")
numberOfNumbersPrimes = []
n = numberOfNumbers
if n % 2 == 0:
print("found prime 2--")
hits = 0
while n % 2 == 0:
hits += 1
n = n // 2
print("...%d time(s), adding [2,%d]" % (hits, hits))
numberOfNumbersPrimes.append([2,hits])
for i in range(3,int(math.sqrt(n))+1,2):
if n % i == 0:
print("found prime %d--" % i)
hits = 0
while n % i == 0:
hits += 1
n = n // i
print("...%d time(s), adding [%d,%d]" % (hits, i, hits))
numberOfNumbersPrimes.append([i,hits])
print("found prime %d--" % n)
if n > 2:
print("...1 time, adding [%d,1]" % n)
numberOfNumbersPrimes.append([n,1])
print("--sumsPrimes and")
print(" numberOfNumbersPrimes are:")
print(sumsPrimes)
print(numberOfNumbersPrimes)
print("Scale = %d" % maxScale)
print("reducing...")
n = 0
while(len(sumsPrimes) > n):
print("in outer while")
m = 0
while(len(numberOfNumbersPrimes) > m):
if(sumsPrimes[n][0] == numberOfNumbersPrimes[m][0]):
if(sumsPrimes[n][1] == numberOfNumbersPrimes[m][1]):
val = sumsPrimes.pop(n)
n -= 1
val = numberOfNumbersPrimes.pop(m)
m -= 1
break
elif(sumsPrimes[n][1] > numberOfNumbersPrimes[m][1]):
sumsPrimes[n][1] -= numberOfNumbersPrimes[m][1]
val = numberOfNumbersPrimes.pop(m)
m -= 1
break
elif(sumsPrimes[n][1] < numberOfNumbersPrimes[m][1]):
numberOfNumbersPrimes[m][1] -= sumsPrimes[n][1]
val = sumsPrimes.pop(n)
n -= 1
m += 1
n += 1
print("--after reducing,")
print("sumsPrimes and")
print("numberOfNumbersPrimes are:")
print(sumsPrimes)
print(numberOfNumbersPrimes)
print("Scale = %d" % maxScale)
allTwosOrFives = True
for ar in numberOfNumbersPrimes:
if ar[0] != 2 and ar[0] != 5:
allTwosOrFives = False
break

todo handle "0" ???

if allTwosOrFives == True:
print("sum = %d" % sum)
print("numberOfNumbers = %d" % numberOfNumbers)
print("maxScale = %d" % maxScale)
print("sum/numberOfNumbers = %d" % (sum/numberOfNumbers))
print("10*maxScale = %d" % 10-maxScale)
res = sum/numberOfNumbers * 10
-maxScale
print("ave = %f" % res)
else:
print("denominator is not a product of only 2's and 5's")
N = 1
for ar in sumsPrimes:
N *= ar[0]
ar[1]
D = 1
for ar in numberOfNumbersPrimes:
D *= ar[0]
*ar[1]
intRes = int(N/D)
remRes = N%D
print("ave = %d %d/%d" % (intRes, remRes, D))
jillnatale@Jills-MacBook-Air ~ %