Skip to content

Commit 14487d3

Browse files
committed
adding prac: ✏️
1 parent 2d52727 commit 14487d3

File tree

5 files changed

+388
-0
lines changed

5 files changed

+388
-0
lines changed
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
2+
3+
# Practice 1: Fundamentals
4+
5+
For each practice problem, write a function that returns a value (not just prints it). You can then call the function a couple times to test it, comment those calls out, and move on to the next problem. An example of this format is given below.
6+
7+
```python
8+
def add(a, b):
9+
return a + b
10+
#print(add(5, 2))
11+
#print(add(8, 1))
12+
```
13+
14+
## Problem 1
15+
16+
Write a function that tells whether a number is even or odd (hint, compare a/2 and a//2, or use a%2)
17+
18+
```python
19+
def is_even(a):
20+
...
21+
is_even(5) → False
22+
is_even(6) → True
23+
```
24+
25+
26+
## Problem 2
27+
28+
Write a function that takes two ints, a and b, and returns True if one is positive and the other is negative.
29+
30+
```python
31+
def opposite(a, b):
32+
...
33+
opposite(10, -1) → True
34+
opposite(2, 3) → False
35+
opposite(-1, -1) → False
36+
```
37+
38+
## Problem 3
39+
40+
Write a function that returns True if a number within 10 of 100.
41+
42+
```python
43+
def near_100(num):
44+
...
45+
near_100(50) → False
46+
near_100(99) → True
47+
near_100(105) → True
48+
```
49+
50+
## Problem 4
51+
52+
Write a function that returns the maximum of 3 parameters.
53+
54+
```python
55+
def maximum_of_three(a, b, c):
56+
...
57+
maximum_of_three(5,6,2) → 6
58+
maximum_of_three(-4,3,10) → 10
59+
```
60+
61+
## Problem 5
62+
63+
Print out the powers of 2 from 2^0 to 2^20
64+
65+
`1, 2, 4, 8, 16, 32, ...`
66+
67+
68+
69+
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
2+
# Practice: Strings
3+
4+
For each practice problem, write a function that returns a value (not just prints it). You can then call the function a couple times to test it, comment those calls out, and move on to the next problem. An example of this format is given below.
5+
6+
```python
7+
def add(a, b):
8+
return a + b
9+
#print(add(5, 2))
10+
#print(add(8, 1))
11+
```
12+
13+
14+
## Problem 1
15+
16+
Get a string from the user, print out another string, doubling every letter.
17+
18+
```
19+
>>> Enter some text: hello
20+
hheelloo
21+
```
22+
23+
## Problem 2
24+
25+
Write a function that takes a string, and returns a list of strings, each missing a different character.
26+
27+
```python
28+
missing_char('kitten') → ['itten', 'ktten', 'kiten', 'kiten', 'kittn', 'kitte']
29+
```
30+
31+
## Problem 3
32+
Return the letter that appears the latest in the english alphabet.
33+
```
34+
>>> latest_letter('pneumonoultramicroscopicsilicovolcanoconiosis')
35+
the latest letter is v.
36+
```
37+
38+
## Problem 4
39+
40+
Write a function that returns the number of occurances of 'hi' in a given string.
41+
42+
```python
43+
count_hi('hihi') → 2
44+
```
45+
46+
## Problem 5
47+
48+
Write a function that returns True if a given string contains the same number of 'cat' as it does 'dog'
49+
50+
```python
51+
cat_dog('catdog') → True
52+
cat_dog('catcat') → False
53+
cat_dog('catdogcatdog') → True
54+
```
55+
56+
57+
58+
59+
## Problem 6
60+
61+
Return the number of letter occurances in a string.
62+
```python
63+
def count_letter(letter, word):
64+
...
65+
count_letter('i', 'antidisestablishmentterianism') → 5
66+
count_letter('p', 'pneumonoultramicroscopicsilicovolcanoconiosis') → 2
67+
```
68+
69+
## Problem 7
70+
71+
Convert input strings to lowercase without any surrounding whitespace.
72+
73+
```
74+
lower_case("SUPER!") → 'super!'
75+
lower_case(" NANNANANANA BATMAN ") → 'nannananana batman'
76+
```
Lines changed: 156 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,156 @@
1+
2+
# Practice Problems: Lists
3+
4+
5+
6+
For each practice problem, write a function that returns a value (not just prints it). You can then call the function a couple times to test it, comment those calls out, and move on to the next problem. An example of this format is given below.
7+
8+
```python
9+
def add(a, b):
10+
return a + b
11+
#print(add(5, 2))
12+
#print(add(8, 1))
13+
```
14+
15+
16+
17+
## Problem 1
18+
19+
Write a function using random.randint to generate an index, use that index to pick a random element of a list and return it.
20+
21+
```
22+
def random_element(a):
23+
...
24+
25+
fruits = ['apples', 'bananas', 'pears']
26+
random_element(fruits) could return 'apples', 'bananas' or 'pears'
27+
```
28+
29+
## Problem 2
30+
31+
Write a REPL which asks users for a list of numbers, which they enter, until they say 'done'. Then print out the list.
32+
33+
```
34+
Enter a number (or 'done'): 5
35+
Enter a number (or 'done'): 34
36+
Enter a number (or 'done'): 515
37+
Enter a number (or 'done'): done
38+
[5, 34, 515]
39+
```
40+
41+
42+
## Problem 3
43+
44+
Write a function that takes a list of numbers, and returns True if there is an even number of even numbers.
45+
46+
```python
47+
eveneven([5, 6, 2]) → True
48+
eveneven([5, 5, 2]) → False
49+
```
50+
51+
52+
## Problem 4
53+
54+
Print out every other element of a list, first using a while loop, then using a for loop.
55+
56+
```
57+
>>> nums = [0, 1, 2, 3, 4, 5, 6, 7, 8]
58+
>>> print_every_other(nums)
59+
0, 2, 4, 6, 8
60+
```
61+
62+
## Problem 5
63+
Write a function that returns the reverse of a list.
64+
65+
`def reverse(nums):`
66+
67+
## Problem 6
68+
Write a function to move all the elements of a list with value less than 10 to a new list and return it.
69+
70+
`def extract_less_than_ten(nums):`
71+
72+
## Problem 7
73+
Write a function to find all common elements between two lists.
74+
75+
`def common_elements(nums1, nums2):`
76+
77+
78+
## Problem 8
79+
Write a function to combine two lists of equal length into one, alternating elements.
80+
81+
```python
82+
def combine(nums1, nums2):
83+
...
84+
combine(['a','b','c'],[1,2,3]) → ['a', 1, 'b', 2, 'c', 3]
85+
```
86+
87+
88+
## Problem 9
89+
90+
Given a list of numbers, and a target number, find a pair of numbers from the list that sum to a target number
91+
92+
```python
93+
nums = [5, 6, 2, 3]
94+
target = 7
95+
find_pair(nums, target) → [5, 2]
96+
```
97+
98+
Optional: return a list of all pairs of numbers that sum to a target value.
99+
100+
101+
## Problem 10
102+
103+
Write a function that merges two lists into a single list, where each element of the output list is a list containing two elements, one from each of the input lists.
104+
105+
```python
106+
merge([5,2,1], [6,8,2]) → [[5,6],[2,8],[1,2]]
107+
```
108+
109+
110+
## Problem 11
111+
112+
Write a function `combine_all` that takes a list of lists, and returns a list containing each element from each of the lists.
113+
114+
```python
115+
nums = [[5,2,3],[4,5,1],[7,6,3]]
116+
combine_all(nums) → [5,2,3,4,5,1,7,6,3]
117+
```
118+
119+
120+
## Problem 12
121+
122+
Write a function that takes `n` as a parameter, and returns a list containing the first `n` [Fibonacci Numbers](https://en.wikipedia.org/wiki/Fibonacci_number).
123+
124+
```python
125+
fibonacci(8) → [1, 1, 2, 3, 5, 8, 13, 21]
126+
```
127+
128+
## Problem 13
129+
130+
Write functions to find the minimum, maximum, mean, and (optionally) mode of a list of numbers.
131+
132+
```python
133+
def minimum(nums):
134+
...
135+
136+
def maxmimum(nums):
137+
...
138+
139+
def mean(nums):
140+
...
141+
142+
def mode(nums): # (OPTIONAL)
143+
...
144+
```
145+
146+
147+
## Problem 14
148+
149+
Write a function which takes a list as a parameter and returns a new list with any duplicates removed.
150+
151+
```python
152+
def find_unique(nums):
153+
...
154+
nums = [12, 24, 35, 24, 88, 120, 155, 88, 120, 155]
155+
unique_nums = find_unique(nums) → [12, 24, 35, 88, 120, 155]
156+
```
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
2+
# Practice 4: Dictionaries
3+
4+
For each practice problem, write a function that returns a value (not just prints it). You can then call the function a couple times to test it, comment those calls out, and move on to the next problem. An example of this format is given below.
5+
6+
```python
7+
def add(a, b):
8+
return a + b
9+
#print(add(5, 2))
10+
#print(add(8, 1))
11+
```
12+
13+
14+
## Problem 1
15+
16+
Given a the two lists below, combine them into a dictionary.
17+
18+
```python
19+
def combine(a, b):
20+
...
21+
fruits = ['apple', 'banana', 'pear']
22+
prices = [1.2, 3.3, 2.1]
23+
combine(fruits, prices) -> {'apple':1.2, 'banana':3.3, 'pear':2.1}
24+
```
25+
26+
27+
## Problem 2
28+
29+
Using the result from the previous problem, iterate through the dictionary and calculate the average price of an item.
30+
31+
```python
32+
def average(d):
33+
...
34+
combined = {'apple':1.2, 'banana':3.3, 'pear':2.1}
35+
average(combined) -> 2.2
36+
```
37+
38+
## Problem 3
39+
40+
Average numbers whose keys start with the same letter. Output a dictionary containing those letters as keys and the averages.
41+
42+
```python
43+
d = {'a1':5, 'a2':2, 'a3':3, 'b1':10, 'b2':1, 'b3':1, 'c1':4, 'c2':5, 'c3':6}
44+
unify(d) -> {'a':3, 'b':4, 'c':5}
45+
```
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
2+
3+
# Practice 5: Comprehensions
4+
5+
6+
For each practice problem, write a function that returns a value (not just prints it). You can then call the function a couple times to test it, comment those calls out, and move on to the next problem. An example of this format is given below.
7+
8+
```python
9+
def add(a, b):
10+
return a + b
11+
#print(add(5, 2))
12+
#print(add(8, 1))
13+
```
14+
15+
16+
## Problem 1
17+
18+
Write a comprehension to generate the first 10 powers of two.
19+
20+
`[1, 2, 4, 8, 16, 32, 64, 128, 256, 512]`
21+
22+
## Problem 2
23+
24+
Write a comprehension to generate the first 10 even numbers.
25+
26+
`[2, 4, 6, 8, 10, 12, 14, 16, 18, 20]`
27+
28+
29+
## Problem 3
30+
31+
Write a dictionary comprehension to swap keys and values of a given dictionary. So `{'a': 1, 'b': 2}` would become `{1: 'a', 2: 'b'}`.
32+
33+
34+
## Problem 4
35+
36+
Write a dictionary comprehension to print each letter of the alphabet and its correstponding ASCII code (check out [ord](https://docs.python.org/3.6/library/functions.html#ord))
37+
38+
`{'a': 97, 'b': 98, 'c': 99, 'd': 100, 'e': 101, 'f': 102, 'g': 103, 'h': 104, ...}`
39+
40+
41+
42+

0 commit comments

Comments
 (0)