Skip to content

Commit

Permalink
[chore] replace py files with notebooks
Browse files Browse the repository at this point in the history
  • Loading branch information
gitgik committed Feb 25, 2021
1 parent a148803 commit 613404e
Show file tree
Hide file tree
Showing 10 changed files with 515 additions and 158 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ A catalogue of data structures implementation + algorithms and coding problems a

## Tries

- [Trie implementation](tries/trie.ipynb)
- [Trie implementation](tries/trie.md)

## Others

Expand Down
94 changes: 94 additions & 0 deletions strings/caesar_cipher.ipynb
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": []
}
]
}
29 changes: 0 additions & 29 deletions strings/caesar_cipher.py

This file was deleted.

116 changes: 116 additions & 0 deletions strings/delimiter_order.ipynb
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": []
}
]
}
47 changes: 0 additions & 47 deletions strings/is_palindrome.py

This file was deleted.

44 changes: 0 additions & 44 deletions strings/maintain_delimiter_order.py

This file was deleted.

Loading

0 comments on commit 613404e

Please sign in to comment.