File tree Expand file tree Collapse file tree 7 files changed +74
-2
lines changed
fraction-addition-and-subtraction Expand file tree Collapse file tree 7 files changed +74
-2
lines changed Original file line number Diff line number Diff line change @@ -904,6 +904,8 @@ https://leetcode.cn/problems/sequence-reconstruction/
904
904
905
905
https://leetcode.cn/problems/ur2n8P/
906
906
907
+ https://leetcode.cn/problems/fraction-addition-and-subtraction/
908
+
907
909
#### 安装教程
908
910
909
911
1 . 安装` deno `
Original file line number Diff line number Diff line change
1
+ export class Fraction {
2
+ sign : number ;
3
+ molecular : number ;
4
+ denominator : number ;
5
+ toString ( ) {
6
+ return `${
7
+ this . sign < 0 ? "-" : ""
8
+ } ${ this . molecular } /${ this . denominator } `;
9
+ }
10
+ constructor ( {
11
+ sign = "+" ,
12
+ molecular = 1 ,
13
+ denominator = 1 ,
14
+ } : {
15
+ sign ?: "+" | "-" | number ;
16
+ molecular ?: number | string ;
17
+ denominator ?: number | string ;
18
+ } = { } ) {
19
+ sign = sign || "+" ;
20
+ this . sign = ( sign === "+" ? 1 : sign === "-" ? - 1 : sign ) *
21
+ Math . sign ( Number ( denominator ) ) *
22
+ Math . sign ( Number ( molecular ) ) ;
23
+ this . molecular = Math . abs ( Number ( molecular ) ) ;
24
+ this . denominator = Math . abs ( Number ( denominator ) ) ;
25
+ }
26
+ }
Original file line number Diff line number Diff line change
1
+ export function deduplication < T > ( iter : Iterable < T > ) : Array < T > {
2
+ return Array . from ( new Set ( iter ) ) ;
3
+ }
Original file line number Diff line number Diff line change
1
+ import { greatest_common_divisor } from "../max-points-on-a-line/greatest_common_divisor.ts" ;
2
+ import { deduplication } from "./deduplication.ts" ;
3
+ import { Fraction } from "./Fraction.ts" ;
4
+ import { parseFraction } from "./parseFraction.ts" ;
5
+ function fractionAddition ( expression : string ) : string {
6
+ const fractions = parseFraction ( expression ) ;
7
+
8
+ const denominator = deduplication (
9
+ fractions . map ( ( f ) => f . denominator ) ,
10
+ ) . reduce ( ( a , b ) => a * b ) ;
11
+ const molecular = fractions
12
+ . map ( ( f ) => ( f . sign * f . molecular * denominator ) / f . denominator )
13
+ . reduce ( ( a , b ) => a + b ) ;
14
+ const gcd = greatest_common_divisor ( molecular , denominator ) ;
15
+
16
+ return new Fraction ( {
17
+ denominator : denominator / gcd ,
18
+ molecular : molecular / gcd ,
19
+ } ) . toString ( ) ;
20
+ }
21
+ export default fractionAddition ;
Original file line number Diff line number Diff line change
1
+ import { Fraction } from "./Fraction.ts" ;
2
+
3
+ export function parseFraction ( expression : string ) : Array < Fraction > {
4
+ return Array . from (
5
+ expression . matchAll (
6
+ / (?< sign > [ + - ] ? ) (?< molecular > \d + ) \/ (?< denominator > \d + ) / g,
7
+ ) ,
8
+ ) . map ( ( a ) => new Fraction ( a . groups ) ) ;
9
+ }
Original file line number Diff line number Diff line change
1
+ import { assertEquals } from "https://deno.land/[email protected] /testing/asserts.ts" ;
2
+ import fractionAddition from "./index.ts" ;
3
+ Deno . test ( "fraction-addition-and-subtraction" , ( ) => {
4
+ assertEquals (
5
+ "77464/145635" ,
6
+ fractionAddition ( "100/399-133/2555-1/2+1/2-1/2+1/2+1/3" ) ,
7
+ ) ;
8
+ assertEquals ( "0/1" , fractionAddition ( "-1/2+1/2" ) ) ;
9
+ assertEquals ( "1/3" , fractionAddition ( "-1/2+1/2+1/3" ) ) ;
10
+ assertEquals ( "-1/6" , fractionAddition ( "1/3-1/2" ) ) ;
11
+ } ) ;
Original file line number Diff line number Diff line change 1
- export const greatest_common_divisor = ( a : number , b : number ) : number => {
1
+ export function greatest_common_divisor ( a : number , b : number ) : number {
2
2
return b != 0 ? greatest_common_divisor ( b , a % b ) : a ;
3
- } ;
3
+ }
You can’t perform that action at this time.
0 commit comments