1
+ public class KataSolution {
2
+
3
+ public static String expand (String expr ) {
4
+ // The corresponding values will be stored in these variables
5
+ int n , a = Integer .MIN_VALUE , b = Integer .MIN_VALUE ;
6
+ char letter = ' ' ;
7
+
8
+ // We get the value of a, b, n and the letter of the expression
9
+ StringBuilder sb = new StringBuilder ();
10
+ for (int i = 1 ; i < expr .length (); ++i ) {
11
+ if (expr .charAt (i ) == '-' || isNumber (String .valueOf (expr .charAt (i )))) {
12
+ sb .append (expr .charAt (i ));
13
+ } else if (isNumber (sb .toString ()) || sb .toString ().equals ("-" )) {
14
+ if (sb .toString ().equals ("-" ))
15
+ sb .append ("1" );
16
+
17
+ if (a == Integer .MIN_VALUE ) {
18
+ a = Integer .parseInt (sb .toString ());
19
+ letter = expr .charAt (i );
20
+ } else
21
+ b = Integer .parseInt (sb .toString ());
22
+
23
+ sb .setLength (0 );
24
+ }
25
+ }
26
+ // If the coefficient a == 1, then we adjust the values
27
+ if (b == Integer .MIN_VALUE ) {
28
+ b = a ;
29
+ a = 1 ;
30
+ letter = expr .charAt (1 );
31
+ }
32
+ // We get the value of the degree
33
+ n = Integer .parseInt (sb .toString ());
34
+
35
+ // If the coefficient b == 0, then we return the result of the expression (a*letter)^n
36
+ if (b == 0 )
37
+ return getPolyUnit (1 , n , 0 , a , b , letter , true );
38
+
39
+ // If the degree is == 0, then return 1
40
+ if (n == 0 )
41
+ return "1" ;
42
+
43
+ sb .setLength (0 );
44
+
45
+ // Otherwise, we calculate the value of each term of the polynomial using Pascal's triangle
46
+ long nFac = getFactorial (n );
47
+ for (int k = 0 ; k < n + 1 ; ++k ) {
48
+ // Calculate the coefficient C_n^k
49
+ long c = nFac / (getFactorial (k ) * getFactorial (n - k ));
50
+
51
+ // If the leading element is missing + before the number
52
+ if (sb .isEmpty ()) {
53
+ sb .append (getPolyUnit (c , n , k , a , b , letter , true ));
54
+ } else
55
+ sb .append (getPolyUnit (c , n , k , a , b , letter , false ));
56
+ }
57
+
58
+ return sb .toString ();
59
+ }
60
+
61
+
62
+ // The method allows you to determine whether a sequence is a number
63
+ private static boolean isNumber (String str ) {
64
+ try {
65
+ Integer .parseInt (str );
66
+ return true ;
67
+ } catch (Exception e ) {
68
+ return false ;
69
+ }
70
+ }
71
+
72
+
73
+ // The method allows you to get the factorial of a number
74
+ private static long getFactorial (int f ) {
75
+ long result = 1 ;
76
+ for (long i = 1 ; i <= f ; i ++) {
77
+ result = result * i ;
78
+ }
79
+ return result ;
80
+ }
81
+
82
+
83
+ // The method allows you to get a correct representation of the next term of the polynomial
84
+ private static String getPolyUnit (long c , int n , int k , int a , int b , char letter , boolean isLead ) {
85
+ StringBuilder result = new StringBuilder ();
86
+
87
+ // We raise the coefficients a and b to the appropriate degrees
88
+ long newA = (long ) Math .pow (a , n - k );
89
+ long newB = (long ) Math .pow (b , k );
90
+
91
+ // If the product is different from zero
92
+ if (c * newA * newB != 0 ) {
93
+ // If the coefficient of a non-leading element is positive
94
+ if (c * newA * newB > 0 && !isLead )
95
+ result .append ("+%d" .formatted (c * newA * newB ));
96
+ // If the coefficient in front of the leading element == -1
97
+ else if (c * newA * newB == -1 && isLead )
98
+ result .append ("-" );
99
+ // If the value of the coefficient modulus is != 1 or the last element is being processed
100
+ else if (Math .abs (c * newA * newB ) != 1 || n - k == 0 )
101
+ result .append (c * newA * newB );
102
+ }
103
+
104
+ // Processing the display of the letter in the expression. It is present in the expression if
105
+ // the coefficient is not zero, or it is not the last element
106
+ if (n - k != 0 && c * newA * newB != 0 ) {
107
+ // If the last element is added, add a letter without a degree
108
+ if (n - k == 1 )
109
+ result .append (letter );
110
+ // Otherwise, with a degree
111
+ else
112
+ result .append ("%s^%d" .formatted (letter , n - k ));
113
+ }
114
+
115
+ return result .toString ();
116
+ }
117
+ }
0 commit comments