Display a linear combination: Difference between revisions

m (Promote to task, lots of examples, little controversy)
Line 1,271:
-e1
</pre>
 
=={{header|Ruby}}==
{{trans|D}}
<lang ruby>def linearCombo(c)
sb = ""
c.each_with_index { |n, i|
if n == 0 then
next
end
if n < 0 then
if sb.length == 0 then
op = "-"
else
op = " - "
end
elsif n > 0 then
if sb.length > 0 then
op = " + "
else
op = ""
end
else
op = ""
end
av = n.abs()
if av != 1 then
coeff = "%d*" % [av]
else
coeff = ""
end
sb = sb + "%s%se(%d)" % [op, coeff, i + 1]
}
if sb.length == 0 then
return "0"
end
return sb
end
 
def main
combos = [
[1, 2, 3],
[0, 1, 2, 3],
[1, 0, 3, 4],
[1, 2, 0],
[0, 0, 0],
[0],
[1, 1, 1],
[-1, -1, -1],
[-1, -2, 0, -3],
[-1],
]
 
for c in combos do
print "%-15s -> %s\n" % [c, linearCombo(c)]
end
end
 
main()</lang>
{{out}}
<pre>[1, 2, 3] -> e(1) + 2*e(2) + 3*e(3)
[0, 1, 2, 3] -> e(2) + 2*e(3) + 3*e(4)
[1, 0, 3, 4] -> e(1) + 3*e(3) + 4*e(4)
[1, 2, 0] -> e(1) + 2*e(2)
[0, 0, 0] -> 0
[0] -> 0
[1, 1, 1] -> e(1) + e(2) + e(3)
[-1, -1, -1] -> -e(1) - e(2) - e(3)
[-1, -2, 0, -3] -> -e(1) - 2*e(2) - 3*e(4)
[-1] -> -e(1)</pre>
 
=={{header|Scala}}==
1,452

edits