Skip to content

Commit 8f3675f

Browse files
committed
Adds builtin function examples.
1 parent b46c399 commit 8f3675f

File tree

2 files changed

+224
-0
lines changed

2 files changed

+224
-0
lines changed

Beginers/BuiltinFunctions/problem.py

+111
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
"""Builtin functions problems.
2+
It might be useful to browse here (adjust your version to suit):
3+
https://docs.python.org/2.7/library/functions.html
4+
5+
1. Create the sequence [0, 3, 6, 9, ... N]. What is the problem if N is very large?
6+
Is there a better way if N is very large?
7+
8+
2. Find the difference between the biggest and smallest values in the list
9+
[4, 3, -9, 21, 0]
10+
11+
3. The same as 2. but use the absolute values in the list.
12+
13+
4.Convert a list:
14+
['Zero', 'One', 'Two', 'Three', 'Four', 'Five', 'Six', 'Seven', 'Eight', 'Nine']
15+
To a dictionary:
16+
{
17+
0 : 'Zero',
18+
1 : 'One',
19+
2 : 'Two',
20+
3 : 'Three',
21+
4 : 'Four',
22+
5 : 'Five',
23+
6 : 'Six',
24+
7 : 'Seven',
25+
8 : 'Eight',
26+
9 : 'Nine',
27+
}
28+
29+
5. I have two list a and b. Is there a way that I can tell if they are the same
30+
list? For example in the following case they are the same list:
31+
a = [1, 2, 3]
32+
b = a
33+
And any change to b be will be 'seen' by a.
34+
However in this case a and b are not the same list in the sense that any change
35+
to b be will NOT be 'seen' by a.
36+
a = [1, 2, 3]
37+
b = [1, 2, 3]
38+
39+
Created on 22 Feb 2016
40+
41+
@author: paulross
42+
"""
43+
import sys
44+
45+
import pytest
46+
47+
def create_sequence(N):
48+
"""Create the 3x table up to and including N."""
49+
# Your code here
50+
pass
51+
52+
def range_of_list():
53+
"""Return the difference between the largest and smallest values in a list."""
54+
x = [4, 3, -9, 21, 0]
55+
# Your code here
56+
57+
def range_of_list_abs():
58+
"""Return the difference between the largest and smallest absolute values in a list."""
59+
x = [4, 3, -9, 21, 0]
60+
# Your code here
61+
62+
def list_to_sequence_dict():
63+
"""Create a dictionary where the key is the ordinal of the object in the list
64+
and the value is the object itself. For example: {0 : 'Zero', 1 : 'One', ...}"""
65+
x = ['Zero', 'One', 'Two', 'Three', 'Four', 'Five', 'Six', 'Seven', 'Eight', 'Nine']
66+
d = {}
67+
# Your code here
68+
return d
69+
70+
def is_same(a, b):
71+
"""Return True is the two items are the same."""
72+
# Your code here
73+
pass
74+
75+
#=========== Tests ===================
76+
def test_create_sequence():
77+
assert create_sequence(12) == [0, 3, 6, 9, 12]
78+
79+
def test_range_of_list():
80+
assert range_of_list() == 30
81+
82+
def test_range_of_list_abs():
83+
assert range_of_list_abs() == 21
84+
85+
def test_list_to_sequence_dict():
86+
expected = {
87+
0 : 'Zero',
88+
1 : 'One',
89+
2 : 'Two',
90+
3 : 'Three',
91+
4 : 'Four',
92+
5 : 'Five',
93+
6 : 'Six',
94+
7 : 'Seven',
95+
8 : 'Eight',
96+
9 : 'Nine',
97+
}
98+
assert list_to_sequence_dict() == expected
99+
100+
def test_is_same():
101+
a = [1, 2, 3]
102+
b = a
103+
assert is_same(a, b)
104+
b = [1, 2, 3]
105+
assert not is_same(a, b)
106+
107+
def main():
108+
return pytest.main(__file__)
109+
110+
if __name__ == '__main__':
111+
sys.exit(main())

Beginers/BuiltinFunctions/solution.py

+113
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
"""Builtin functions problems.
2+
It might be useful to browse here (adjust your version to suit):
3+
https://docs.python.org/2.7/library/functions.html
4+
5+
1. Create the sequence [0, 3, 6, 9, ... N]. What is the problem if N is very large?
6+
Is there a better way if N is very large?
7+
8+
2. Find the difference between the biggest and smallest values in the list
9+
[4, 3, -9, 21, 0]
10+
11+
3. The same as 2. but use the absolute values in the list.
12+
13+
4.Convert a list:
14+
['Zero', 'One', 'Two', 'Three', 'Four', 'Five', 'Six', 'Seven', 'Eight', 'Nine']
15+
To a dictionary:
16+
{
17+
0 : 'Zero',
18+
1 : 'One',
19+
2 : 'Two',
20+
3 : 'Three',
21+
4 : 'Four',
22+
5 : 'Five',
23+
6 : 'Six',
24+
7 : 'Seven',
25+
8 : 'Eight',
26+
9 : 'Nine',
27+
}
28+
29+
5. I have two list a and b. Is there a way that I can tell if they are the same
30+
list? For example in the following case they are the same list:
31+
a = [1, 2, 3]
32+
b = a
33+
And any change to b be will be 'seen' by a.
34+
However in this case a and b are not the same list in the sense that any change
35+
to b be will NOT be 'seen' by a.
36+
a = [1, 2, 3]
37+
b = [1, 2, 3]
38+
39+
Created on 22 Feb 2016
40+
41+
@author: paulross
42+
"""
43+
import sys
44+
45+
import pytest
46+
47+
def create_sequence(N):
48+
"""Create the 3x table up to and including N."""
49+
return range(0, N + 3, 3)
50+
51+
def range_of_list():
52+
"""Return the difference between the largest and smallest values in a list."""
53+
x = [4, 3, -9, 21, 0]
54+
return max(x) - min(x)
55+
56+
def range_of_list_abs():
57+
"""Return the difference between the largest and smallest absolute values in a list."""
58+
x = [4, 3, -9, 21, 0]
59+
abs_x = [abs(value) for value in x]
60+
return max(abs_x) - min(abs_x)
61+
62+
def list_to_sequence_dict():
63+
"""Create a dictionary where the key is the ordinal of the object in the list
64+
and the value is the object itself. For example: {0 : 'Zero', 1 : 'One', ...}"""
65+
x = ['Zero', 'One', 'Two', 'Three', 'Four', 'Five', 'Six', 'Seven', 'Eight', 'Nine']
66+
d = {}
67+
for index, value in enumerate(x):
68+
d[index] = value
69+
return d
70+
71+
def is_same(a, b):
72+
"""Return True is the two items are the same."""
73+
# This is the same as:
74+
# return a is b
75+
return id(a) == id(b)
76+
77+
#=========== Tests ===================
78+
def test_create_sequence():
79+
assert create_sequence(12) == [0, 3, 6, 9, 12]
80+
81+
def test_range_of_list():
82+
assert range_of_list() == 30
83+
84+
def test_range_of_list_abs():
85+
assert range_of_list_abs() == 21
86+
87+
def test_list_to_sequence_dict():
88+
expected = {
89+
0 : 'Zero',
90+
1 : 'One',
91+
2 : 'Two',
92+
3 : 'Three',
93+
4 : 'Four',
94+
5 : 'Five',
95+
6 : 'Six',
96+
7 : 'Seven',
97+
8 : 'Eight',
98+
9 : 'Nine',
99+
}
100+
assert list_to_sequence_dict() == expected
101+
102+
def test_is_same():
103+
a = [1, 2, 3]
104+
b = a
105+
assert is_same(a, b)
106+
b = [1, 2, 3]
107+
assert not is_same(a, b)
108+
109+
def main():
110+
return pytest.main(__file__)
111+
112+
if __name__ == '__main__':
113+
sys.exit(main())

0 commit comments

Comments
 (0)