-
Notifications
You must be signed in to change notification settings - Fork 0
/
leet_code_592.js
130 lines (107 loc) · 3 KB
/
leet_code_592.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
// Input: "-1/2+1/2"
// Output: "0/1"
// Input: "-1/2+1/2+1/3"
// Output: "1/3"
// Input: "5/3+1/3"
// Output: "2/1"
// solve by implementing a stack
// seperate out each expression
// helper function of adding
// helper function of fraction reducing
var fractionAddition = function (expression) {
if (!expression) return '0/1';
// split each expression
let splitArr = splitExp(expression);
// initialize a running total
let fracStack = splitArr[0];
// in a for loop, iterate through and use add function
for (let i = 1; i < splitArr.length; i++){
fracStack = addFrac(fracStack, splitArr[i]);
if (fracStack[1] == 0) fracStack = "+0/1";
}
return reduceFrac(fracStack);
};
function splitExp(expression) {
// expression contains no spaces
// so pattern is always sign number / number
// except first one. if first entry is not '-', add '+'
if (expression[0] !== '-') expression = '+' + expression;
let reArr = [];
let subExp;
for(let i = 0; i<expression.length; i = i + 4){
subExp = expression.substring(i, i+4);
reArr.push(subExp);
}
return reArr;
}
function addFrac(exp1, exp2){
// in order to add functions, compare signs and denominators
let reExp;
if ( exp1[3] !== exp2[3]){
// use sameDenom then add value
let reArr = sameDenom(exp1, exp2);
exp1 = reArr[0];
exp2 = reArr[1];
reExp = addValue(exp1, exp2);
} else {
// add the two exp
reExp = addValue(exp1, exp2);
}
return reExp;
}
function sameDenom(exp1, exp2){
// returns two expressions cross multiplied in array
let denom1 = exp1[3];
let denom2 = exp2[3];
let lcm = (denom1 * denom2).toString();
let nume1 = exp1[1] * denom2;
let nume2 = exp2[1] * denom1;
exp1 = exp1[0] + nume1 + '/' + lcm;
exp2 = exp2[0] + nume2 + '/' + lcm;
return [exp1, exp2];
}
function addValue(exp1, exp2){
let sign1 = exp1[0];
let sign2 = exp2[0];
let nume1 = exp1.split(/[-+/]+/)[1];
let nume2 = exp2.split(/[-+/]+/)[1];
let reExp;
if (sign1 !== sign2) {
// compare numerators
if (nume1 <= nume2) {
let newNum = nume2 - nume1;
reExp = sign2 + newNum + '/' + exp1[exp1.length - 1];
} else {
let newNum = nume1 - nume2;
reExp = sign1 + newNum + '/' + exp1[exp1.length - 1];
}
} else {
let newNum = parseInt(nume1) + parseInt(nume2);
reExp = sign1 + newNum + '/' + exp1[exp1.length - 1];
}
return reExp;
}
function reduceFrac(exp){
let sign = exp[0];
let splitArr = exp.split(/[-+/]+/)
let nume = splitArr[1];
let denom = splitArr[2];
if (nume == 0) return `${nume}` + '/' + '1';
let greater = (nume > denom) ? nume : denom;
for (let i = 2; i < greater; i++) {
if (!(nume % i) && !(denom % i)) {
nume = nume / i;
denom = denom / i;
greater = greater / i;
i = 2;
}
}
if ( sign === '+') {
return `${nume}` + '/' + `${denom}`;
} else {
return sign + `${nume}` + '/' + `${denom}`;
}
}
fractionAddition("5/3+1/3");
// failed tech interview likely
// practice more