Skip to content

Commit 3cecfef

Browse files
committed
Added Python Program To Check For Balanced Parathesis
1 parent 045111e commit 3cecfef

File tree

2 files changed

+148
-37
lines changed

2 files changed

+148
-37
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
# -*- coding: utf-8 -*-
2+
"""
3+
Created on Fri Oct 18 23:13:34 2019
4+
5+
@author: Prajwal
6+
"""
7+
8+
9+
10+
11+
def push(x, stack):
12+
stack.append(x)
13+
14+
15+
def pop(stack):
16+
x=stack.pop()
17+
return x
18+
19+
def is_empty(stack):
20+
if(len(stack)==0):
21+
return 1
22+
else:
23+
return 0
24+
25+
def match(exp):
26+
stack=list()
27+
for i in exp:
28+
ch=i
29+
#checks for opening brackets
30+
if(ch=='('):
31+
push(ch, stack)
32+
elif(ch=='['):
33+
push(ch, stack)
34+
elif(ch=='{'):
35+
push(ch, stack)
36+
37+
38+
#checks for closing brackets
39+
elif(ch==')'):
40+
if((is_empty(stack))==0):
41+
x=pop(stack)
42+
if(x != '('):
43+
return 0
44+
else:
45+
return 0
46+
elif(ch==']'):
47+
if((is_empty(stack))==0):
48+
x=pop(stack)
49+
if(x != '['):
50+
return 0
51+
else:
52+
return 0
53+
elif(ch=='}'):
54+
if((is_empty(stack))==0):
55+
x=pop(stack)
56+
if(x != '{'):
57+
return 0
58+
else:
59+
return 0
60+
61+
#checks is all elements of stack are dealt with
62+
if(is_empty(stack)):
63+
return 1
64+
else:
65+
return 0
66+
67+
68+
if __name__=='__main__':
69+
string=input('Enter the parenthesis string : ')
70+
result=match(string)
71+
if(result):
72+
print('Parenthesis are matched')
73+
else:
74+
print('Parenthesis are not matched')
75+
76+
77+
78+
79+
80+

BucketSort.cpp

+68-37
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,69 @@
1-
// C++ BucketSort
2-
#include <iostream>
3-
#include <algorithm>
4-
#include <vector>
5-
using namespace std;
6-
7-
// Function to sort arr[] of size n using bucket sort
8-
void bucketSort(float arr[], int n)
9-
{
10-
//Create n empty buckets
11-
vector<float> b[n];
12-
// adds array elements in different buckets
13-
for (int i=0; i<n; i++)
14-
{
15-
int bi = n*arr[i]; // Index in bucket
16-
b[bi].push_back(arr[i]);
17-
}
18-
// Sort buckets individually
19-
for (int i=0; i<n; i++)
20-
sort(b[i].begin(), b[i].end());
21-
//Concatenate all buckets into arr[]
22-
int index = 0;
23-
for (int i = 0; i < n; i++)
24-
for (int j = 0; j < b[i].size(); j++)
25-
arr[index++] = b[i][j];
26-
}
27-
28-
//Main Funct
29-
int main()
30-
{
31-
float arr[] = {0.897, 0.565, 0.656, 0.1234, 0.665, 0.3434};
32-
int n = sizeof(arr)/sizeof(arr[0]);
33-
bucketSort(arr, n);
34-
cout << "Sorted array is \n";
35-
for (int i=0; i<n; i++)
36-
cout << arr[i] << " ";
37-
return 0;
1+
#include <cstdio>
2+
#include <climits>
3+
4+
#define INF INT_MAX
5+
6+
#define ELEMENT_COUNT 100000
7+
#define ELEMENT_RANGE (1 << 17)
8+
#define GROUP_RANGE (1 << 2)
9+
10+
using namespace std;
11+
12+
int n, d[ELEMENT_COUNT];
13+
14+
struct node
15+
{
16+
int v, next;
17+
}A[ELEMENT_COUNT + 1];
18+
int head[ELEMENT_RANGE / GROUP_RANGE], cnt[ELEMENT_RANGE / GROUP_RANGE], vc = 1;
19+
20+
void insert(int v)
21+
{
22+
int group = v / GROUP_RANGE;
23+
A[vc].next = head[group];
24+
head[group] = vc;
25+
A[vc].v = v;
26+
cnt[group]++;
27+
vc++;
28+
}
29+
30+
void bucket_sort()
31+
{
32+
for (int i = 0; i < n; i++)
33+
{
34+
insert(d[i]);
35+
}
36+
int ptr = 0;
37+
for (int i = 0; i < ELEMENT_RANGE / GROUP_RANGE; i++)
38+
{
39+
for (int j = 0; j < cnt[i]; j++)
40+
{
41+
int minv = INF, ord;
42+
for (int cur = head[i]; cur != 0; cur = A[cur].next)
43+
{
44+
if (A[cur].v < minv)
45+
{
46+
minv = A[cur].v;
47+
ord = cur;
48+
}
49+
}
50+
d[ptr++] = minv;
51+
A[ord].v = INF;
52+
}
53+
}
54+
}
55+
56+
int main()
57+
{
58+
scanf("%d", &n);
59+
for (int i = 0; i < n; i++)
60+
{
61+
scanf("%d", &d[i]);
62+
}
63+
bucket_sort();
64+
for (int i = 0; i < n; i++)
65+
{
66+
printf("%d ", d[i]);
67+
}
68+
return 0;
3869
}

0 commit comments

Comments
 (0)