Last active
August 6, 2018 22:12
-
-
Save lozhn/ad8ca7707d470d7d682a4b931cf239ff to your computer and use it in GitHub Desktop.
codechallenge
This file contains hidden or 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
{ | |
"cells": [ | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"### 1. Implement a function that takes two integers a and b and returns their product, without using the '*' operator.\n" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 1, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"True" | |
] | |
}, | |
"execution_count": 1, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"def multiply(a, b):\n", | |
" res = 0\n", | |
" while a > 0:\n", | |
" res += b\n", | |
" a -= 1\n", | |
" return res\n", | |
" \n", | |
"multiply(4, 5) == 20" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"### 2. Implement a function that takes a sorted array of integers A and an integer x and returns the position of x in A or -1 if x is not in A.\n" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 34, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"[1, 3, 5, 7, 9]\n", | |
"not found 0 result -1\n", | |
"found '1' at index 0\n", | |
"not found 2 result -1\n", | |
"found '3' at index 1\n", | |
"not found 4 result -1\n", | |
"found '5' at index 2\n", | |
"not found 6 result -1\n", | |
"found '7' at index 3\n", | |
"not found 8 result -1\n", | |
"found '9' at index 4\n" | |
] | |
} | |
], | |
"source": [ | |
"def search(arr, a):\n", | |
" if not arr:\n", | |
" return -1\n", | |
" if arr[0] > a or arr[-1] < a:\n", | |
" return -1\n", | |
" \n", | |
" def _search(s, e): \n", | |
" m = (s + e) // 2\n", | |
"\n", | |
" if s == m and arr[s] != a:\n", | |
" return -1\n", | |
" if arr[s] == a:\n", | |
" return s\n", | |
" if arr[m] == a:\n", | |
" return m\n", | |
" if arr[e] == a:\n", | |
" return e\n", | |
" \n", | |
" if arr[m] > a:\n", | |
" return _search(s, m)\n", | |
" if arr[m] < a:\n", | |
" return _search(m, e)\n", | |
" \n", | |
" return _search(0, len(arr)-1)\n", | |
"\n", | |
"test = [i for i in range(10) if i % 2]\n", | |
"print(test)\n", | |
"for i in range(10):\n", | |
" res = search(test, i)\n", | |
" if i % 2 and res == i // 2:\n", | |
" print(f\"found '{i}' at index {res}\")\n", | |
" else:\n", | |
" print(f\"not found {i} result {res}\")" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"### 3. Implement a function that takes two strings a and b and returns a boolean indicating whether a is an anagram of b. Comment on your implementation." | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 68, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"(True, False, True, False)" | |
] | |
}, | |
"execution_count": 68, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"\"\"\" Based on this - https://en.wikipedia.org/wiki/Prime_number\n", | |
"0. Anagrams has the same length\n", | |
"1. Substitue each char with a corresponding prime number.\n", | |
"2. Compute the product of primes given a string of chars.\n", | |
"3. The product is commutative thus the order of chars doesn't matter.\n", | |
"4. The product of prime numbers has no other divisors except for its factors\n", | |
"\n", | |
"e.g.\n", | |
"abba = 2*3*3*2\n", | |
"bbaa = 3*3*2*2\n", | |
"\"\"\"\n", | |
"\n", | |
"def anagram(a, b):\n", | |
" if len(a) != len(b):\n", | |
" return False\n", | |
" primes = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101]\n", | |
" def _prime_for_char(c):\n", | |
" return primes[ord(c.lower())-97] # a = 97, b = 98...\n", | |
" product_a, product_b = 1, 1\n", | |
" for i, j in zip(a, b):\n", | |
" product_a *= _prime_for_char(i)\n", | |
" product_b *= _prime_for_char(j)\n", | |
" return product_a == product_b\n", | |
"\n", | |
"anagram(\"abba\", \"baba\"), anagram(\"abbd\", \"abbcd\"), anagram(\"xyx\", \"xxy\"), anagram(\"\", \"baba\")" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"### 4. Design a simple REST-api for a small dance studio. Users can login, book a specific date and time to have a dance lesson, pay by credit card or by paypal and rare their lesson after they are done." | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"public\n", | |
"- `/auth` \n", | |
" - `/sign_up` - **POST**: creates new user, sends confirmation\n", | |
" - `/sign_in` - **POST**: creates and returns a session or a token and signs in\n", | |
" - `/sign_out` - **DELETE**: destroy session and logs out\n", | |
" \n", | |
"protected (requires session or token)\n", | |
"- `/bookings` \n", | |
" - `/` - **GET**: returns all bookings, **POST**: creates new booking\n", | |
" - `/:id` - **GET**: returns repr of the booking, **PUT**: updates, **DELETE**: destroys\n", | |
" - `/:id/rate` - **GET**: returns rate if exists, **POST**: adds rate, **PUT**: allows to update rate only X minutes after the creation\n", | |
" \n", | |
" \n", | |
"- `/pay` \n", | |
" - `/:provider{paypal,card}` - **POST**: creates payment (takes time to verify payment) with associated booking_id" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"### 5. If you were to follow a product-first approach, how would your answer to (4) change, if at all." | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"I guess I wouldn't change that. \n", | |
"\n", | |
"I think API design has to meet the needs and \"product-frist approach\", in my opinion, is about a way you think at higher level of development rather than routing, json and etc. \n", | |
"\n", | |
"In other words, Product-first is to make great products like Slack and the way API is built doesn't matter at that point. " | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"### 6. Describe in detail how would you unit-test the API in (4) and how would you do proper integration testing. Describe at least the basic test cases." | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"**Intergation**\n", | |
"\n", | |
"- POST('login', 'pass') `/auth/sign_up`\n", | |
" SHOULD SUCCESS if 'login' and 'pass' are valid AND not in the DB \n", | |
" SHOULD FAIL OTHERWISE\n", | |
"- ...\n", | |
"- POST('auth-headers', 'date-time', 'dance-lesson-type', 'dance-lesson-teacher') `/bookings`\n", | |
" SHOULD SUCCESS if 'auth-headers' are valid AND 'dance-lesson-teacher' has available time slot AND 'dance-lesson-teacher' can teach 'dance-lesson-type'\n", | |
" SHOULD FAIL OTHERWISE\n", | |
"\n", | |
"---\n", | |
"\n", | |
"**Unit**\n", | |
"\n", | |
"- `SessionsController.new.create('login', 'pass')` SHOULD SUCCESS if 'login' and 'pass' are VALID. FAIL OTHERWISE\n", | |
"- `booking.add_rate(rate)` SHOULD SUCCESS if 'booking' IS PAYED, IS VISITED, HAS NO RATE, FAIL OTHERWISE" | |
] | |
} | |
], | |
"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.7.0" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 2 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment