Skip to content

Commit 0ce050b

Browse files
committed
changed to sum_first_half
1 parent 2cda124 commit 0ce050b

1 file changed

Lines changed: 20 additions & 42 deletions

File tree

pretext/functions/funcWithListsAndLoops.ptx

Lines changed: 20 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -155,27 +155,30 @@ main()
155155
</input>
156156
</program>
157157
</exercise>
158-
<exercise label="flal_avg_drop_high_and_low">
158+
159+
<exercise label="flal_ac_sum_first_half">
159160
<statement>
160-
<p>Write a function <c>avg_with_drop</c> that takes a list, <c>num_list</c> and returns the average of the values in the list, but it does not include the highest or lowest value in the average. For example, <c>avg_with_drop([1,2,3,4])</c> should return <c>2.5</c>.</p>
161+
<p>Write a function <c>sum_first_half</c> that takes a list and returns a the sum of just the first half of the items.
162+
For example, <c>sum_first_half([1,2,3,4])</c> should return ``3`` (sum of 1 and 2) and <c>first_half([7,8,9])</c> should return ``7``. </p>
161163
</statement>
162-
<program xml:id="flal_avg_drop_high_and_low_editor" interactive="activecode" language="python">
164+
<program xml:id="flal_ac_sum_first_half_editor" interactive="activecode" language="python">
163165
<input>
164-
def avg_with_drop(num_list):
166+
def sum_first_half(alist):
165167

166168
====
167169
from unittest.gui import TestCaseGui
168170

169-
class myTests(TestCaseGui):
171+
class myTests(TestCaseGui):
170172

171-
def testOne(self):
172-
self.assertEqual(avg_with_drop([1,2,3,4]), 2.5, 'avg_with_drop([1,2,3,4])')
173-
self.assertEqual(avg_with_drop([2,4,6,8]), 5, 'avg_with_drop([2,4,6,8])')
174-
self.assertEqual(avg_with_drop([10, 80, 100, 60]), 70, 'avg_with_drop([10, 80, 100, 60])')
175-
self.assertEqual(avg_with_drop([-10, 80, 120, 60]), 70, 'avg_with_drop([-10, 80, 120, 60])')
176-
self.assertEqual(avg_with_drop([5, 10, 15, 20]), 12.5, 'avg_with_drop([5, 10, 15, 20])')
173+
def testOne(self):
174+
self.assertEqual(sum_first_half([1,2,3,4]), 3, 'sum_first_half([1,2,3,4])')
175+
self.assertEqual(sum_first_half([7,8,9]), 7, 'sum_first_half([7,8,9])')
176+
self.assertEqual(sum_first_half([]), 0, 'sum_first_half([])')
177+
self.assertEqual(sum_first_half([6]), 0, 'sum_first_half([6])')
178+
self.assertEqual(sum_first_half([1,2,3,4,5]), 3, 'sum_first_half([1,2,3,4,5])')
179+
self.assertEqual(sum_first_half([1,2,3,4,5,6]), 6, 'sum_first_half([1,2,3,4,5,6])')
177180

178-
myTests().main()
181+
myTests().main()
179182
</input>
180183
</program>
181184
</exercise>
@@ -234,14 +237,6 @@ print(list_trans(l1))
234237
</program>
235238
</statement>
236239
<choices>
237-
<choice>
238-
<statement>
239-
<p>[2, 5, 7, 3]</p>
240-
</statement>
241-
<feedback>
242-
<p>This is what the list looks like before the pop executes.</p>
243-
</feedback>
244-
</choice>
245240
<choice>
246241
<statement>
247242
<p>[5, 7, 3]</p>
@@ -370,6 +365,7 @@ list_trans(l1)
370365
<subsection xml:id="flal_the-for-each-loop">
371366
<title>The For-Each Loop</title>
372367
<p>A for-each loop in Python will loop though the items in a list starting with the item at index 0, then index 1, and so on till the last item in the list.</p>
368+
373369
<exercise label="flal_ll_fitb_count_odd_first">
374370
<statement>
375371
<p>What is the first thing that will be printed by the code below? <var/> </p>
@@ -389,25 +385,8 @@ list_trans(l1)
389385
</var>
390386
</setup>
391387
</exercise>
392-
<exercise label="flal_ll_fitb_count_odd_last">
393-
<statement>
394-
<p>What is the last thing that will be printed by the code below? <var/> </p>
395-
</statement>
396-
<setup>
397-
<var>
398-
<condition number="0">
399-
<feedback>
400-
<p>It will print the number of values that are odd in the last list which is zero.</p>
401-
</feedback>
402-
</condition>
403-
<condition string=".*">
404-
<feedback>
405-
<p>Run the code to check.</p>
406-
</feedback>
407-
</condition>
408-
</var>
409-
</setup>
410-
</exercise>
388+
389+
411390
<exercise label="flal_ll_ac_count_odd">
412391
<statement>
413392
<p>Run this code to see what it prints.</p>
@@ -533,7 +512,7 @@ main()
533512
</exercise>
534513

535514
<note>
536-
<p>The function range(start, end, by) will return a range object (an iterator) that allows you to loop from start (inclusive) to end (exclusive) and add the value of by after each execution of the loop.</p>
515+
<p>The function <c>range(start, end, by)</c> will return a range object (an iterator) that allows you to loop from <c>start</c> (inclusive) to <c>end</c> (exclusive) and add the value of <c>by</c> after each execution of the loop.</p>
537516
</note>
538517

539518
<exercise label="flal_ll_pp_total_at_odd_indices" numbered="yes" adaptive="yes" indentation="hide" language="python">
@@ -619,8 +598,7 @@ main()
619598
</var>
620599
</setup>
621600
</exercise>
622-
<p>What do you think would happen if you deleted lines 6 and 7 in the above code?</p>
623-
601+
624602
<note>
625603
<p>A loop that never ends is called an infinite loop. A while loop should have some way to end. If you have an infinite loop you may need to refresh the page to stop it.</p>
626604
</note>

0 commit comments

Comments
 (0)