@@ -238,12 +238,15 @@ def evalAlgorithm(newFormula, formula, binary):
238238 # We not need to use eval(), We can do this with an algorithm, It's not hard!
239239 return eval (newFormula )
240240
241- def evalFormula (formula , binaries ):
242- print ("formula:" , formula )
241+ def evalFormula (formula , binaries , debug ):
242+ if debug :
243+ print ("formula:" , formula )
243244 # print(binaries)
244- print ("Solving g for binaries:" )
245+ if debug :
246+ print ("Solving g for binaries:" )
245247 results = []
246- print (binaries )
248+ if debug :
249+ print (binaries )
247250 for binary in binaries :
248251 newFormula = formula
249252 i = 0
@@ -264,7 +267,8 @@ def evalFormula(formula, binaries):
264267
265268 calcFormula = evalAlgorithm (newFormula , formula , binary )
266269 calcFormula = 1 if calcFormula >= 1 else 0
267- print ("\t " , binary , formula , newFormula , calcFormula )
270+ if debug :
271+ print ("\t " , binary , formula , newFormula , calcFormula )
268272 if calcFormula == 1 :
269273 results .append (binary )
270274 return results
@@ -310,6 +314,7 @@ def removeDublicateValue(array):
310314def commonValues (array ):
311315 # if array != None and len(array) != 0:
312316 # print(array)
317+ result = []
313318 for x in array [0 ]:
314319 # print("x", x)
315320 tc = 0
@@ -330,14 +335,18 @@ def commonValues(array):
330335 # print()
331336
332337 if tc > 0 :
333- return x
338+ result . append ( x )
334339 # print("yes", c)
335340 # else:
336341 # print("no")
337342 # print("----------")
338-
343+ if len (result ) > 0 :
344+ return result
339345 return None
340346
347+ # t=[['y', 'x', 'z'],['y', 'x', "z'"]]
348+ # print( commonValues(t) )
349+
341350def removeValue (array , values ):
342351 result = []
343352 for item in array :
@@ -377,115 +386,173 @@ def filterGArray(g):
377386 g .remove (g [1 ])
378387 return g
379388
380- # number="156"
381- # number="0367"
382- # number="45"
383- # number="145"
384- # number="457"
385-
386- # number="1357"
387- number = "0246"
388- number = "0167"
389- number = "1247"
390- number = "25"
391- print ("Enter number: " )
392- number = input ()
393389countChild = 3
394- numberLength = len (number )
395390alphas = string .ascii_lowercase
396391alphas = alphas [- 3 :] + alphas [0 :- 3 ]
397392alphas = alphas [0 :countChild ]
398393alphasLength = len (alphas )
399394binaries = None
400395formulas = None
401396
402- print ("Number: " , number )
403- print ("NumberLength: " , numberLength )
404-
405- if not isOcta (number ):
406- print ("Error: Number not valid!" )
407- sys .exit (- 1 )
408-
409- if charsRepeated (number ):
410- print ("Error: Number not valid, You cannot use dublicate digit in your number!" )
411- sys .exit (- 1 )
412-
413- print ("Alpha is :" , alphas )
414- print ("AlphaCount is :" , alphasLength )
415-
416- binaries = charsToBin (number )
417- if binaries == None :
418- print ("Error: binaries is None!" )
419- sys .exit (- 1 )
420- binariesLength = len (binaries )
421- print ("Binaries: " , binaries )
422- print ("BinariesLength: " , binariesLength )
423-
424- formulas = binsToFormula (binaries )
425- if formulas == None :
426- print ("Error: formulas is None!" )
427- sys .exit (- 1 )
428-
429- print ("Formulas: " , formulas )
430-
431- subscriptions = []
432-
433- # loop 0 until numberLength-1
434- for x in range (0 , numberLength ):
435- # loop x+1 until numberLength-1
436- for y in range (x + 1 , numberLength ):
437- print ("[ " ,x + 1 ,"th <-> " ,y + 1 ,"th ] : {" , formulas [x ] , ", " , formulas [y ], "}" )
438- subscription = subscriptionOfFormulas (formulas [x ], formulas [y ])
439- print (" Subscription: " , subscription )
440- subscriptions .append (subscription )
441-
442- print ("Subscriptions: " , subscriptions )
443- subscriptionsFilter = subscriptionsFilter (subscriptions )
444- print ("SubscriptionsAll: " , subscriptionsFilter )
445- if subscriptionsFilter == None or len (subscriptionsFilter ) == 0 :
446- g = binaries
447- print ("g: " , g )
448- print ("gFormula: " , binsToFormula (g ))
449- else :
450- g = subscriptionsFilter
451- commonSubscriptions = commonValues (g )
452- print ("commonSubscriptions: " , commonSubscriptions )
453- if commonSubscriptions != None :
454- print ("newG did changed using removeValue()..." )
455- newG = [commonSubscriptions , [removeValue (g , commonSubscriptions )]]
456- print ("newG: " , newG )
457- newG = filterGArray (newG )
458- print ("newGFilter: " , newG )
459- else :
460- newG = g
461- print ("newG: " , newG )
462-
463- gFormula = toFormula (newG )
464- print ("newGFormula: " , gFormula ) #It's $g formula
465-
466- gValues = evalFormula (gFormula , binaries )
467- gValuesCount = len (gValues )
468- print ("Compare gValuesCount with binariesLength: " , gValuesCount , "??" , binariesLength )
469-
470- if gValuesCount == binariesLength :
471- print ("\t They are equal!" )
472- # print("-->", newG)
473- # print("-->", formulasToBin(newG))
474- # g=formulasToBin(newG)
475- g = (newG )
476- elif gValuesCount < binariesLength :
477- print ("\t binariesLength is bigger then gValuesCount." )
478- g = newG
479- print ("Diff two array:" )
480- print ("\t subscriptions1: " , binaries )
481- print ("\t subscriptions2: " , gValues )
482- nonSubscribersAll = nonSubscribers (binaries , gValues )
483- print ("NonSubscribers: " , nonSubscribersAll )
484- nonSubscribersAllFormula = binsToFormula (nonSubscribersAll )
485- print ("nonSubscribersAllFormula: " , nonSubscribersAllFormula )
486- g = g + nonSubscribersAllFormula
487- else :
488- print ("Error: binariesLength is more then gValuesCount and we not except it!" )
397+ def run (number , debug ):
398+ numberLength = len (number )
399+ if debug :
400+ print ("Number: " , number )
401+ print ("NumberLength: " , numberLength )
402+
403+ if not isOcta (number ):
404+ if debug :
405+ print ("Error: Number not valid!" )
406+ sys .exit (- 1 )
407+
408+ if charsRepeated (number ):
409+ if debug :
410+ print ("Error: Number not valid, You cannot use dublicate digit in your number!" )
411+ sys .exit (- 1 )
412+
413+ if debug :
414+ print ("Alpha is :" , alphas )
415+ print ("AlphaCount is :" , alphasLength )
416+
417+ binaries = charsToBin (number )
418+ if binaries == None :
419+ if debug :
420+ print ("Error: binaries is None!" )
421+ sys .exit (- 1 )
422+ binariesLength = len (binaries )
423+ if debug :
424+ print ("Binaries: " , binaries )
425+ print ("BinariesLength: " , binariesLength )
426+
427+ formulas = binsToFormula (binaries )
428+ if formulas == None :
429+ if debug :
430+ print ("Error: formulas is None!" )
489431 sys .exit (- 1 )
490432
491- print ("graph: " , g )
433+ if debug :
434+ print ("Formulas: " , formulas )
435+
436+ subscriptions = []
437+
438+ # loop 0 until numberLength-1
439+ for x in range (0 , numberLength ):
440+ # loop x+1 until numberLength-1
441+ for y in range (x + 1 , numberLength ):
442+ if debug :
443+ print ("[ " ,x + 1 ,"th <-> " ,y + 1 ,"th ] : {" , formulas [x ] , ", " , formulas [y ], "}" )
444+ subscription = subscriptionOfFormulas (formulas [x ], formulas [y ])
445+ if debug :
446+ print (" Subscription: " , subscription )
447+ subscriptions .append (subscription )
448+
449+ if debug :
450+ print ("Subscriptions: " , subscriptions )
451+ subscriptionsFilters = subscriptionsFilter (subscriptions )
452+ if debug :
453+ print ("SubscriptionsAll: " , subscriptionsFilters )
454+ if subscriptionsFilters == None or len (subscriptionsFilters ) == 0 :
455+ g = binsToFormula (binaries )
456+ if debug :
457+ print ("g: " , g )
458+ print ("gFormula: " , binsToFormula (g ))
459+ else :
460+ g = subscriptionsFilters
461+ commonSubscriptions = commonValues (g )
462+ if debug :
463+ print ("commonSubscriptions: " , commonSubscriptions )
464+ if commonSubscriptions != None :
465+ if debug :
466+ print ("newG did changed using removeValue()..." )
467+ newG = [commonSubscriptions , [removeValue (g , commonSubscriptions )]]
468+ if debug :
469+ print ("newG: " , newG )
470+ newG = filterGArray (newG )
471+ if debug :
472+ print ("newGFilter: " , newG )
473+ else :
474+ newG = g
475+ if debug :
476+ print ("newG: " , newG )
477+
478+ gFormula = toFormula (newG )
479+ if debug :
480+ print ("newGFormula: " , gFormula ) #It's $g formula
481+
482+ gValues = evalFormula (gFormula , binaries , debug )
483+ gValuesCount = len (gValues )
484+ if debug :
485+ print ("Compare gValuesCount with binariesLength: " , gValuesCount , "??" , binariesLength )
486+
487+ if gValuesCount == binariesLength :
488+ if debug :
489+ print ("\t They are equal!" )
490+ # print("-->", newG)
491+ # print("-->", formulasToBin(newG))
492+ # g=formulasToBin(newG)
493+ g = (newG )
494+ elif gValuesCount < binariesLength :
495+ if debug :
496+ print ("\t binariesLength is bigger then gValuesCount." )
497+ g = newG
498+ if debug :
499+ print ("Diff two array:" )
500+ print ("\t subscriptions1: " , binaries )
501+ print ("\t subscriptions2: " , gValues )
502+ nonSubscribersAll = nonSubscribers (binaries , gValues )
503+ if debug :
504+ print ("NonSubscribers: " , nonSubscribersAll )
505+ nonSubscribersAllFormula = binsToFormula (nonSubscribersAll )
506+ if debug :
507+ print ("nonSubscribersAllFormula: " , nonSubscribersAllFormula )
508+ g = g + nonSubscribersAllFormula
509+ else :
510+ if debug :
511+ print ("Error: binariesLength is more then gValuesCount and we not except it!" )
512+ sys .exit (- 1 )
513+ if debug :
514+ print ("graph: " , g )
515+ return g
516+
517+ def tests ():
518+ results = []
519+ results .append (["1357" , [["z" ]]])
520+ results .append (["0246" , [["z'" ]]])
521+ results .append (["1247" , [["x'" ,"y'" ,"z" ],["x'" ,"y" ,"z" ],["x" ,"y'" ,"z'" ],["x" ,"y" ,"z" ]]])
522+ results .append (["0367" , [["x" ,"y" ],["x'" ,"y" ,"z" ],["x'" ,"y'" ,"z'" ]]])
523+ results .append (["2345" , [["x'" ,"y" ],["x" ,"y'" ]]])
524+ results .append (["0167" , [["x'" ,"y'" ],["x" ,"y" ]]])
525+ results .append (["3" , [["x'" ,"y" ,"z" ]]])
526+ results .append (["35" , [["x'" ,"y" ,"z" ],["x" ,"y'" ,"z" ]]])
527+ results .append (["67" , [["x" ,"y" ]]])
528+ results .append (["012" , [["x'" ,"y'" ],["x'" ,"y" ,"z'" ]]])
529+ results .append (["25" , [["x'" ,"y" ,"z'" ],["x" ,"y'" ,"z" ]]])
530+ i = 1
531+ for item in results :
532+ print ("Test " + str (i )+ ": " , run (item [0 ], False ) == item [1 ])
533+ i = i + 1
534+
535+ # number="156"
536+ # number="0367"
537+ # number="45"
538+ # number="145"
539+ # number="457"
540+
541+ # number="1357"
542+ # number="0246"
543+ # number="0167"
544+ # number="1247"
545+ # number="25"
546+ # print("Enter number: ")
547+ # number=input()
548+
549+ tests ();
550+ # print(run(input("Enter number:"), False))
551+ # print(run("1247", False))
552+
553+ # print("Excepted is: ", run("1247", True))
554+ # print("Excepted is: ", [["x'","y'","z"],["x'","y","z"],["x","y'","z'"],["x","y","z"]])
555+
556+ print ("Excepted is: " , run ("0367" , True ))
557+ print ("Excepted is: " , [["x" ,"y" ],["x'" ,"y" ,"z" ],["x'" ,"y'" ,"z'" ]])
558+
0 commit comments