axpgn ha scritto:Feedback: non funziona
Cordialmente, Alex
Aglia! Nel senso che calcola l'output sbagliato o ti da qualche errore di esecuzione?
axpgn ha scritto:Feedback: non funziona
Cordialmente, Alex
#!/usr/bin/python
import sys
import os
DEBUG = 0 # 0 = none, 1 = verbose, 2 = debug
class Terna:
terna = {}
string = ""
def __init__(self, n1,n2,n3):
try:
values = [int(n1),int(n2),int(n3)]
self.string = str(n1)+"+"+str(n2)+"+"+str(n3)
values.sort()
if DEBUG > 1: print(str(values))
values_dict = {}
for i in xrange(0,3):
value = values[i]
if(not value in values_dict):
times = values.count(value)
values_dict[value] = times
self.terna = values_dict
except:
print("Error: worng Terna constructor!")
exit(1)
def equivalentTo(self, AltraTerna):
res = True
for val in self.terna:
try:
if self.terna[val] != AltraTerna.terna[val]:
res = False
except:
res = False
return res
class TreNumeri:
permutations = { "cardinality":0, "set": [ Terna(0,0,0) ] }
unique_perms = { "cardinality":0, "set": [ Terna(0,0,0) ] }
n = 0
def __init__(self, number):
if(number==0):
print("Storing n=0!")
self.n = int(number)
self.permutations = self.getPermutations()
self.unique_perms = self.getDiffTernas()
self.printResponso()
def printResponso(self):
uniqueTernasOutput = ""
temp = 0
for uT in self.unique_perms["set"]:
temp += 1
uniqueTernasOutput += "["+uT.string+"] "
if temp == 5:
temp = 0
uniqueTernasOutput += "\n# "
if DEBUG>0: print("#########################################")
print("# \tn = "+str(self.n)+" --> "+str(self.unique_perms["cardinality"]))
if DEBUG>0:
print("#########################################")
print("# We've found "+str(self.unique_perms["cardinality"])+" unique sets of three positives")
print("# numbers such that their sum is "+str(self.n)+":")
print("#########################################")
print("# "+uniqueTernasOutput)
print("#######################################")
def getPermutations(self):
# Generating and storing all possible permutations of integers such that their sum is self.n
n = self.n
permutations = []
for x in xrange(0,n):
permutations.append( Terna(n-x,x,0) )
for y in xrange(0,x):
permutations.append( Terna(n-x, x-y, y) )
if DEBUG>0:
outPrint = ""
for te in permutations: outPrint += "["+te.string+"] "
print("We've "+str(len(permutations))+" permutations:\n"+outPrint)
return { "cardinality": len(permutations), "set" : permutations }
def getDiffTernas(self):
# Storing a new permutations set and cleaning it from equivalents ternas
res_set = list(self.permutations["set"])
for t1 in self.permutations["set"]:
t1_has_been_checked = False
for t2 in res_set:
if t1.string == t2.string or t1.equivalentTo(t2):
if t1_has_been_checked:
res_set.pop( res_set.index(t2) )
else:
t1_has_been_checked = True
if DEBUG>0:
outPrint = ""
for te in res_set: outPrint += "["+te.string+"] "
print("We've "+str(len(res_set))+" unique permutations:\n"+outPrint)
return { "cardinality": len(res_set), "set" : res_set }
class main:
name = ""
arguments = []
def __init__(self, argv):
self.executeOverUserInput(argv)
def executeOverUserInput(self,argv):
self.evaluateArguments(argv)
res = TreNumeri(self.arguments[0])
def testExecution(self):
for i in xrange(0,20): TreNumeri(i)
for i in xrange(1,3): TreNumeri(21**i)
def evaluateArguments(self,argv):
self.name = argv[0]
if(len(argv)<2):
print("Error: excepting a second argument!")
exit(1)
try:
int(argv[1])
except:
if(float(argv[1])): print("The result is 0.")
else: print("Error: excepting a number as argument!")
exit(1)
self.arguments = argv[1:]
if(len(argv)>2): DEBUG = 1
main(sys.argv)
Erich ha scritto:In effetti mi hai ben indirizzato per trovare l'errore, ...
Erich ha scritto:Ecco il codice, adesso sicuramente più comprensibile:
axpgn ha scritto:Bel lavoro, really!
Purtroppo non serve a niente … ovviamente ciò che chiedo è altro
axpgn ha scritto:Erich ha scritto:In effetti mi hai ben indirizzato per trovare l'errore, ...
Ero molto bravo a trovare i bug degli altri ... ho pure fatto in tempo a notare che avevi scritto "per $n=6$"
axpgn ha scritto:Giusto per curiosità ( ) , quanto ti ci vorrebbe per trovare la risposta a
$n=12345678901234567890123456789012345678901234567890123456789012345678901234567890$ ?
(sono $80$ cifre, erano $100$ ma non me le scrive tutte )
Cordialmente, Alex
axpgn ha scritto:Uellà che analisi! … ma sei già laureato o ancora studi?
axpgn ha scritto:Un'ultima domanda (se posso permettermi): lo hai fatto per divertimento o anche come esercitazione?
Erich ha scritto:Studio ancora, non saprei se purtroppo o per fortuna!
Visitano il forum: Nessuno e 1 ospite