File tree Expand file tree Collapse file tree 4 files changed +130
-1
lines changed Expand file tree Collapse file tree 4 files changed +130
-1
lines changed Original file line number Diff line number Diff line change @@ -29,4 +29,4 @@ def baseConvert(num, base):
29
29
30
30
return result
31
31
32
- print (baseConvert (25 ,2 ))
32
+ print (baseConvert (2 ,2 ))
Original file line number Diff line number Diff line change
1
+ class Stack :
2
+ """docstring for stack."""
3
+ def __init__ (self ):
4
+ self .stack = []
5
+ def push (self , item ):
6
+ self .stack .append (item )
7
+ def pop (self ):
8
+ return self .stack .pop ()
9
+ def peek (self ):
10
+ return self .stack [self .size ()- 1 ]
11
+ def isEmpty (self ):
12
+ return self .stack == []
13
+ def size (self ):
14
+ return len (self .stack )
15
+
16
+ def infix2postFix (string ):
17
+ prec = {}
18
+ prec ["*" ] = 3
19
+ prec ["/" ] = 3
20
+ prec ["+" ] = 3
21
+ prec ["-" ] = 3
22
+ prec ["(" ] = 1
23
+
24
+ s = Stack ()
25
+ expression = string .split (' ' )
26
+ result = ""
27
+
28
+ for x in expression :
29
+ if x == "(" :
30
+ s .push (x )
31
+
32
+ elif x == ")" :
33
+ item = s .pop ()
34
+ while (item != "(" ):
35
+ result += item
36
+ item = s .pop ()
37
+
38
+ elif x in "1234567890" or x in "abcdefghijklmnopqrstuvwxyz" :
39
+ result += x
40
+
41
+ elif x in "*/-+" and s .size () != 0 :
42
+ if s .peek () in "*/+-" :
43
+ while (prec [s .peek ()] >= prec [x ] ):
44
+ result += s .pop ()
45
+ s .push (x )
46
+ s .push (x )
47
+
48
+ elif x in "*/-+" :
49
+ s .push (x )
50
+
51
+ leftStack = s .size ()
52
+
53
+ while (leftStack > 0 ):
54
+ result += s .pop ()
55
+ leftStack -= 1
56
+ return result
57
+
58
+ print (infix2postFix ("9 + 3" ))
Original file line number Diff line number Diff line change
1
+
2
+ class Stack :
3
+ def __init__ (self ):
4
+ self .stack = []
5
+ def push (self , item ):
6
+ self .stack .append (item )
7
+ def pop (self ):
8
+ return self .stack .pop ()
9
+ def isEmpty (self ):
10
+ return self .stack == []
11
+ def size (self ):
12
+ return len (self .stack )
13
+
14
+ n = int (input ())
15
+ while (n > 0 ):
16
+ n = n - 1
17
+ x = str (input ())
18
+ a , b = x .split (' ' )
19
+ aS = Stack ()
20
+ bS = Stack ()
21
+ for x in a :
22
+ aS .push (int (x ))
23
+ for y in b :
24
+ bS .push (int (y ))
25
+ string1 = ""
26
+ string2 = ""
27
+ while (aS .isEmpty () == False ):
28
+ string1 += str (aS .pop ())
29
+ while (bS .isEmpty () == False ):
30
+ string2 += str (bS .pop ())
31
+
32
+ # print("String1" + string1)
33
+ digit1 = int (string1 )
34
+ digit2 = int (string2 )
35
+
36
+ sum = digit1 + digit2
37
+
38
+ sS = Stack ()
39
+ sumString = str (sum )
40
+
41
+ for x in sumString :
42
+ sS .push (int (x ))
43
+
44
+ result = ""
45
+
46
+ length = sS .size ()
47
+
48
+ while (length > 0 ):
49
+ length = length - 1
50
+ item = sS .pop ()
51
+ if (item != 0 ):
52
+ sS .push (item )
53
+ break
54
+
55
+ while (sS .isEmpty () == False ):
56
+ result += str (sS .pop ())
57
+ print (result )
Original file line number Diff line number Diff line change
1
+ n = int (input ())
2
+ while (n > 0 ):
3
+ n = n - 1
4
+ a = raw_input ()
5
+ # print(a)
6
+ x ,y = a .split (' ' )
7
+ x = x [::- 1 ]
8
+ y = y [::- 1 ]
9
+ res = int (x ) + int (y )
10
+ res = str (res )
11
+ while (res [len (res )- 1 : len (res )] == '0' ) and len (res ) > 0 :
12
+ res = res [:len (res )- 1 ]
13
+ res = res [::- 1 ]
14
+ print (res )
You can’t perform that action at this time.
0 commit comments