Skip to content
This repository was archived by the owner on Aug 2, 2021. It is now read-only.

Commit 48d894a

Browse files
committed
Computer Assignment 3 & 4
1 parent b9b653f commit 48d894a

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

51 files changed

+6358
-0
lines changed

.DS_Store

2 KB
Binary file not shown.

CA2/.DS_Store

0 Bytes
Binary file not shown.

CA3/.DS_Store

6 KB
Binary file not shown.
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "code",
5+
"execution_count": 119,
6+
"metadata": {},
7+
"outputs": [],
8+
"source": [
9+
"def quick_sort(array) :\n",
10+
"\n",
11+
" less = []\n",
12+
" equal = []\n",
13+
" greater = []\n",
14+
"\n",
15+
" if len(array) > 1:\n",
16+
" pivot = array[0]\n",
17+
" for x in array:\n",
18+
" if x < pivot:\n",
19+
" less.append(x)\n",
20+
" elif x == pivot:\n",
21+
" equal.append(x)\n",
22+
" elif x > pivot:\n",
23+
" greater.append(x)\n",
24+
" return quick_sort(less)+equal+quick_sort(greater)\n",
25+
" else: \n",
26+
" return array\n",
27+
"\n",
28+
"class WellSort :\n",
29+
" def __init__(self,n,array) :\n",
30+
" self.array = array\n",
31+
" self.n = n\n",
32+
" def s(self) :\n",
33+
" self.array = quick_sort(self.array)\n",
34+
" \n",
35+
" result = str()\n",
36+
" \n",
37+
" if self.n % 2 :\n",
38+
" j = int(self.n/2)+1\n",
39+
" else :\n",
40+
" j = int(self.n/2)\n",
41+
" \n",
42+
" for i in range(j) :\n",
43+
" result += str(self.array[i])\n",
44+
" if i < j - 1 : result += \" \"\n",
45+
" if j < self.n :\n",
46+
" result += str(self.array[j]) + \" \"\n",
47+
" j += 1\n",
48+
" print(result)\n",
49+
" "
50+
]
51+
},
52+
{
53+
"cell_type": "code",
54+
"execution_count": 120,
55+
"metadata": {},
56+
"outputs": [
57+
{
58+
"name": "stdout",
59+
"output_type": "stream",
60+
"text": [
61+
"5\n",
62+
"1 6 4 3 3\n",
63+
"1 4 3 6 3 \n"
64+
]
65+
}
66+
],
67+
"source": [
68+
"n = input()\n",
69+
"l = []\n",
70+
"s = input()\n",
71+
"for i in s.split() :\n",
72+
" l.append(int(i))\n",
73+
"ws = WellSort(int(n),l)\n",
74+
"ws.s()"
75+
]
76+
},
77+
{
78+
"cell_type": "code",
79+
"execution_count": null,
80+
"metadata": {},
81+
"outputs": [],
82+
"source": []
83+
}
84+
],
85+
"metadata": {
86+
"kernelspec": {
87+
"display_name": "Python 3",
88+
"language": "python",
89+
"name": "python3"
90+
},
91+
"language_info": {
92+
"codemirror_mode": {
93+
"name": "ipython",
94+
"version": 3
95+
},
96+
"file_extension": ".py",
97+
"mimetype": "text/x-python",
98+
"name": "python",
99+
"nbconvert_exporter": "python",
100+
"pygments_lexer": "ipython3",
101+
"version": "3.8.3"
102+
}
103+
},
104+
"nbformat": 4,
105+
"nbformat_minor": 4
106+
}
Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "code",
5+
"execution_count": 1,
6+
"metadata": {},
7+
"outputs": [],
8+
"source": [
9+
"class Node:\n",
10+
" def __init__(self, data):\n",
11+
" self.data = data\n",
12+
" # dictionary from child_data to child_node\n",
13+
" self.children = dict()\n",
14+
" self.is_word_end = False\n",
15+
"\n",
16+
" @staticmethod\n",
17+
" def print_graph(cur_node, cur_word = ''):\n",
18+
" if cur_node.is_word_end:\n",
19+
" print(cur_word)\n",
20+
" for data, child in cur_node.children.items():\n",
21+
" Node.print_graph(child, cur_word + data)\n",
22+
"\n",
23+
"\n",
24+
"def build_graph(strings):\n",
25+
" root = Node(None)\n",
26+
" for str_ in strings:\n",
27+
" parent = root\n",
28+
" for char in str_:\n",
29+
" if char in parent.children.keys():\n",
30+
" cur_node = parent.children[char]\n",
31+
" else: \n",
32+
" cur_node = Node(char)\n",
33+
" parent.children[char] = cur_node\n",
34+
" parent = cur_node\n",
35+
" cur_node.is_word_end = True\n",
36+
" return root\n",
37+
"\n",
38+
"\n",
39+
"def is_similar(cur_node, query, index, skipped_node=False):\n",
40+
" if index == len(query):\n",
41+
" return True\n",
42+
" char = query[index]\n",
43+
" if char in cur_node.children.keys():\n",
44+
" if is_similar(cur_node.children[char], query, index+1):\n",
45+
" return True\n",
46+
" if skipped_node is False:\n",
47+
" for child in cur_node.children.values():\n",
48+
" if is_similar(child, query, index+1, skipped_node=True):\n",
49+
" return True\n",
50+
" return False\n",
51+
"\n",
52+
"\n",
53+
"def similarity_checker(strings, queries):\n",
54+
" root = build_graph(strings)\n",
55+
" resutls = []\n",
56+
" # Node.print_graph(root)\n",
57+
" for query in queries:\n",
58+
" if is_similar(root, query, 0):\n",
59+
" resutls.append('YES')\n",
60+
" else: \n",
61+
" resutls.append('NO')\n",
62+
" return resutls\n",
63+
"\n",
64+
"\n",
65+
"def run(inputs):\n",
66+
" n, q = map(int, inputs[0].strip().split())\n",
67+
" strings, queries = [], []\n",
68+
" for i in range(1, n+1):\n",
69+
" strings.append(inputs[i])\n",
70+
" for i in range(n+1, len(inputs)):\n",
71+
" queries.append(inputs[i])\n",
72+
" \n",
73+
" return similarity_checker(strings, queries)\n",
74+
"\n",
75+
"def main():\n",
76+
" inputs = [input()]\n",
77+
" n, q = map(int, inputs[0].strip().split())\n",
78+
" for _ in range(n+q):\n",
79+
" inputs.append(input())\n",
80+
" print('\\n'.join(run(inputs)))\n"
81+
]
82+
},
83+
{
84+
"cell_type": "code",
85+
"execution_count": 2,
86+
"metadata": {},
87+
"outputs": [
88+
{
89+
"name": "stdout",
90+
"output_type": "stream",
91+
"text": [
92+
"Yes\n",
93+
"Yes\n",
94+
"No\n",
95+
"Yes\n"
96+
]
97+
}
98+
],
99+
"source": [
100+
"# main()\n",
101+
"inputs = [\n",
102+
"\"5 4\",\n",
103+
"\"ab\",\n",
104+
"\"cacab\",\n",
105+
"\"cbabc\",\n",
106+
"\"acc\",\n",
107+
"\"cacab\",\n",
108+
"\"abc\",\n",
109+
"\"aa\",\n",
110+
"\"acbca\",\n",
111+
"\"cb\",\n",
112+
"]\n",
113+
"print('\\n'.join(run(inputs)))"
114+
]
115+
}
116+
],
117+
"metadata": {
118+
"kernelspec": {
119+
"display_name": "Python 3",
120+
"language": "python",
121+
"name": "python3"
122+
},
123+
"language_info": {
124+
"codemirror_mode": {
125+
"name": "ipython",
126+
"version": 3
127+
},
128+
"file_extension": ".py",
129+
"mimetype": "text/x-python",
130+
"name": "python",
131+
"nbconvert_exporter": "python",
132+
"pygments_lexer": "ipython3",
133+
"version": "3.8.3"
134+
}
135+
},
136+
"nbformat": 4,
137+
"nbformat_minor": 4
138+
}

0 commit comments

Comments
 (0)