Skip to content

Commit bdd89e0

Browse files
committed
cleaned up solutions a bit
1 parent 57c318c commit bdd89e0

2 files changed

Lines changed: 38 additions & 16 deletions

File tree

‎solutions/Session03/list_lab.py‎

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -41,16 +41,17 @@
4141

4242
ind = int(input("give me an index> "))
4343

44-
print("you selected fruit number: %i, which is %s" % (ind, fruits[ind-1]))
44+
print("you selected fruit number: {}, which is {}".format(ind, fruits[ind - 1]))
4545
print()
4646
print("All the fruit:\n", fruits)
4747
print()
4848

4949
fruits.insert(0, 'Kiwi')
5050

51-
print("Added a kiwi:\n", fruits)
51+
print("Added a kiwi at the beginning:\n", fruits)
5252

53-
print("All the P fruits")
53+
print()
54+
print("All the P fruits:")
5455
for fruit in fruits:
5556
if fruit[0].lower() == 'p':
5657
print(fruit)
@@ -60,22 +61,26 @@
6061
d_fruits = fruits * 2
6162

6263
print("All the fruits are:", fruits)
63-
ans = input("Which fruit would you like to delete? ")
64+
ans = input("Which fruit would you like to delete? >")
6465
while ans in d_fruits:
6566
d_fruits.remove(ans)
6667

67-
print("with fruit deleted:\n", d_fruits)
68+
print("\nWith fruit deleted:\n", d_fruits)
6869

6970
# another way to do that:
7071
# This one only requires looping through list once
71-
d_fruits = []
72-
for fruit in fruits * 2:
72+
d_fruits = fruits * 2
73+
d_fruits_new = []
74+
for fruit in d_fruits * 2:
7375
if fruit != ans:
74-
d_fruits.append(fruit)
76+
d_fruits_new.append(fruit)
7577

76-
print("With fruit deleted another way:\n", d_fruits)
78+
print("\nWith fruit deleted another way:\n", d_fruits_new)
7779
print()
7880

81+
# Series 3
82+
################
83+
7984
# keep a copy around for next step
8085
orig_fruits = fruits[:]
8186

@@ -91,18 +96,21 @@
9196
# Note: I could be smarter about re-asking here,
9297
# but that's not the point of this excercise...
9398

94-
print("here they are with only the ones you like")
99+
print("\nHere they are with only the ones you like")
95100
print(fruits)
96101
print()
97102

103+
# Series 4
104+
##########
105+
98106
fruits = orig_fruits[:] # makes a copy
99107

100108
# option 1: build up a copy one by one:
101109
r_fruits = []
102110
for fruit in fruits:
103111
r_fruits.append(fruit[::-1])
104112

105-
print("here they are reversed")
113+
print("Here they are reversed")
106114
print(r_fruits)
107115

108116
# option 2: make a copy, then modify in place:

‎solutions/Session03/slicing_lab.py‎

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,53 @@
11
#!/usr/bin/env python3
22

33
"""
4-
The version done in class
4+
One solution...
55
"""
66

77

88
def swap(seq):
9-
return seq[-1:]+seq[1:-1]+seq[:1]
9+
"""with the first and last items exchanged"""
10+
return seq[-1:] + seq[1:-1] + seq[:1]
1011

1112

1213
assert swap('something') == 'gomethins'
1314
assert swap(tuple(range(10))) == (9, 1, 2, 3, 4, 5, 6, 7, 8, 0)
1415

1516

1617
def rem(seq):
18+
"""With every other item removed"""
1719
return seq[::2]
1820

21+
1922
assert rem('a word') == 'awr'
2023

2124

2225
def rem4(seq):
26+
"""With the first and last 4 items removed, and every other item in between"""
2327
return seq[4:-4:2]
2428

25-
print(rem4((1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11), ))
29+
30+
a_tuple = (1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11)
31+
print(rem4(a_tuple))
32+
assert rem4(a_tuple) == (5, 7)
2633

2734

2835
def reverse(seq):
36+
"""With the elements reversed (just with slicing)"""
2937
return seq[::-1]
3038

3139

3240
print(reverse('a string'))
41+
assert reverse([3, 6, 1, 8, 3, 7]) == [7, 3, 8, 1, 6, 3]
3342

3443

3544
def thirds(seq):
36-
i = len(seq)//3
45+
"""With the middle third, then last third, then the first third in the new order"""
46+
i = len(seq) // 3
3747
return seq[i:-i] + seq[-i:] + seq[:i]
3848

39-
print (thirds(tuple(range(12))))
49+
50+
print(thirds(tuple(range(12))))
51+
assert thirds(tuple(range(12))) == (4, 5, 6, 7, 8, 9, 10, 11, 0, 1, 2, 3)
52+
53+

0 commit comments

Comments
 (0)