Task: Display a linear combination



C#: using System;
using System.Collections.Generic;
using System.Text;

namespace DisplayLinearCombination {
    class Program {
        static string LinearCombo(List<int> c) {
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < c.Count; i++) {
                int n = c[i];
                if (n < 0) {
                    if (sb.Length == 0) {
                        sb.Append('-');
                    } else {
                        sb.Append(" - ");
                    }
                } else if (n > 0) {
                    if (sb.Length != 0) {
                        sb.Append(" + ");
                    }
                } else {
                    continue;
                }

                int av = Math.Abs(n);
                if (av != 1) {
                    sb.AppendFormat("{0}*", av);
                }
                sb.AppendFormat("e({0})", i + 1);
            }
            if (sb.Length == 0) {
                sb.Append('0');
            }
            return sb.ToString();
        }

        static void Main(string[] args) {
            List<List<int>> combos = new List<List<int>>{
                new List<int> { 1, 2, 3},
                new List<int> { 0, 1, 2, 3},
                new List<int> { 1, 0, 3, 4},
                new List<int> { 1, 2, 0},
                new List<int> { 0, 0, 0},
                new List<int> { 0},
                new List<int> { 1, 1, 1},
                new List<int> { -1, -1, -1},
                new List<int> { -1, -2, 0, -3},
                new List<int> { -1},
            };

            foreach (List<int> c in combos) {
                var arr = "[" + string.Join(", ", c) + "]";
                Console.WriteLine("{0,15} -> {1}", arr, LinearCombo(c));
            }
        }
    }
}









Ada: with Ada.Text_Io;
with Ada.Strings.Unbounded;
with Ada.Strings.Fixed;

procedure Display_Linear is

   subtype Position is Positive;
   type Coefficient is new Integer;
   type Combination is array (Position range <>) of Coefficient;

   function Linear_Combination (Comb : Combination) return String is
      use Ada.Strings.Unbounded;
      use Ada.Strings;
      Accu : Unbounded_String;
   begin
      for Pos in Comb'Range loop
         case Comb (Pos) is
            when Coefficient'First .. -1 =>
               Append (Accu, (if Accu = "" then "-" else " - "));
            when 0 => null;
            when 1 .. Coefficient'Last =>
               Append (Accu, (if Accu /= "" then " + " else ""));
         end case;

         if Comb (Pos) /= 0 then
            declare
               Abs_Coeff   : constant Coefficient := abs Comb (Pos);
               Coeff_Image : constant String := Fixed.Trim (Coefficient'Image (Abs_Coeff), Left);
               Exp_Image   : constant String := Fixed.Trim (Position'Image (Pos), Left);
            begin
               if Abs_Coeff /= 1 then
                  Append (Accu, Coeff_Image & "*");
               end if;
               Append (Accu, "e(" & Exp_Image & ")");
            end;
         end if;
      end loop;

      return (if Accu = "" then "0" else To_String (Accu));
   end Linear_Combination;

   use Ada.Text_Io;
begin
   Put_Line (Linear_Combination ((1, 2, 3)));
   Put_Line (Linear_Combination ((0, 1, 2, 3)));
   Put_Line (Linear_Combination ((1, 0, 3, 4)));
   Put_Line (Linear_Combination ((1, 2, 0)));
   Put_Line (Linear_Combination ((0, 0, 0)));
   Put_Line (Linear_Combination ((1 => 0)));
   Put_Line (Linear_Combination ((1, 1, 1)));
   Put_Line (Linear_Combination ((-1, -1, -1)));
   Put_Line (Linear_Combination ((-1, -2, 0, -3)));
   Put_Line (Linear_Combination ((1 => -1)));
end Display_Linear;