From 08977ce8d78634c2840f6ac3e6528bdc79ee4224 Mon Sep 17 00:00:00 2001 From: mavsun Date: Mon, 22 Jan 2024 19:36:33 -0500 Subject: [PATCH 1/8] Re-Implemented LinkedList and completed Exercises --- .../3_LinkedList/linkedlist_steve.ipynb | 188 ++++++++++++++++++ 1 file changed, 188 insertions(+) create mode 100644 data_structures/3_LinkedList/linkedlist_steve.ipynb diff --git a/data_structures/3_LinkedList/linkedlist_steve.ipynb b/data_structures/3_LinkedList/linkedlist_steve.ipynb new file mode 100644 index 0000000..4c3d4c5 --- /dev/null +++ b/data_structures/3_LinkedList/linkedlist_steve.ipynb @@ -0,0 +1,188 @@ +{ + "cells": [ + { + "attachments": {}, + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Linked list are series of nodes that makes it easier to insert or remove elements from any where in the middle of the list" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": {}, + "outputs": [], + "source": [ + "class Node:\n", + " # each node in a linked list has a value and the index to the next value\n", + " def __init__(self, val = None, idx = None):\n", + " self.value = val\n", + " self.next = idx" + ] + }, + { + "cell_type": "code", + "execution_count": 126, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "1--2--4--4--5\n", + "0--1--2--3--4--4--5\n", + "1--2--3--4--5\n", + "1--2--3--4--5--6\n", + "1--2--4--5\n", + "==========\n", + "L.L. Length = 4\n", + "1--2--4--5\n" + ] + } + ], + "source": [ + "class LinkedList:\n", + "\n", + " ### this implmentation makes LL look kinda like this:\n", + " ### [1,[2,[3,[4,[5,None]]]]]\n", + "\n", + " def __init__(self):\n", + " self.head = None\n", + "\n", + " def print_ll(self):\n", + " node = self.head\n", + " ll = ''\n", + " \n", + " while node.next:\n", + " ll += str(node.value) + '--'\n", + " node = node.next\n", + "\n", + " ll += str(node.value)\n", + " print(ll)\n", + " \n", + " def get_len(self):\n", + " count = 0\n", + " node = self.head\n", + "\n", + " while node:\n", + " node = node.next\n", + " count += 1\n", + " \n", + " print('L.L. Length =',count)\n", + " \n", + " def insert_at_bgn(self, data):\n", + " self.head = Node(data, self.head)\n", + "\n", + " def insert_at_end(self, data):\n", + " node = self.head \n", + "\n", + " while node.next:\n", + " node = node.next \n", + " node.next = Node(data, None)\n", + "\n", + " def insert_values(self, node_list):\n", + " for elem in node_list:\n", + " self.insert_at_end(elem)\n", + "\n", + " def insert_at(self, data, idx):\n", + " node = self.head\n", + " count = 0\n", + "\n", + " if idx == 0:\n", + " self.insert_at_bgn(data)\n", + "\n", + " else:\n", + " # stopping at the previous node\n", + " # does not handle 0\n", + " while count != idx-1: \n", + " node = node.next\n", + " count += 1\n", + "\n", + " new_node = Node(data, node.next)\n", + " node.next = new_node\n", + "\n", + " def remove_at(self, idx):\n", + " node = self.head\n", + " \n", + " if idx == 0:\n", + " self.head = self.head.next\n", + "\n", + " else:\n", + " count = 0\n", + " while count <= idx-1: # takes me to the node immediately before the removal node\n", + " node = node.next\n", + " count += 1\n", + " node.next = node.next.next\n", + "\n", + " def insert_after_value(self, val, data):\n", + " node = self.head\n", + "\n", + " idx = 0\n", + " while node.value != val:\n", + " node = node.next\n", + " idx += 1\n", + " # lacking an end node handle\n", + " self.insert_at(data, idx+1)\n", + "\n", + "\n", + " def remove_by_value(self, val): \n", + " node = self.head\n", + "\n", + " idx = 0\n", + " while node.value != val:\n", + " node = node.next\n", + " idx += 1\n", + " self.remove_at(idx-1)\n", + "\n", + "if __name__ == '__main__':\n", + " ll = LinkedList()\n", + "\n", + " ll.insert_at_bgn(2)\n", + " ll.insert_at_bgn(1)\n", + " ll.insert_values([4,4,5])\n", + " ll.print_ll()\n", + "\n", + " ll.insert_at(3,2)\n", + " ll.insert_at(0,0)\n", + " ll.print_ll()\n", + "\n", + " ll.remove_at(4)\n", + " ll.remove_at(0)\n", + " ll.print_ll()\n", + "\n", + " ll.insert_after_value(5, 6)\n", + " ll.print_ll()\n", + " ll.remove_by_value(6)\n", + " ll.remove_by_value(3)\n", + " ll.print_ll()\n", + "\n", + " print('='*10)\n", + " ll.get_len()\n", + " ll.print_ll()\n" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3", + "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.0" + }, + "orig_nbformat": 4 + }, + "nbformat": 4, + "nbformat_minor": 2 +} From 25f96ae0542702f99e040261a3dd4213659ca573 Mon Sep 17 00:00:00 2001 From: mavsun Date: Sun, 28 Jan 2024 00:26:13 -0500 Subject: [PATCH 2/8] Jan-28 changes, solutions --- .../HashTable_incomplete.ipynb | 82 +++++++++++++++++++ Steve_Implementation/Stack&Queue.ipynb | 82 +++++++++++++++++++ .../linkedlist.ipynb | 0 3 files changed, 164 insertions(+) create mode 100644 Steve_Implementation/HashTable_incomplete.ipynb create mode 100644 Steve_Implementation/Stack&Queue.ipynb rename data_structures/3_LinkedList/linkedlist_steve.ipynb => Steve_Implementation/linkedlist.ipynb (100%) diff --git a/Steve_Implementation/HashTable_incomplete.ipynb b/Steve_Implementation/HashTable_incomplete.ipynb new file mode 100644 index 0000000..a2b21bf --- /dev/null +++ b/Steve_Implementation/HashTable_incomplete.ipynb @@ -0,0 +1,82 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": 2, + "metadata": {}, + "outputs": [], + "source": [ + "class HashTable:\n", + " def __init__(self):\n", + " self.Idx = 100\n", + " self.Data = [None for i in range(self.Idx)]\n", + "\n", + " def HashFunction(self, key):\n", + " hash = 0\n", + " for i in key:\n", + " hash += ord(key)\n", + " return hash % self.Idx\n", + " \n", + " def __getitem__(self, key):\n", + " hash = self.HashFunction(key)\n", + "\n", + " return self.Data[hash]\n", + " \n", + " def __setitem__(self, key, val):\n", + " hash = self.HashFunction(key)\n", + " self.Data[hash] = val\n", + "\n", + " def __delitem__(self, key):\n", + " hash = self.HashFunction(key)\n", + " self.Daata[hash] = None\n", + "\n", + "if __name__ == '__main__':\n", + " ht = HashTable()\n", + "\n", + " ht['1'] = 1" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "1" + ] + }, + "execution_count": 3, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "ht['1']" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3", + "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.0" + }, + "orig_nbformat": 4 + }, + "nbformat": 4, + "nbformat_minor": 2 +} diff --git a/Steve_Implementation/Stack&Queue.ipynb b/Steve_Implementation/Stack&Queue.ipynb new file mode 100644 index 0000000..1f76394 --- /dev/null +++ b/Steve_Implementation/Stack&Queue.ipynb @@ -0,0 +1,82 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": 13, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "10\n", + "12\n", + "10\n", + "False\n", + "True\n" + ] + } + ], + "source": [ + "from collections import deque\n", + "\n", + "class Stack:\n", + " def __init__(self):\n", + " self.data = deque()\n", + " \n", + " def push(self,val):\n", + " self.data.appendleft(val)\n", + "\n", + " def pop(self):\n", + " self.data.popleft()\n", + "\n", + " def peek(self):\n", + " return self.data[0]\n", + " \n", + " def is_empty(self):\n", + " if len(self.data) == 0: return True\n", + " else: return False\n", + "\n", + " def size(self):\n", + " return len(self.data)\n", + "\n", + "\n", + "if __name__ == '__main__':\n", + " stack = Stack()\n", + "\n", + " stack.push(10)\n", + " print(stack.peek())\n", + " stack.push(12)\n", + " print(stack.peek())\n", + "\n", + " stack.pop()\n", + " print(stack.peek())\n", + " print(stack.is_empty())\n", + " stack.pop()\n", + " print(stack.is_empty())" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3", + "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.0" + }, + "orig_nbformat": 4 + }, + "nbformat": 4, + "nbformat_minor": 2 +} diff --git a/data_structures/3_LinkedList/linkedlist_steve.ipynb b/Steve_Implementation/linkedlist.ipynb similarity index 100% rename from data_structures/3_LinkedList/linkedlist_steve.ipynb rename to Steve_Implementation/linkedlist.ipynb From 43e855101dac9508b243ebf4c5d8c46bcef2d48e Mon Sep 17 00:00:00 2001 From: mavsun Date: Sun, 28 Jan 2024 00:27:24 -0500 Subject: [PATCH 3/8] Jan-28 changes and solutions. --- .../leetcode worked solutions.ipynb | 152 ++++++++++++++++++ data_structures/5_Stack/5_stack.ipynb | 2 +- 2 files changed, 153 insertions(+), 1 deletion(-) create mode 100644 Steve_Implementation/leetcode worked solutions.ipynb diff --git a/Steve_Implementation/leetcode worked solutions.ipynb b/Steve_Implementation/leetcode worked solutions.ipynb new file mode 100644 index 0000000..f133b55 --- /dev/null +++ b/Steve_Implementation/leetcode worked solutions.ipynb @@ -0,0 +1,152 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": 1, + "metadata": {}, + "outputs": [], + "source": [ + "### Leetcode 217\n", + "\n", + "class Solution(object):\n", + " def containsDuplicate(self, nums):\n", + " \"\"\"\n", + " :type nums: List[int]\n", + " :rtype: bool\n", + " \"\"\"\n", + " if len(set(nums)) != len(nums):\n", + " return True\n", + " else:\n", + " return False" + ] + }, + { + "cell_type": "code", + "execution_count": 13, + "metadata": {}, + "outputs": [], + "source": [ + "### Leetcode 242\n", + "\n", + "class Solution(object):\n", + " def isAnagram(self, s, t):\n", + " \"\"\"\n", + " :type s: str\n", + " :type t: str\n", + " :rtype: bool\n", + " \"\"\"\n", + " \n", + " # phase 1\n", + " # a = {i:s.count(i) for i in sorted(s)}\n", + " # b = {i:t.count(i) for i in sorted(t)}\n", + "\n", + " # phase 2\n", + " if sorted(s) == sorted(t): return True\n", + " else: False" + ] + }, + { + "cell_type": "code", + "execution_count": 14, + "metadata": {}, + "outputs": [], + "source": [ + "### Leetcode 1\n", + "\n", + "class Solution(object):\n", + " def twoSum(self, nums, target):\n", + " \"\"\"\n", + " :type nums: List[int]\n", + " :type target: int\n", + " :rtype: List[int]\n", + " \"\"\"\n", + " \n", + " a = 0\n", + " b = 1\n", + " while nums[a] + nums[b] != target:\n", + " b += 1\n", + "\n", + " ### how to optimize this bit?????\n", + " if b == len(nums):\n", + " a += 1\n", + " b = a + 1\n", + "\n", + " return [a,b]" + ] + }, + { + "cell_type": "code", + "execution_count": 29, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "[['tea', 'ate', 'eat'],\n", + " ['ate', 'tea'],\n", + " ['nat', 'tan'],\n", + " ['ate'],\n", + " ['nat'],\n", + " ['bat']]" + ] + }, + "execution_count": 29, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "### Leetcode 49\n", + "\n", + "strs = [\"eat\",\"tea\",\"tan\",\"ate\",\"nat\",\"bat\"]\n", + "\n", + "l = []\n", + "ll = []\n", + "a = 0\n", + "b = a + 1\n", + "while a != len(strs):\n", + " if a == len(strs) - 1:\n", + " ll.append([strs[a]])\n", + " break\n", + "\n", + " elif sorted(strs[a]) == sorted(strs[b]):\n", + " l.append(strs[b])\n", + "\n", + " b += 1\n", + "\n", + " if b == len(strs):\n", + " l.append(strs[a])\n", + " ll.append(l)\n", + " l = []\n", + "\n", + " a += 1\n", + " b = a + 1\n", + "\n", + "ll\n", + "\n", + " " + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3", + "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.0" + } + }, + "nbformat": 4, + "nbformat_minor": 2 +} diff --git a/data_structures/5_Stack/5_stack.ipynb b/data_structures/5_Stack/5_stack.ipynb index 4f8ad4b..2e6ff80 100644 --- a/data_structures/5_Stack/5_stack.ipynb +++ b/data_structures/5_Stack/5_stack.ipynb @@ -661,7 +661,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.7.3" + "version": "3.10.0" } }, "nbformat": 4, From b309c2046ac315376a74206b3a4093acd9bd2e8e Mon Sep 17 00:00:00 2001 From: mavsun Date: Sun, 28 Jan 2024 00:52:59 -0500 Subject: [PATCH 4/8] phase 1 solution for leetcode49 --- Steve_Implementation/leetcode worked solutions.ipynb | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Steve_Implementation/leetcode worked solutions.ipynb b/Steve_Implementation/leetcode worked solutions.ipynb index f133b55..08fd9d1 100644 --- a/Steve_Implementation/leetcode worked solutions.ipynb +++ b/Steve_Implementation/leetcode worked solutions.ipynb @@ -76,7 +76,7 @@ }, { "cell_type": "code", - "execution_count": 29, + "execution_count": null, "metadata": {}, "outputs": [ { @@ -111,6 +111,7 @@ "\n", " elif sorted(strs[a]) == sorted(strs[b]):\n", " l.append(strs[b])\n", + " strs.remove(strs[a])\n", "\n", " b += 1\n", "\n", From 0f7d7e13bd7af279e97317a89b188167a5e397de Mon Sep 17 00:00:00 2001 From: mavsun Date: Sun, 28 Jan 2024 00:57:28 -0500 Subject: [PATCH 5/8] phase 1 solution leetcode 49 --- .../leetcode worked solutions.ipynb | 20 +++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/Steve_Implementation/leetcode worked solutions.ipynb b/Steve_Implementation/leetcode worked solutions.ipynb index 08fd9d1..c9108f0 100644 --- a/Steve_Implementation/leetcode worked solutions.ipynb +++ b/Steve_Implementation/leetcode worked solutions.ipynb @@ -76,21 +76,16 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 1, "metadata": {}, "outputs": [ { "data": { "text/plain": [ - "[['tea', 'ate', 'eat'],\n", - " ['ate', 'tea'],\n", - " ['nat', 'tan'],\n", - " ['ate'],\n", - " ['nat'],\n", - " ['bat']]" + "[['tea', 'ate', 'tan'], ['ate'], ['nat'], ['bat']]" ] }, - "execution_count": 29, + "execution_count": 1, "metadata": {}, "output_type": "execute_result" } @@ -131,7 +126,7 @@ ], "metadata": { "kernelspec": { - "display_name": "Python 3", + "display_name": "Python 3.9.6 64-bit", "language": "python", "name": "python3" }, @@ -145,7 +140,12 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.10.0" + "version": "3.9.6" + }, + "vscode": { + "interpreter": { + "hash": "31f2aee4e71d21fbe5cf8b01ff0e069b9275f58929596ceb00d14d90e3e16cd6" + } } }, "nbformat": 4, From 7199d92495194314827cf8d6c3ae6c6a6ebd9526 Mon Sep 17 00:00:00 2001 From: mavsun Date: Sun, 28 Jan 2024 02:25:08 -0500 Subject: [PATCH 6/8] final solution Leetcode 49 --- .../leetcode worked solutions.ipynb | 41 ++++++------------- 1 file changed, 13 insertions(+), 28 deletions(-) diff --git a/Steve_Implementation/leetcode worked solutions.ipynb b/Steve_Implementation/leetcode worked solutions.ipynb index c9108f0..5e7b8fc 100644 --- a/Steve_Implementation/leetcode worked solutions.ipynb +++ b/Steve_Implementation/leetcode worked solutions.ipynb @@ -76,16 +76,16 @@ }, { "cell_type": "code", - "execution_count": 1, + "execution_count": 49, "metadata": {}, "outputs": [ { "data": { "text/plain": [ - "[['tea', 'ate', 'tan'], ['ate'], ['nat'], ['bat']]" + "[['tan', 'nat'], ['eat', 'tea', 'ate'], ['bat']]" ] }, - "execution_count": 1, + "execution_count": 49, "metadata": {}, "output_type": "execute_result" } @@ -93,34 +93,19 @@ "source": [ "### Leetcode 49\n", "\n", - "strs = [\"eat\",\"tea\",\"tan\",\"ate\",\"nat\",\"bat\"]\n", - "\n", - "l = []\n", - "ll = []\n", - "a = 0\n", - "b = a + 1\n", - "while a != len(strs):\n", - " if a == len(strs) - 1:\n", - " ll.append([strs[a]])\n", - " break\n", - "\n", - " elif sorted(strs[a]) == sorted(strs[b]):\n", - " l.append(strs[b])\n", - " strs.remove(strs[a])\n", - "\n", - " b += 1\n", - "\n", - " if b == len(strs):\n", - " l.append(strs[a])\n", - " ll.append(l)\n", - " l = []\n", + "class Solution(object):\n", + " def groupAnagrams(self, strs):\n", + " \"\"\"\n", + " :type strs: List[str]\n", + " :rtype: List[List[str]]\n", + " \"\"\" \n", "\n", - " a += 1\n", - " b = a + 1\n", + " hash = {j:[] for j in set([''.join(sorted(i)) for i in strs])}\n", "\n", - "ll\n", + " for i in strs:\n", + " hash[''.join(sorted(i))].append(i)\n", "\n", - " " + " return list(hash.values())\n" ] } ], From 815c308ec28fc28ef3c6665ca97ceeaa1d268969 Mon Sep 17 00:00:00 2001 From: mavsun Date: Sun, 28 Jan 2024 03:33:51 -0500 Subject: [PATCH 7/8] Leetcode 347 initial solution --- .../leetcode worked solutions.ipynb | 46 +++++++++++++++++++ 1 file changed, 46 insertions(+) diff --git a/Steve_Implementation/leetcode worked solutions.ipynb b/Steve_Implementation/leetcode worked solutions.ipynb index 5e7b8fc..cffb913 100644 --- a/Steve_Implementation/leetcode worked solutions.ipynb +++ b/Steve_Implementation/leetcode worked solutions.ipynb @@ -107,6 +107,52 @@ "\n", " return list(hash.values())\n" ] + }, + { + "cell_type": "code", + "execution_count": 122, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{0: [], 1: []}\n" + ] + }, + { + "data": { + "text/plain": [ + "[1]" + ] + }, + "execution_count": 122, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "### LEETCODE 347\n", + "\n", + "### the optimal solution would have first count each element\n", + "### then flip the count and the key\n", + "### this solution does the other way around, which gets the elem from nums\n", + "### based on how many times it had appeared.\n", + "### very slow and expensive\n", + "\n", + "nums = [1] #4, -1\n", + "k = 2\n", + "\n", + "hash = {i:[] for i in range(len(nums)+1)}\n", + "print(hash)\n", + "for i in set(nums):\n", + " hash[nums.count(i)] += [i]\n", + "\n", + "L = []\n", + "for i in list(hash.values()):\n", + " L += i\n", + "L[-k:]" + ] } ], "metadata": { From 57c01360f675d084d0e4678e0c47d15a5f2d6b53 Mon Sep 17 00:00:00 2001 From: mavsun Date: Sat, 3 Feb 2024 16:43:12 -0500 Subject: [PATCH 8/8] more leet code solutions --- .../leetcode worked solutions.ipynb | 161 ++++++++++++++++++ Steve_Implementation/python practices.ipynb | 74 ++++++++ 2 files changed, 235 insertions(+) create mode 100644 Steve_Implementation/python practices.ipynb diff --git a/Steve_Implementation/leetcode worked solutions.ipynb b/Steve_Implementation/leetcode worked solutions.ipynb index cffb913..b842c9a 100644 --- a/Steve_Implementation/leetcode worked solutions.ipynb +++ b/Steve_Implementation/leetcode worked solutions.ipynb @@ -153,6 +153,167 @@ " L += i\n", "L[-k:]" ] + }, + { + "cell_type": "code", + "execution_count": 182, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[1, 2, 6, 24, 120]\n", + "6\n", + "30\n", + "120\n", + "360\n", + "720\n" + ] + }, + { + "data": { + "text/plain": [ + "[360, 240, 180, 144, 120]" + ] + }, + "execution_count": 182, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "### LEETCODE 238\n", + "\n", + "nums = [2,3,4,5,6]\n", + "\n", + "a = [1]\n", + "_a = 1\n", + "for i in range(len(nums)-1):\n", + " _a *= nums[i]\n", + " a.append(_a)\n", + "\n", + "_b = 1\n", + "for i in range(len(nums)-1,-1,-1):\n", + " a[i] *= _b\n", + " _b *= nums[i]\n", + "\n", + "print(a)" + ] + }, + { + "cell_type": "code", + "execution_count": 219, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "2\n" + ] + } + ], + "source": [ + "### 128\n", + "\n", + "nums = [0,-1]\n", + "nums2 = sorted(nums)\n", + "\n", + "if len(nums) == 0:\n", + " return 0\n", + " \n", + "else:\n", + " nums2 = sorted(nums)\n", + "\n", + " i = 0\n", + " k1 = 1\n", + " kmax = 0\n", + " while i < len(nums2)-1:\n", + "\n", + " if nums2[i]+1 == nums2[i+1]:\n", + " k1+=1\n", + " \n", + " elif nums2[i] == nums2[i+1]:\n", + " pass\n", + "\n", + " else:\n", + " if k1 > kmax: kmax = k1\n", + " k1 = 1\n", + "\n", + " i += 1\n", + "\n", + " if k1 > kmax: kmax = k1\n", + " print(kmax)\n" + ] + }, + { + "cell_type": "code", + "execution_count": 371, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "False\n" + ] + } + ], + "source": [ + "### LEETCODE 36\n", + "\n", + "L = [[\".\",\".\",\".\",\".\",\"5\",\".\",\".\",\"1\",\".\"],[\".\",\"4\",\".\",\"3\",\".\",\".\",\".\",\".\",\".\"],[\".\",\".\",\".\",\".\",\".\",\"3\",\".\",\".\",\"1\"],[\"8\",\".\",\".\",\".\",\".\",\".\",\".\",\"2\",\".\"],[\".\",\".\",\"2\",\".\",\"7\",\".\",\".\",\".\",\".\"],[\".\",\"1\",\"5\",\".\",\".\",\".\",\".\",\".\",\".\"],[\".\",\".\",\".\",\".\",\".\",\"2\",\".\",\".\",\".\"],[\".\",\"2\",\".\",\"9\",\".\",\".\",\".\",\".\",\".\"],[\".\",\".\",\"4\",\".\",\".\",\".\",\".\",\".\",\".\"]]\n", + "# L = [[\"5\",\"3\",\".\",\".\",\"7\",\".\",\".\",\".\",\".\"],[\"6\",\".\",\".\",\"1\",\"9\",\"5\",\".\",\".\",\".\"],[\".\",\"9\",\"8\",\".\",\".\",\".\",\".\",\"6\",\".\"],[\"8\",\".\",\".\",\".\",\"6\",\".\",\".\",\".\",\"3\"],[\"4\",\".\",\".\",\"8\",\".\",\"3\",\".\",\".\",\"1\"],[\"7\",\".\",\".\",\".\",\"2\",\".\",\".\",\".\",\"6\"],[\".\",\"6\",\".\",\".\",\".\",\".\",\"2\",\"8\",\".\"],[\".\",\".\",\".\",\"4\",\"1\",\"9\",\".\",\".\",\"5\"],[\".\",\".\",\".\",\".\",\"8\",\".\",\".\",\"7\",\"9\"]]\n", + "\n", + "L_T = [[L[j][i] for j in range(len(L))] for i in range(len(L[0]))]\n", + "ref = [2,5,8]\n", + "\n", + "\n", + "status = True\n", + "prefix_count = 0\n", + "for i in range(len(L)):\n", + " row_ = L[i][0: i+8-prefix_count+1]\n", + " col_ = L_T[i][i-prefix_count : i+8-prefix_count+1]\n", + "\n", + " if i in ref:\n", + " grid_step = 0\n", + "\n", + " while grid_step <= ref.index(i):\n", + " \n", + " grid_up = L[i-2-grid_step*3][i-2:i+1] + \\\n", + " L[i-1-grid_step*3][i-2:i+1] + \\\n", + " L[i-grid_step*3][i-2:i+1]\n", + "\n", + " grid_lf = L_T[i-2-grid_step*3][i-2:i+1] + \\\n", + " L_T[i-1-grid_step*3][i-2:i+1] + \\\n", + " L_T[i-grid_step*3][i-2:i+1]\n", + "\n", + " # grid check\n", + " check_up = [grid_up.count(elem) for elem in set(grid_up) if elem != '.']\n", + " check_lf = [grid_lf.count(elem) for elem in set(grid_lf) if elem != '.']\n", + "\n", + " if sum(check_up) == len(check_up) and sum(check_lf) == len(check_lf):\n", + " grid_step += 1\n", + " pass\n", + " else:\n", + " # print('grid check failed')\n", + " status = False\n", + " break\n", + "\n", + " check_r = [row_.count(i) for i in set(row_) if i != '.']\n", + " check_c = [col_.count(i) for i in set(col_) if i != '.']\n", + "\n", + " if (sum(check_r) == len(check_r)) and (sum(check_c) == len(check_c)):\n", + " pass\n", + " else:\n", + " status = False\n", + " break\n", + "\n", + " prefix_count += 1\n", + "\n", + "print(status)" + ] } ], "metadata": { diff --git a/Steve_Implementation/python practices.ipynb b/Steve_Implementation/python practices.ipynb new file mode 100644 index 0000000..298d6de --- /dev/null +++ b/Steve_Implementation/python practices.ipynb @@ -0,0 +1,74 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": 1, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "edcba\n" + ] + } + ], + "source": [ + "def spin_words(sentence):\n", + " # Your code goes here\n", + " return \" \".join([x[::-1] if len(x) >= 5 else x for x in sentence.split(\" \")])\n", + "\n", + "print('abcde'[::-1])" + ] + }, + { + "cell_type": "code", + "execution_count": 17, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "4" + ] + }, + "execution_count": 17, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "def count_bits(n):\n", + " # return sum([1 for i in bin(n)[2:] if i == '1'])\n", + " return bin(n)[2:].count('1')\n" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3.9.6 64-bit", + "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.9.6" + }, + "orig_nbformat": 4, + "vscode": { + "interpreter": { + "hash": "31f2aee4e71d21fbe5cf8b01ff0e069b9275f58929596ceb00d14d90e3e16cd6" + } + } + }, + "nbformat": 4, + "nbformat_minor": 2 +}