{ "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 }