Offering a Perl solution, because it has several higher levels than bash, which make the code a little easier:
use strict; use warnings; use feature qw(say); my @terms; while (my $line = readline(*DATA)) { chomp($line); my $degree = () = $line =~ / /g; my @coefficients = split / /, $line; my @terms; while ($degree >= 0) { my $coefficient = shift @coefficients; next if $coefficient == 0; push @terms, $degree > 1 ? "${coefficient}x^$degree" : $degree > 0 ? "${coefficient}x" : $coefficient; } continue { $degree--; } say join '+', @terms; } __DATA__ 23 12 0 33 3 4 19
Output Example:
hunter@eros ~ perl test.pl 23x^3+12x^2+33 3x^2+4x+19
Any information that you want to use for any of the built-in functions used above: readline, chomp, push, shift, split, say and join can be found in perldoc with perldoc -f <function-name>
source share