ASKSAGE: Sage Q&A Forum - Latest question feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Sat, 18 Mar 2017 13:14:33 -0500Priniting Horizontally??https://ask.sagemath.org/question/36978/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[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!sssageeeeSat, 18 Mar 2017 13:14:33 -0500https://ask.sagemath.org/question/36978/