Ask Your Question
0

How to serially number the output ?

asked 2022-08-19 04:59:03 +0100

sgmth gravatar image

I want the output of the following code to be serially numbered as

(1)A combination is-------

(2)A combination is -------

(3)A combination is-------

and so on.

The code is :

# Write your matrix

H = matrix(2,2, [1, 1,1, -1])     

n = 2   # Enter the order of the matrix H 

V = GF(2)^(n^2)               

R1=V.list()[1:]                    
 #We are removing the all-zero list because not needed for our purpose

R2=R1[:-1]                          
 #We are removing the all-ones list because not needed for our purpose

A = []
for v in  R2:
    A.append(matrix(ZZ,n,n,v))          
 # Forming n by n matrices from  list in  R2. Contains all 0,1 matrices.                                               

B=[]
for a in A:                       
     b=-a                                           # Forming n by n matrices from A by taking their negative. Contains all 0,-1 matrices    
     B.append(b)

C = []
for a in A:
    C.append(a)
for b in B:
    C.append(b)         #Union of A and B

test_sum = 4

test_comb = Combinations(C,test_sum)  
 # Here we are forming all "test_sum"-set combinations  from C".  
 # That is, if test_sum =3, then test_comb, gives all 3 set combinations  from C" 

for t in test_comb:
    if (sum(t)==H):                 
        #Checking which combinations in  test_sum give the  sum = H and printing those

        print("A combination is: ")    
        show(t)                     
        print("\n")
edit retag flag offensive close merge delete

Comments

Just introduce a counter that will increment with every printed combination.

Max Alekseyev gravatar imageMax Alekseyev ( 2022-08-19 05:53:54 +0100 )edit

Thanks a lot. I didn't know about the "Counter" thing. Saw on internet about it and applied it. Thanks a lot.

sgmth gravatar imagesgmth ( 2022-08-19 08:58:18 +0100 )edit

@max-alekseyev/ : could you turn your comment into an answer, in order to help future perusers of ask.sagemath.org with a similar question ?

Also : why a "-1" to the question itself ? I's answer is not trivial for Sage (or Python) newbies...

Emmanuel Charpentier gravatar imageEmmanuel Charpentier ( 2022-08-20 13:19:34 +0100 )edit

1 Answer

Sort by » oldest newest most voted
0

answered 2022-08-24 15:16:36 +0100

Sébastien gravatar image

One may use enumerate and string formating:

for (i,t) in enumerate(test_comb, start=1):
    if (sum(t)==H):                 
        #Checking which combinations in  test_sum give the  sum = H and printing those
        print("({}) A combination is: {}\n".format(i, t))
edit flag offensive delete link more

Comments

Numbering here will not be consecutive as some values of i will be skipped when the condition sum(t)==H fails.

Max Alekseyev gravatar imageMax Alekseyev ( 2022-08-24 16:26:26 +0100 )edit

oups, you are right!

Sébastien gravatar imageSébastien ( 2022-08-24 18:06:58 +0100 )edit

As I have mentioned in my previous two comments, I have taken M as follows:`

                                  M = matrix(2, 2, [1, 1, 1, -1])`
sgmth gravatar imagesgmth ( 2023-01-05 13:37:17 +0100 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower

Stats

Asked: 2022-08-19 04:59:03 +0100

Seen: 245 times

Last updated: Aug 24 '22