8000 added three more exercises · learnbyexample/TUI-apps@e7d8975 · GitHub
[go: up one dir, main page]

Skip to content

Commit e7d8975

Browse files
added three more exercises
1 parent 3b529e6 commit e7d8975

File tree

8 files changed

+63
-0
lines changed

8 files changed

+63
-0
lines changed

PythonExercises/nums.txt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
8
2+
53
3+
3.14
4+
84
5+
73e2
6+
100
7+
2937

PythonExercises/questions.json

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,5 +83,20 @@
8383
"question": "Write a function that returns a dictionary sorted by values in ascending order.",
8484
"q_file": "dictionary_sort.py",
8585
"exp_op": "all tests passed\n"
86+
},
87+
"18": {
88+
"question": "Write a function that returns a list of string slices as per the following rules:\n\n* return the input string as the only element if its length is less than 3 characters\n* otherwise, return slices that have 2 or more characters as shown below\n\nAssume that the input string will contain only alphabets.",
89+
"q_file": "string_slices.py",
90+
"exp_op": "all tests passed\n"
91+
},
92+
"19": {
93+
"question": "Square even numbers and cube odd numbers. For example, `[321, 1, -4, 0, 5, 2]` should give `[33076161, 1, 16, 0, 125, 4]` as the output.",
94+
"q_file": "square_cube.py",
95+
"exp_op": "all tests passed\n"
96+
},
97+
"20": {
98+
"question": "The file `nums.txt` (in the current working directory) has a single column of numbers. Display the sum of these numbers (`10485.14`).",
99+
"q_file": "sum_column.py",
100+
"exp_op": "10485.14\n"
86101
}
87102
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
def sqr_even_cube_odd(seq):
2+
# add your solution here
3+
4+
assert sqr_even_cube_odd([321, 1, -4, 0, 5, 2]) == [33076161, 1, 16, 0, 125, 4]
5+
assert sqr_even_cube_odd((2, 4, 6)) == [4, 16, 36]
6+
7+
print('all tests passed')
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
def slice(s):
2+
# add your solution here
3+
4+
assert slice('a') == ['a']
5+
assert slice('to') == ['to']
6+
assert slice('cat') == ['ca', 'cat', 'at']
7+
assert slice('table') == ['ta', 'tab', 'tabl', 'table', 'ab', 'abl', 'able', 'bl', 'ble', 'le']
8+
9+
print('all tests passed')
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# add your solution here
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
def sqr_even_cube_odd(seq):
2+
return [n**3 if n%2 else n**2 for n in seq]
3+
4+
assert sqr_even_cube_odd([321, 1, -4, 0, 5, 2]) == [33076161, 1, 16, 0, 125, 4]
5+
assert sqr_even_cube_odd((2, 4, 6)) == [4, 16, 36]
6+
7+
print('all tests passed')
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
def slice(s):
2+
c = len(s)
3+
if c < 3:
4+
return [s]
5+
return [s[i:j+1] for i in range(c) for j in range(i+1, c)]
6+
7+
assert slice('a') == ['a']
8+
assert slice('to') == ['to']
9+
assert slice('cat') == ['ca', 'cat', 'at']
10+
assert slice('table') == ['ta', 'tab', 'tabl', 'table', 'ab', 'abl', 'able', 'bl', 'ble', 'le']
11+
12+
print('all tests passed')
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
total = 0
2+
with open('nums.txt', 'r') as f:
3+
for line in f:
4+
total += float(line)
5+
print(total)

0 commit comments

Comments
 (0)
0