File size: 8,030 Bytes
8938d1b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "OrderedDict([(1, 1)])\n",
      "OrderedDict([(1, 1), (2, 2)])\n",
      "OrderedDict([(2, 2), (1, 1)])\n",
      "OrderedDict([(1, 1), (3, 3)])\n",
      "OrderedDict([(1, 1), (3, 3)])\n",
      "OrderedDict([(3, 3), (4, 4)])\n",
      "OrderedDict([(3, 3), (4, 4)])\n",
      "OrderedDict([(4, 4), (3, 3)])\n",
      "OrderedDict([(3, 3), (4, 4)])\n"
     ]
    }
   ],
   "source": [
    "from collections import OrderedDict\n",
    "\n",
    "class LRUCache:\n",
    "\n",
    "  # initialising capacity\n",
    "  def __init__(self, capacity: int):\n",
    "    self.cache = OrderedDict()\n",
    "    self.capacity = capacity\n",
    "\n",
    "  # we return the value of the key\n",
    "  # that is queried in O(1) and return -1 if we\n",
    "  # don't find the key in out dict / cache.\n",
    "  # And also move the key to the end\n",
    "  # to show that it was recently used.\n",
    "  def get(self, key: int) -> int:\n",
    "    if key not in self.cache:\n",
    "      return -1\n",
    "    else:\n",
    "      self.cache.move_to_end(key)\n",
    "      return self.cache[key]\n",
    "\n",
    "  # first, we add / update the key by conventional methods.\n",
    "  # And also move the key to the end to show that it was recently used.\n",
    "  # But here we will also check whether the length of our\n",
    "  # ordered dictionary has exceeded our capacity,\n",
    "  # If so we remove the first key (least recently used)\n",
    "  def put(self, key: int, value: int) -> None:\n",
    "    self.cache[key] = value\n",
    "    self.cache.move_to_end(key)\n",
    "    if len(self.cache) > self.capacity:\n",
    "      self.cache.popitem(last = False)\n",
    "\n",
    "\n",
    "# RUNNER\n",
    "# initializing our cache with the capacity of 2\n",
    "cache = LRUCache(2)\n",
    "\n",
    "\n",
    "cache.put(1, 1)\n",
    "print(cache.cache)\n",
    "cache.put(2, 2)\n",
    "print(cache.cache)\n",
    "cache.get(1)\n",
    "print(cache.cache)\n",
    "cache.put(3, 3)\n",
    "print(cache.cache)\n",
    "cache.get(2)\n",
    "print(cache.cache)\n",
    "cache.put(4, 4)\n",
    "print(cache.cache)\n",
    "cache.get(1)\n",
    "print(cache.cache)\n",
    "cache.get(3)\n",
    "print(cache.cache)\n",
    "cache.get(4)\n",
    "print(cache.cache)\n",
    "\n",
    "#This code was contributed by Sachin Negi\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Stop Words (EASY) - 15 minutes\n",
    "\n",
    "Implement a function that takes a string text and an integer k, and returns the list of words that occur in the text at least k times. The words must be returned in the order of their first occurrence in the text."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'the': [2, 0], 'brown': [3, 1], 'fox': [1, 2], 'jumps': [1, 3], 'over': [1, 4], 'dog': [1, 7], 'and': [1, 8], 'runs': [1, 9], 'away': [1, 10], 'to': [1, 11], 'a': [1, 12], 'house': [1, 14]}\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "['the',\n",
       " 'brown',\n",
       " 'fox',\n",
       " 'jumps',\n",
       " 'over',\n",
       " 'dog',\n",
       " 'and',\n",
       " 'runs',\n",
       " 'away',\n",
       " 'to',\n",
       " 'a',\n",
       " 'house']"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "text = \"the brown fox jumps over the brown dog and runs away to a brown house\"\n",
    "k = 2 \n",
    "\n",
    "def stop_words(text, k):\n",
    "    cnt = {}\n",
    "    for i, w in enumerate(text.split(\" \")):\n",
    "        if w not in cnt:\n",
    "            cnt[w] = [0, i]\n",
    "        cnt[w][0] += 1\n",
    "    print(cnt)\n",
    "    stop_words = [(i, w) for w, (c, i) in cnt.items() if c >= k]\n",
    "    res = [w for _, w in sorted(stop_words)]\n",
    "    return res\n",
    "\n",
    "\n",
    "stop_words(text, 1)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# DPI Software Traffic Deviation Report - Medium SQL - 35 minutes\n",
    "\n",
    "As part of HackerSniff's DPI (Deep Packet Inspection) software analytics, a team needs a list of all the clients and traffic protocols they have used.\n",
    "\n",
    " \n",
    "\n",
    "The result should be in the following format: client, protocol.\n",
    "\n",
    "    protocol is a comma-separated list of all the protocols for particular client, ordered descending by total traffic, which calculated as sum of traffic_in and traffic_out.\n",
    "    Results should be sorted ascending by client.\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "sql = \"\"\"\n",
    "WITH _data AS (\n",
    "SELECT client,\n",
    "    protocol,\n",
    "    SUM(traffic_in) + SUM(traffic_out) traffic\n",
    "FROM traffic\n",
    "GROUP BY client, protocol\n",
    ")\n",
    "\n",
    "SELECT client,\n",
    "    GROUP_CONCAT(protocol ORDER BY traffic DESC) AS protocol\n",
    "FROM _data\n",
    "GROUP BY client\n",
    "ORDER BY client\n",
    "\"\"\"\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Balanced Sales Array (EASY) \n",
    "\n",
    "Given has an array of sales numbers, what is the index of the smallest index element for which the sums of all elements to the left and to the right are equal. The array may not be reordered.\n",
    "For example, given the array sales = [1, 2, 3, 4, 6],we see that 1+2+3=6,Using zero based indexing,sales[3] = 4 is the value sought.The index is 3."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "16\n",
      "16\n",
      "[1, 3, 6, 10, 16]\n",
      "[6, 10, 13, 15, 16]\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "3"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data = [1, 2, 3, 4, 6]\n",
    "\n",
    "class Solution:\n",
    "    \"\"\"\n",
    "    @param sales: a integer array\n",
    "    @return: return a Integer\n",
    "    \"\"\"\n",
    "    import itertools\n",
    "    def BalancedSalesArray(self, sales):\n",
    "        # write your code here\n",
    "        left,right = [],[]\n",
    "        leftsum = rightsum = 0\n",
    "        n = len(sales)\n",
    "        for i in range(n):\n",
    "            leftsum += sales[i] \n",
    "            left.append(leftsum)\n",
    "            rightsum += sales[n-1-i]\n",
    "            right.append(rightsum)\n",
    "            \n",
    "        print(leftsum)\n",
    "        print(rightsum)\n",
    "\n",
    "        print(left)\n",
    "        print(right)\n",
    "\n",
    "        n = len(sales)\n",
    "        for i in range(n):\n",
    "            if left[i] == right[n-1-i]:\n",
    "                return i \n",
    "        return -1\n",
    "        \n",
    "        \n",
    "Solution().BalancedSalesArray(data)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3.10.5 ('pytorch-nightly')",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.5"
  },
  "orig_nbformat": 4,
  "vscode": {
   "interpreter": {
    "hash": "8a8bcccfb183d1298694efece6cf41240378bc61621e95c864629a40c5876542"
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}