Ask Your Question
0

Priniting Horizontally??

asked 2017-03-18 19:18:05 +0100

sssageeee gravatar image

updated 2017-03-19 01:55:48 +0100

kcrisman gravatar image

Hello, guys! hope you are having a good day. First of all, I'm very new to sage and this forum, so would you please be generous to my question,, please?..

So, I was trying to do some recursive/iterative calculations as follows:

def calculator(L1,L2):
    c=0
    m=len(L1)
    n=len(L2)
    print '('
    if m==0:
        c=c+0
        print '+'
        print c
        print ')'
    elif m==1:
        l1=L1[0]
        l2=L2[0]
        if l1<l2:
            c=c+l1
            print '+'
            print c
            print ')'
        else:
            c=c+0
            print '+'
            print c
            print ')'
    else:
        for i in range(m):
            l1=L1[i]
            l2=L2[i]
            if l1<l2:
                c=c+l2
                print '+'
                print c
                print ')'
            else:
                L3=[L1[j] for j in range(i)]+[L1[j] for j in range(i+1,m)]
                L4=[L2[j] for j in range(i)]+[L2[j] for j in range(i+1,n)]
                c=c+(-1)^m*calculator(L3,L4)
                print '+'
                print c
                print ')'
    return c

And, if we calculate, for example,

L1=[5,7,3]; L2=[7,5,2]; calculator(L1,L2)

then we get: (I've intentionally wrote vertically in the below, because that is exactly the sage gives us as a result)

(

+

7

)

(

+

7

)

(

+

5

)

+

12

)

+

-5

)

(

+

7

)

(

+

5

)

+

12

)

+

-17

)

-17

BUT!, if we actually, calculate by hand, then we get:

calculator([5,7,3],[7,5,2])

=7-calculator([5,3],[7,2])-calculator([5,7],[7,5]

=7-(7+calculator([5],[7]))-(7+calculator([5],[7])

=7-(7+(5))-(7+(5))=-17

And I actually want 7-(7+(5))-(7+(5))=-17 as a result... Can any body help me how to solve this kind of problem,, please...

And, moreover, if it is possible to get the result like

"calculator([5,7,3],[7,5,2])

=7-calculator([5,3],[7,2])-calculator([5,7],[7,5]

=7-(7+calculator([5],[7]))-(7+calculator([5],[7])

=7-(7+(5))-(7+(5))=-17"

then, it would be much greater...!

Oh one last thing to note is that even though we get the vertical result as above, if we write down horizontally that result, we get

( + 7 ) ( + 7 ) ( + 5 ) + 12 ) + -5 ) ( + 7 ) ( + 5 ) + 12 ) + -17 ) -17

which shows us that there are some redundancy in the result and some parentheses are showing weirdly...

Can any body,, please help me how to solve this kind of problem... Thank you for help! and I hope you have a great day!!! I wish you the best luck for your future! Thank you!

edit retag flag offensive close merge delete

Comments

Welcome to Sage! Hope you feel welcome. In this case, it's that print statements by definition give new lines after each one. You'll want to make a string with each of the parts and then print that string at the end. Hopefully another person can give some details for you, unfortunately I only have time for this comment. Good luck!

kcrisman gravatar imagekcrisman ( 2017-03-18 21:53:39 +0100 )edit

2 Answers

Sort by ยป oldest newest most voted
2

answered 2017-03-19 11:25:59 +0100

ndomes gravatar image

I edited your code, using string formatting and indentation to get a proper output. More about string formatting see here.

def plusminus(x):
    return '+' if x >= 0 else '-'

def calculator(L1,L2,d=0):  
    # d   -  recursion depth counter, used for indentation    
    c=0
    m=len(L1)
    n=len(L2)
    if m==0:
        pass  
    elif m==1:
        l1=L1[0]
        l2=L2[0]
        if l1<l2:
            c=c+l1            
        print d*'          '+'c{} = {} '.format(d,c)    
    else:
        for i in range(m):
            l1=L1[i]
            l2=L2[i]
            if l1<l2:
                c=c+l2
                print d*'          '+'c{} = {} '.format(d,c)
            else:
                L3 = L1[0:i] + L1[i+1:m]   # in this case list slicing is shorter and more readable 
                L4 = L2[0:i] + L2[i+1:n]
                print d*'          '+'c{} = {} {} calculator({},{})'.format(d,c,plusminus((-1)^m),L3,L4)
                c2 = calculator(L3,L4,d+1)
                c1 = c  # copy unchanged c for output
                c=c+(-1)^m*c2
                print d*'          '+'c{} = {} {} {} = {}'.format(d,c1,plusminus((-1)^m),c2,c)
                print 
    return c

#L1=[5,7,3,8]; L2=[7,5,2,6] 
L1=[5,7,3]; L2=[7,5,2]
#L1=[5,7]; L2=[7,5] 
#L1 = [5]; L2 = [7]
print 'calculator({},{})'.format(L1,L2)
calculator(L1,L2)
edit flag offensive delete link more

Comments

Thank you soooo much ndomes! This answer is very awesomeeeee! You helped me very much a lot. Thank you very much again! I hope you have a great day! And wish you the best luck for your future!

sssageeee gravatar imagesssageeee ( 2017-03-19 20:34:43 +0100 )edit
1

answered 2017-03-19 10:27:29 +0100

tmonteil gravatar image

updated 2017-03-19 10:29:53 +0100

If you do not want to print a new line after each print command, just add a comma after the print command:

sage: print 1 ; print ')' ; print 3
1
)
3
sage: print 1, ; print ')', ; print 3,
1 ) 3

Note that it is Python2 specific, so that when Sage will be using Python 3, you will have to write print(my_string, end='')

That said, as suggested by @krismann, i would better suggest to construct a single string and return it at the end. You can concatenate strings as follows:

sage: a = ''
sage: a += '1'
sage: a += ')'
sage: a += '3'
sage: a
'1)3'
sage: print a
1)3
edit flag offensive delete link more

Comments

Thank you tmonteil. Your answer is also very helpful! I hope you have a nice day! and wish you the best luck for your future!

sssageeee gravatar imagesssageeee ( 2017-03-19 20:41:36 +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

2 followers

Stats

Asked: 2017-03-18 19:14:33 +0100

Seen: 935 times

Last updated: Mar 19 '17