# Priniting Horizontally??

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
l2=L2
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(,))-(7+calculator(,)

=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(,))-(7+calculator(,)

=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 close merge delete

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!

Sort by » oldest newest most voted 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
l2=L2
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 = ; L2 = 
print 'calculator({},{})'.format(L1,L2)
calculator(L1,L2)

more

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!

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

more

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!