Skip to content

Commit d151fb3

Browse files
author
Philip Guo
committed
lalala
1 parent e0fe266 commit d151fb3

2 files changed

Lines changed: 17 additions & 12 deletions

File tree

question.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@
7373
<div id="pyInputPane">
7474

7575
<p/>Write your solution code here:<br/>
76-
<textarea class="codeInputPane" id="actualCodeInput" cols="60" rows="18" wrap="off">
76+
<textarea class="codeInputPane" id="actualCodeInput" cols="60" rows="15" wrap="off">
7777
</textarea>
7878

7979
<p/>Write your test code here:<br/>

questions/inplace-reverse.txt

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,27 @@
11
Name:
2-
In-place reverse
2+
Debugging: In-place reverse
33

44
Question:
5-
Write a function to reverse a list in-place. For example, it
6-
should mutate ['a', 'b, 'c', 'd', 'e'] into ['e', 'd', 'c', 'b', 'a'].
5+
6+
The given function reverses a list in-place, but it doesn't work for all
7+
test inputs (try to "Submit answer" to see the failing tests). Change
8+
ONE line to make this function pass all tests.
79

810
Hint:
9-
Think about swapping pairs of elements.
11+
Focus on the 'indices' variable.
1012

1113
Solution:
12-
Swap the first and last elements, then the second and second-to-last
13-
elements, etc.
14+
Change line 3 to "indices = range((maxIndex + 1)/2)"
1415

1516
Skeleton:
1617

1718
def reverse(lst):
18-
# write your solution code here
19-
N = len(lst)
20-
for i in range(N/2):
19+
maxIndex = len(lst) - 1
20+
indices = range(maxIndex/2)
21+
for i in indices:
2122
tmp = lst[i]
22-
lst[i] = lst[N-i]
23-
lst[N-i] = tmp
23+
lst[i] = lst[maxIndex - i]
24+
lst[maxIndex - i] = tmp
2425

2526

2627
Test:
@@ -30,27 +31,31 @@ reverse(input)
3031
Expect:
3132
input = ['e', 'd', 'c', 'b', 'a']
3233

34+
3335
Test:
3436
input = ['a', 'b', 'c', 'd']
3537
reverse(input)
3638

3739
Expect:
3840
input = ['d', 'c', 'b', 'a']
3941

42+
4043
Test:
4144
input = ['a', 'b', 'c']
4245
reverse(input)
4346

4447
Expect:
4548
input = ['c', 'b', 'a']
4649

50+
4751
Test:
4852
input = ['a', 'b']
4953
reverse(input)
5054

5155
Expect:
5256
input = ['b', 'a']
5357

58+
5459
Test:
5560
input = ['a']
5661
reverse(input)

0 commit comments

Comments
 (0)