This repository has been archived on 2023-07-05. You can view files and clone it, but cannot push or open issues/pull-requests.
notes/Project Vault/Current Occupations/Potential and Future/Amazon/Coding Assessment/ipynb practice problems/Notebook code/RunLengthEncoding.ipynb

1 line
3.8 KiB
Plaintext

{"nbformat":4,"nbformat_minor":0,"metadata":{"colab":{"provenance":[]},"kernelspec":{"name":"python3","display_name":"Python 3"},"language_info":{"name":"python"}},"cells":[{"cell_type":"markdown","source":["# Problem statement: Run-length encoding is a fast and simple method of encoding strings. The basic idea is to represent repeated successive characters as a single count and character. For example, the string \"AAAABBBCCDAA\" would be encoded as \"4A3B2C1D2A\".\n","Implement run-length encoding and decoding. You can assume the string to be encoded have no digits and consists solely of alphabetic characters. You can assume the string to be decoded is valid"],"metadata":{"id":"8CyZfl83RfPm"}},{"cell_type":"code","execution_count":null,"metadata":{"id":"4C8nYe_TRZFa"},"outputs":[],"source":["def runlength(x):\n"," \n"," encoded = []\n"," count_lc=[i+1 for l in x]\n","\n"," for letter in enumerate(x, start=0):\n"," if letter == letter + 1: \n"," count_lc \n","\n"," print(count_lc + letter)\n"]},{"cell_type":"code","source":["runlength(\"AAALLLLNNN\")"],"metadata":{"colab":{"base_uri":"https://localhost:8080/","height":294},"id":"Q3igp4nGU6oC","executionInfo":{"status":"error","timestamp":1657480487155,"user_tz":240,"elapsed":288,"user":{"displayName":"Shwetha Jayaraj","userId":"01455478857425759475"}},"outputId":"85f71018-5fc8-4b99-8fcb-66750db8ab4d"},"execution_count":null,"outputs":[{"output_type":"error","ename":"TypeError","evalue":"ignored","traceback":["\u001b[0;31m---------------------------------------------------------------------------\u001b[0m","\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)","\u001b[0;32m<ipython-input-35-610396e24660>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mrunlength\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"AAALLLLNNN\"\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m","\u001b[0;32m<ipython-input-34-7b576001af01>\u001b[0m in \u001b[0;36mrunlength\u001b[0;34m(x)\u001b[0m\n\u001b[1;32m 3\u001b[0m \u001b[0mcount_lc\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;34m+\u001b[0m\u001b[0;36m1\u001b[0m \u001b[0;32mfor\u001b[0m \u001b[0mi\u001b[0m \u001b[0;32min\u001b[0m \u001b[0mx\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 4\u001b[0m \u001b[0;32mfor\u001b[0m \u001b[0mletter\u001b[0m \u001b[0;32min\u001b[0m \u001b[0menumerate\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mx\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mstart\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;36m0\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 5\u001b[0;31m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mcount_lc\u001b[0m \u001b[0;34m+\u001b[0m \u001b[0mletter\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m","\u001b[0;31mTypeError\u001b[0m: can only concatenate list (not \"tuple\") to list"]}]},{"cell_type":"code","source":[],"metadata":{"id":"WDQ_jbQ4fGUT"},"execution_count":null,"outputs":[]},{"cell_type":"code","source":["def solve(self, n):\n"," n = 5\n"," n = len(lst)\n"," lst = [i for i in len(range())]\n"," for i in range(lst):\n"," print(sum(i))"],"metadata":{"id":"r1d7p9i5jkiX"},"execution_count":null,"outputs":[]},{"cell_type":"code","source":["last = \n","lst = [i +2 for in range(1,)]"],"metadata":{"id":"3m0HPpRgn4p4"},"execution_count":null,"outputs":[]},{"cell_type":"code","source":["length = n #the amount of items in array\n","list = a"],"metadata":{"id":"Zvycj7-MwnIk"},"execution_count":null,"outputs":[]},{"cell_type":"code","source":[],"metadata":{"id":"ghbZRMzWj0Is"},"execution_count":null,"outputs":[]}]}