Skip to content

Commit 4a935e3

Browse files
Adding worded questions. They're still not added to README
1 parent b1a0fde commit 4a935e3

12 files changed

Lines changed: 684 additions & 4 deletions

solutions/python/fibonacci.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,3 +30,22 @@ def fibonacci_comprehension(limit):
3030
[sequence.append(sequence[i] + sequence[i-1]) for i in range(1, limit)]
3131

3232
return sequence[-1]
33+
34+
def fibonacci_generator():
35+
""" fibonacci sequence using a generator."""
36+
a, b = 0, 1
37+
yield a
38+
yield b
39+
while True:
40+
a, b = b, b + a
41+
yield b
42+
43+
if __name__ == '__main__:'
44+
45+
# Use generator version
46+
for i,value in zip(range(15),fibonacci_generator()):
47+
print(value)
48+
49+
# Cant use this way. You will just get 0's
50+
for _ in range(15):
51+
print(next(fibonacci_generator())) # we just keep hitting the first "yeild a"

worded_questions/README.md

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,20 +13,25 @@
1313
| CATEGORY | EASY | MEDIUM | HARD | EXPERT | MASTER |
1414
|---------------|-------------------|-----------------------|-----------------------|---------------------------|--------------------|
1515
| **Uncategorized** | [Easy](#uncatergorized-easy) | [Medium](#uncatergorized-med) | [Hard](#uncatergorized-hard) | [Expert](#uncatergorized-expert) | |
16-
| **Regex** | [Easy](#regex-easy) | [Medium](#regex-med) | [Hard](#regex-hard) | [Expert](#regex-expert) | |
17-
| | | | | | | |
16+
| **Regex** | [Easy](#regex-easy) | [Medium](#regex-medium) | [Hard](#regex-hard) | [Expert](#regex-expert) | |
17+
| **Threading** | [Easy](#threading-easy) | | | | | |
1818

1919
# QUESTIONS
2020

2121
## EASY
2222

23+
#### Regex Easy
24+
25+
#### Regex Medium
26+
2327
#### Uncategorized Easy
2428

2529
* **[What is Python really? Compare with other languages.](/answers/what-is-python-really.ipynb)**
30+
* **[What is Monkey Patching and is it a good idea?](/answers/what-is-monkey-patching-is-it-a-good-idea.ipynb)**
2631

27-
#### Regex Easy
32+
#### Threading Easy
2833

29-
#### Regex Medium
34+
* **[Is multi-threading in python a good idea?](/answers/is-multithreading-in-python-a-good-idea.ipynb)**
3035

3136
## MEDIUM
3237

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"metadata": {},
6+
"source": [
7+
"## Suppose you have a list whose contents don't matter. Show me three different ways of fetching every third item in the list."
8+
]
9+
},
10+
{
11+
"cell_type": "code",
12+
"execution_count": null,
13+
"metadata": {
14+
"collapsed": true
15+
},
16+
"outputs": [],
17+
"source": [
18+
"every_third = [] \n",
19+
"for index, item in enumerate(items, 1):\n",
20+
" if index % 3 == 0: every_third.append(item)"
21+
]
22+
},
23+
{
24+
"cell_type": "code",
25+
"execution_count": null,
26+
"metadata": {
27+
"collapsed": true
28+
},
29+
"outputs": [],
30+
"source": []
31+
}
32+
],
33+
"metadata": {
34+
"kernelspec": {
35+
"display_name": "Python 3",
36+
"language": "python",
37+
"name": "python3"
38+
},
39+
"language_info": {
40+
"codemirror_mode": {
41+
"name": "ipython",
42+
"version": 3
43+
},
44+
"file_extension": ".py",
45+
"mimetype": "text/x-python",
46+
"name": "python",
47+
"nbconvert_exporter": "python",
48+
"pygments_lexer": "ipython3",
49+
"version": "3.5.1"
50+
}
51+
},
52+
"nbformat": 4,
53+
"nbformat_minor": 0
54+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"metadata": {},
6+
"source": [
7+
"## What is the difference between __ getattr __ and __getattribute __?"
8+
]
9+
},
10+
{
11+
"cell_type": "markdown",
12+
"metadata": {},
13+
"source": [
14+
"A key difference between __getattr__ and __getattribute__ is that __getattr__ is only invoked if the attribute wasn't found the usual ways. It's good for implementing a fallback for missing attributes, and is probably the one of two you want.\n",
15+
"\n",
16+
"__getattribute__ is invoked before looking at the actual attributes on the object, and so can be tricky to implement correctly. You can end up in infinite recursions very easily.\n",
17+
"\n",
18+
"New-style classes derive from object, old-style classes are those in Python 2.x with no explicit base class. But the distinction between old-style and new-style classes is not the important one when choosing between __getattr__ and __getattribute__.\n",
19+
"\n",
20+
"You almost certainly want __getattr__."
21+
]
22+
},
23+
{
24+
"cell_type": "code",
25+
"execution_count": null,
26+
"metadata": {
27+
"collapsed": true
28+
},
29+
"outputs": [],
30+
"source": [
31+
"# TODO: Put some example code."
32+
]
33+
}
34+
],
35+
"metadata": {
36+
"kernelspec": {
37+
"display_name": "Python 3",
38+
"language": "python",
39+
"name": "python3"
40+
},
41+
"language_info": {
42+
"codemirror_mode": {
43+
"name": "ipython",
44+
"version": 3
45+
},
46+
"file_extension": ".py",
47+
"mimetype": "text/x-python",
48+
"name": "python",
49+
"nbconvert_exporter": "python",
50+
"pygments_lexer": "ipython3",
51+
"version": "3.5.1"
52+
}
53+
},
54+
"nbformat": 4,
55+
"nbformat_minor": 0
56+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "code",
5+
"execution_count": null,
6+
"metadata": {
7+
"collapsed": true
8+
},
9+
"outputs": [],
10+
"source": [
11+
"## How does Python's garbage collection work?"
12+
]
13+
}
14+
],
15+
"metadata": {
16+
"kernelspec": {
17+
"display_name": "Python 3",
18+
"language": "python",
19+
"name": "python3"
20+
},
21+
"language_info": {
22+
"codemirror_mode": {
23+
"name": "ipython",
24+
"version": 3
25+
},
26+
"file_extension": ".py",
27+
"mimetype": "text/x-python",
28+
"name": "python",
29+
"nbconvert_exporter": "python",
30+
"pygments_lexer": "ipython3",
31+
"version": "3.5.1"
32+
}
33+
},
34+
"nbformat": 4,
35+
"nbformat_minor": 0
36+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"metadata": {},
6+
"source": [
7+
"## How to read a 8GB file in python?"
8+
]
9+
}
10+
],
11+
"metadata": {
12+
"kernelspec": {
13+
"display_name": "Python 3",
14+
"language": "python",
15+
"name": "python3"
16+
},
17+
"language_info": {
18+
"codemirror_mode": {
19+
"name": "ipython",
20+
"version": 3
21+
},
22+
"file_extension": ".py",
23+
"mimetype": "text/x-python",
24+
"name": "python",
25+
"nbconvert_exporter": "python",
26+
"pygments_lexer": "ipython3",
27+
"version": "3.5.1"
28+
}
29+
},
30+
"nbformat": 4,
31+
"nbformat_minor": 0
32+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"metadata": {},
6+
"source": [
7+
"## Python and multi-threading. Is it a good idea? List some ways to get some Python code to run in a parallel way.\n"
8+
]
9+
},
10+
{
11+
"cell_type": "markdown",
12+
"metadata": {},
13+
"source": [
14+
"Python doesn't allow multi-threading in the truest sense of the word. It has a multi-threading package but if you want to multi-thread to speed your code up, then it's usually not a good idea to use it. Python has a construct called the Global Interpreter Lock (GIL). The GIL makes sure that only one of your 'threads' can execute at any one time. A thread acquires the GIL, does a little work, then passes the GIL onto the next thread. This happens very quickly so to the human eye it may seem like your threads are executing in parallel, but they are really just taking turns using the same CPU core. All this GIL passing adds overhead to execution. This means that if you want to make your code run faster then using the threading package often isn't a good idea.\n",
15+
"\n",
16+
"There are reasons to use Python's threading package. If you want to run some things simultaneously, and efficiency is not a concern, then it's totally fine and convenient. Or if you are running code that needs to wait for something (like some IO) then it could make a lot of sense. But the threading library wont let you use extra CPU cores.\n",
17+
"\n",
18+
"Multi-threading can be outsourced to the operating system (by doing multi-processing), some external application that calls your Python code (eg, Spark or Hadoop), or some code that your Python code calls (eg: you could have your Python code call a C function that does the expensive multi-threaded stuff).\n",
19+
"\n",
20+
"### Why is this Important?\n",
21+
"\n",
22+
"Because the GIL is an A-hole. Lots of people spend a lot of time trying to find bottlenecks in their fancy Python multi-threaded code before they learn what the GIL is."
23+
]
24+
}
25+
],
26+
"metadata": {
27+
"kernelspec": {
28+
"display_name": "Python 3",
29+
"language": "python",
30+
"name": "python3"
31+
},
32+
"language_info": {
33+
"codemirror_mode": {
34+
"name": "ipython",
35+
"version": 3
36+
},
37+
"file_extension": ".py",
38+
"mimetype": "text/x-python",
39+
"name": "python",
40+
"nbconvert_exporter": "python",
41+
"pygments_lexer": "ipython3",
42+
"version": "3.5.1"
43+
}
44+
},
45+
"nbformat": 4,
46+
"nbformat_minor": 0
47+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"metadata": {},
6+
"source": [
7+
"## I'm getting a maximum recursion depth error for a function. What does this mean? How can I mitigate the problem?"
8+
]
9+
}
10+
],
11+
"metadata": {
12+
"kernelspec": {
13+
"display_name": "Python 3",
14+
"language": "python",
15+
"name": "python3"
16+
},
17+
"language_info": {
18+
"codemirror_mode": {
19+
"name": "ipython",
20+
"version": 3
21+
},
22+
"file_extension": ".py",
23+
"mimetype": "text/x-python",
24+
"name": "python",
25+
"nbconvert_exporter": "python",
26+
"pygments_lexer": "ipython3",
27+
"version": "3.5.1"
28+
}
29+
},
30+
"nbformat": 4,
31+
"nbformat_minor": 0
32+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"metadata": {},
6+
"source": [
7+
"## Tell me about the GIL. How does it impact concurrency in Python? What kinds of applications does it impact more than others?"
8+
]
9+
},
10+
{
11+
"cell_type": "code",
12+
"execution_count": null,
13+
"metadata": {
14+
"collapsed": true
15+
},
16+
"outputs": [],
17+
"source": []
18+
}
19+
],
20+
"metadata": {
21+
"kernelspec": {
22+
"display_name": "Python 3",
23+
"language": "python",
24+
"name": "python3"
25+
},
26+
"language_info": {
27+
"codemirror_mode": {
28+
"name": "ipython",
29+
"version": 3
30+
},
31+
"file_extension": ".py",
32+
"mimetype": "text/x-python",
33+
"name": "python",
34+
"nbconvert_exporter": "python",
35+
"pygments_lexer": "ipython3",
36+
"version": "3.5.1"
37+
}
38+
},
39+
"nbformat": 4,
40+
"nbformat_minor": 0
41+
}

0 commit comments

Comments
 (0)