Skip to content

Commit 4ae4aa1

Browse files
refactor 537
1 parent 66d146a commit 4ae4aa1

File tree

2 files changed

+50
-32
lines changed

2 files changed

+50
-32
lines changed
Lines changed: 43 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
package com.fishercoder.solutions;
22

3+
import java.util.stream.Stream;
4+
35
/**
6+
* 537. Complex Number Multiplication
7+
*
48
* Given two strings representing two complex numbers.
59
610
You need to return a string representing their multiplication. Note i2 = -1 according to the definition.
@@ -21,35 +25,49 @@
2125
*/
2226
public class _537 {
2327

24-
public String complexNumberMultiply(String a, String b) {
25-
String[] part1And2 = a.split("\\+");
26-
String[] part3And4 = b.split("\\+");
27-
String product1 = String.valueOf(Integer.parseInt(part1And2[0]) * Integer.parseInt(part3And4[0]));//this is real number multiplication
28-
String product2 = multiply(part1And2[0], part3And4[1]);
29-
String product3 = multiply(part3And4[0], part1And2[1]);
30-
String product4 = multiplyTwoIs(part3And4[1], part1And2[1]);
31-
String twoISum = sumTwoI(product2, product3);
32-
String numberValue = String.valueOf(Integer.valueOf(product1) + Integer.valueOf(product4));
33-
return numberValue + "+" + twoISum;
34-
}
28+
public static class Solution1 {
29+
public String complexNumberMultiply(String a, String b) {
30+
String[] part1And2 = a.split("\\+");
31+
String[] part3And4 = b.split("\\+");
32+
String product1 = String.valueOf(Integer.parseInt(part1And2[0]) * Integer.parseInt(part3And4[0]));//this is real number multiplication
33+
String product2 = multiply(part1And2[0], part3And4[1]);
34+
String product3 = multiply(part3And4[0], part1And2[1]);
35+
String product4 = multiplyTwoIs(part3And4[1], part1And2[1]);
36+
String twoISum = sumTwoI(product2, product3);
37+
String numberValue = String.valueOf(Integer.valueOf(product1) + Integer.valueOf(product4));
38+
return numberValue + "+" + twoISum;
39+
}
3540

36-
private String sumTwoI(String product2, String product3) {
37-
int number2 = Integer.parseInt(product2.substring(0, product2.length() - 1));
38-
int number3 = Integer.parseInt(product3.substring(0, product3.length() - 1));
39-
return String.valueOf(number2 + number3) + "i";
40-
}
41+
private String sumTwoI(String product2, String product3) {
42+
int number2 = Integer.parseInt(product2.substring(0, product2.length() - 1));
43+
int number3 = Integer.parseInt(product3.substring(0, product3.length() - 1));
44+
return String.valueOf(number2 + number3) + "i";
45+
}
4146

42-
private String multiplyTwoIs(String p, String q) {
43-
int number1 = Integer.parseInt(p.substring(0, p.length() - 1));
44-
int number2 = Integer.parseInt(q.substring(0, q.length() - 1));
45-
int numberProduct = number1 * number2;
46-
return String.valueOf(-numberProduct);
47-
}
47+
private String multiplyTwoIs(String p, String q) {
48+
int number1 = Integer.parseInt(p.substring(0, p.length() - 1));
49+
int number2 = Integer.parseInt(q.substring(0, q.length() - 1));
50+
int numberProduct = number1 * number2;
51+
return String.valueOf(-numberProduct);
52+
}
4853

49-
private String multiply(String p, String withI) {
50-
int numberPart = Integer.parseInt(withI.substring(0, withI.length() - 1));
51-
return String.valueOf(numberPart * Integer.valueOf(p)) + "i";
54+
private String multiply(String p, String withI) {
55+
int numberPart = Integer.parseInt(withI.substring(0, withI.length() - 1));
56+
return String.valueOf(numberPart * Integer.valueOf(p)) + "i";
57+
}
5258
}
5359

60+
public static class Solution2 {
61+
/**
62+
* (a + bi) * (c + di) could become (ac - bd) + (ad + bc)*i
63+
* Thus, we have the following function
64+
*/
65+
public String complexNumberMultiply(String a, String b) {
66+
int[] coefficients1 = Stream.of(a.split("\\+|i")).mapToInt(Integer::parseInt).toArray();
67+
int[] coefficients2 = Stream.of(b.split("\\+|i")).mapToInt(Integer::parseInt).toArray();
68+
return (coefficients1[0] * coefficients2[0] - coefficients1[1] * coefficients2[1]) + "+"
69+
+ (coefficients1[0] * coefficients2[1] + coefficients1[1] * coefficients2[0] + "i");
70+
}
71+
}
5472

5573
}

src/test/java/com/fishercoder/_537Test.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,16 @@
1111
* Created by fishercoder on 1/25/17.
1212
*/
1313
public class _537Test {
14-
private static _537 test;
14+
private static _537 .Solution1 solution1;
15+
private static _537 .Solution2 solution2;
1516
private static String expected;
16-
private static String actual;
1717
private static String a;
1818
private static String b;
1919

2020
@BeforeClass
2121
public static void setup() {
22-
test = new _537();
22+
solution1 = new _537.Solution1();
23+
solution2 = new _537.Solution2();
2324
}
2425

2526
@Before
@@ -31,16 +32,15 @@ public void test1() {
3132
expected = "0+2i";
3233
a = "1+1i";
3334
b = "1+1i";
34-
actual = test.complexNumberMultiply(a, b);
35-
assertEquals(expected, actual);
35+
assertEquals(expected, solution1.complexNumberMultiply(a, b));
36+
assertEquals(expected, solution2.complexNumberMultiply(a, b));
3637
}
3738

3839
@Test
3940
public void test2() {
4041
expected = "0+-2i";
4142
a = "1+-1i";
4243
b = "1+-1i";
43-
actual = test.complexNumberMultiply(a, b);
44-
assertEquals(expected, actual);
44+
assertEquals(expected, solution2.complexNumberMultiply(a, b));
4545
}
4646
}

0 commit comments

Comments
 (0)