FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
coding-Interview/BasicCalculatorWords.java at master · arjunmullick/coding-Interview · GitHub
arjunmullick
/
coding-Interview
Public
Notifications
You must be signed in to change notification settings
Fork
1
Star
3
Code
Issues
0
Pull requests
0
Actions
Projects
Wiki
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Wiki
Security and quality
Insights
Expand file tree
Breadcrumbs
coding-Interview
/
BasicCalculatorWords.java
Copy path
More file actions
More file actions
Latest commit
History
History
History
119 lines (102 loc) · 4.03 KB
Breadcrumbs
coding-Interview
/
BasicCalculatorWords.java
Copy path
File metadata and controls
119 lines (102 loc) · 4.03 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
import
java
.
io
.*;
import
java
.
util
.
HashMap
;
import
java
.
util
.*;
import
com
.
fasterxml
.
jackson
.
databind
.
ObjectMapper
;
import
com
.
fasterxml
.
jackson
.
core
.
JsonProcessingException
;
/*
Advance
Implement the same but with grouping.
eval("(five plus four)") == 9
eval("(one plus one) times ten") == 20
eval("five times (two plus negative one)") == 5
eval("(two plus negative four) times (ten plus one)") == -22
eval("(one plus (three plus four) times five)") == 36
eval("(one plus seven) times three plus four") == 28
eval("(one plus seven) times (three plus four)") == 56
eval("(one plus seven) times (three plus (four plus one plus (negative one)))") == 56
eval("(one plus seven) times (three plus (four plus one plus negative one))") == 56
eval("(one plus seven) times (negative three plus four)") == 8
eval("(five plus negative four) times three") == 3
eval("(five plus negative four) times (three plus four)") == 7
*/
// Main class should be named 'Solution'
class
Solution
{
public
static
HashMap
<
String
,
Integer
>
numbers
=
new
HashMap
<>();
public
static
HashMap
<
String
,
Character
>
operations
=
new
HashMap
<>();
public
static
void
initMap
(){
numbers
.
put
(
"zero"
,
0
);
numbers
.
put
(
"one"
,
1
);
numbers
.
put
(
"two"
,
2
);
numbers
.
put
(
"three"
,
3
);
numbers
.
put
(
"four"
,
4
);
numbers
.
put
(
"five"
,
5
);
numbers
.
put
(
"six"
,
6
);
numbers
.
put
(
"seven"
,
7
);
numbers
.
put
(
"eight"
,
8
);
numbers
.
put
(
"nine"
,
9
);
numbers
.
put
(
"ten"
,
10
);
operations
.
put
(
"plus"
,
'+'
);
operations
.
put
(
"times"
,
'*'
);
//operations.put("negative", '-');
}
public
static
int
eval
(
String
str
){
if
(
str
==
null
||
str
.
length
() ==
0
)
return
0
;
Stack
<
Integer
>
stack
=
new
Stack
<>();
Character
op
=
'+'
;
//revisit
int
num
=
0
;
String
[]
arr
=
str
.
split
(
" "
);
int
n
=
arr
.
length
;
boolean
negative
=
false
;
for
(
int
i
=
0
;
i
<
n
;
i
++){
String
word
=
arr
[
i
];
if
(
word
.
length
() ==
0
)
continue
;
if
(
numbers
.
containsKey
(
word
)){
//System.out.println(word + " " + op);
if
(
op
==
'+'
){
num
=
numbers
.
get
(
word
);
if
(
negative
) {
stack
.
push
(-
num
);
}
else
{
stack
.
push
(
num
);
}
negative
=
false
;
//reset
}
else
if
(
op
==
'*'
){
int
prev
=
stack
.
pop
();
num
=
prev
*
numbers
.
get
(
word
);
if
(
negative
) {
stack
.
push
(-
num
);
}
else
{
stack
.
push
(
num
);
}
negative
=
false
;
//reset
}
else
if
(
op
==
'-'
){
num
= -
1
*
numbers
.
get
(
word
);
stack
.
push
(
num
);
}
}
else
if
(
operations
.
containsKey
(
word
)){
op
=
operations
.
get
(
word
);
}
else
if
(
word
.
equals
(
"negative"
)){
negative
=
true
;
}
}
//System.out.println(stack);
int
ans
=
0
;
while
(
stack
.
size
() >
0
){
ans
=
ans
+
stack
.
pop
();
}
return
ans
;
}
public
static
void
main
(
String
[]
args
) {
System
.
out
.
println
(
"Hello, World"
);
initMap
();
System
.
out
.
println
(
eval
(
"one plus one"
));
System
.
out
.
println
(
eval
(
"five plus two times negative one"
));
System
.
out
.
println
(
eval
(
"negative three times ten"
));
System
.
out
.
println
(
eval
(
"five times three plus one"
));
System
.
out
.
println
(
eval
(
"two times three times four"
));
System
.
out
.
println
(
eval
(
"two times three plus four times five"
));
System
.
out
.
println
(
eval
(
"two plus three times four plus five"
));
System
.
out
.
println
(
eval
(
"two times three times four plus five times six"
));
System
.
out
.
println
(
eval
(
"two times three plus four plus five plus six times seven"
));
}
}
Back
|
FazBrowse Home
|
New Git URL