forked from gitgik/algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[chore] replace py files with notebooks
- Loading branch information
Showing
10 changed files
with
515 additions
and
158 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
{ | ||
"metadata": { | ||
"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.0-final" | ||
}, | ||
"orig_nbformat": 2, | ||
"kernelspec": { | ||
"name": "python3", | ||
"display_name": "Python 3.9.0 64-bit", | ||
"metadata": { | ||
"interpreter": { | ||
"hash": "aee8b7b246df8f9039afb4144a1f6fd8d2ca17a180786b69acc140d282b71a49" | ||
} | ||
} | ||
} | ||
}, | ||
"nbformat": 4, | ||
"nbformat_minor": 2, | ||
"cells": [ | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 1, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"\"\"\"Caecar Cipher implementation.\n", | ||
"\n", | ||
"Given a non-empty string of lowercase alphabetic characters, write a function that\n", | ||
"takes in the string, and an integer called a key,\n", | ||
"And returns a string obtained by shifting every letter of the input string by k positions\n", | ||
"in the alphabet, where k is the key.\n", | ||
"\n", | ||
"Sample input: \"xyz\", 2\n", | ||
"Sample output: \"zab\"\n", | ||
"\"\"\"\n", | ||
"\n", | ||
"\n", | ||
"def caesar_cipher_encryptor(string, key):\n", | ||
" \"\"\"\n", | ||
" Complexity:\n", | ||
" O(n) space\n", | ||
" O(n) time\n", | ||
" \"\"\"\n", | ||
" new_letters = []\n", | ||
" new_key = key % 26\n", | ||
" for letter in string:\n", | ||
" new_letter_code = ord(letter) + new_key\n", | ||
" if new_letter_code > 122: # z character code is 122\n", | ||
" character = chr(96 + new_letter_code % 122)\n", | ||
" new_letters.append(character)\n", | ||
" else:\n", | ||
" new_letters.append(chr(new_letter_code))\n", | ||
"\n", | ||
" return \"\".join(new_letters)" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 3, | ||
"metadata": {}, | ||
"outputs": [ | ||
{ | ||
"output_type": "execute_result", | ||
"data": { | ||
"text/plain": [ | ||
"'zab'" | ||
] | ||
}, | ||
"metadata": {}, | ||
"execution_count": 3 | ||
} | ||
], | ||
"source": [ | ||
"caesar_cipher_encryptor(\"xyz\", 2)" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [] | ||
} | ||
] | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
{ | ||
"metadata": { | ||
"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.0-final" | ||
}, | ||
"orig_nbformat": 2, | ||
"kernelspec": { | ||
"name": "python3", | ||
"display_name": "Python 3.9.0 64-bit", | ||
"metadata": { | ||
"interpreter": { | ||
"hash": "aee8b7b246df8f9039afb4144a1f6fd8d2ca17a180786b69acc140d282b71a49" | ||
} | ||
} | ||
} | ||
}, | ||
"nbformat": 4, | ||
"nbformat_minor": 2, | ||
"cells": [ | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 1, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"\"\"\"\n", | ||
"Reverse the words in a string but maintain the relative order of delimiters.\n", | ||
"\n", | ||
"For example:\n", | ||
" input = \"hello:world/here\"\n", | ||
" output = \"here:world/hello\"\n", | ||
"\n", | ||
"\"\"\"\n", | ||
"import re\n", | ||
"\n", | ||
"\n", | ||
"def reverse(string, delimiters):\n", | ||
" \"\"\"Split the words and delimiters into their respective lists.\n", | ||
" Then reverse the list of words and merge the two lists together.\n", | ||
"\n", | ||
" Complexity: O(n) time | O(n) space, where n == length of input string.\n", | ||
" \"\"\"\n", | ||
" words = re.split(f\"[{delimiters}]+\", string)\n", | ||
" not_words = re.split(f\"[^({delimiters})]+\", string)\n", | ||
"\n", | ||
" # remove last empty string if present\n", | ||
" if words[-1] == '':\n", | ||
" words = words[:-1]\n", | ||
"\n", | ||
" # NOTE: we can in built reverse but there's always another way\n", | ||
" # reversed_strings = list(reversed(words))\n", | ||
" start = 0\n", | ||
" end = len(words) - 1\n", | ||
" while start < end:\n", | ||
" words[start], words[end] = words[end], words[start]\n", | ||
" start += 1\n", | ||
" end -= 1\n", | ||
"\n", | ||
" output = []\n", | ||
" for index, delimiter in enumerate(not_words):\n", | ||
" print(index)\n", | ||
" output.append(delimiter)\n", | ||
" try:\n", | ||
" print(f\"adding {words[index]}\")\n", | ||
" output.append(words[index])\n", | ||
" except IndexError:\n", | ||
" pass\n", | ||
"\n", | ||
" return ''.join(output)" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 3, | ||
"metadata": {}, | ||
"outputs": [ | ||
{ | ||
"output_type": "stream", | ||
"name": "stdout", | ||
"text": [ | ||
"0\nadding here\n1\nadding world\n2\nadding hello\n3\n" | ||
] | ||
}, | ||
{ | ||
"output_type": "execute_result", | ||
"data": { | ||
"text/plain": [ | ||
"'here:world/hello'" | ||
] | ||
}, | ||
"metadata": {}, | ||
"execution_count": 3 | ||
} | ||
], | ||
"source": [ | ||
"reverse(\"hello:world/here\", \":/\")" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [] | ||
} | ||
] | ||
} |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.