Checkpoint 8.1.1.
Write a Python function to sum the first N numbers starting with 0. So if N is 4 then your function should add 0 + 1 + 2 + 3
.. activecode:: uniqueid 'nocanvas': directives.flag,
:nopre: do not create an output component
:above: put the canvas above the code
:autorun: run this activecode as soon as the page is loaded
:autograde: typically unittest
:caption: caption under the active code
:include: invisibly include code from another activecode
:hidecode: Don:t show the editor initially
:language: python, html, javascript, java, python2, python3
:tour_1: audio tour track
:tour_2: audio tour track
:tour_3: audio tour track
:tour_4: audio tour track
:tour_5: audio tour track
:nocodelens: Do not show the codelens button
:coach: Show the codecoach button
:timelimit: set the time limit for this program
:stdin: : A file to simulate stdin (java, python2, python3)
:datafile: : A datafile for the program to read (java, python2, python3)
:sourcefile: : source files (java, python2, python3)
:available_files: : other additional files (java, python2, python3)
If this is a homework problem instead of an example in the text
then the assignment text should go here. The assignment text ends with
the line containing four tilde ~
~~~~
print("hello world")
====
print("Hidden code, such as unit tests come after the four = signs")
.. activecode:: sigcse_ex1
:language: python
Write a Python function to sum the first N numbers starting with 0. So if N is 4 then your function should add 0 + 1 + 2 + 3
~~~~
def sum_first_n(N):
# your code here
:language:
is indented by four spaces. from the previous line. It must be indented at least three spaces to line up with the “a” in activecode, but using 4 keeps it consistent with my own personal python indentation style. Everything else in the body of the directive must also be indented to match the indentation of the optional parameters.~~~~
thats four tildes. The next part of the body is any code you want to provide. Just put in your Python or other language as you would normally write it... actex::
directive for the .. activecode::
directive. Making that substitution gives us the following:from unittest.gui import TestCaseGui
class myTests(TestCaseGui):
def testOne(self):
self.assertEqual(add(2,2),4,"A feedback string when the test fails")
self.assertAlmostEqual(add(2.0,3.0), 5.0, 1, "Try adding your parmeters")
myTests().main()
https://docs.python.org/2/library/unittest.html#assert-methods
.. mchoice:: uniqueid
:multiple_answers: boolean [optional]
:random: boolean [optional]
:answer_a: possible answer -- what follows _ is label
:answer_b: possible answer
:answer_c: possible answer
:answer_d: possible answer
:answer_e: possible answer
:correct: letter of correct answer or list of correct answer letters (in case of multiple answe
rs)
:feedback_a: displayed if a is picked
:feedback_b: displayed if b is picked
:feedback_c: displayed if c is picked
:feedback_d: displayed if d is picked
:feedback_e: displayed if e is picked
Question text ...
m = Stack()
m.push('x')
m.push('y')
m.push('z')
while not m.isEmpty():
m.pop()
m.pop()
.. mchoice:: stack_2
:answer_a: 'x'
:answer_b: the stack is empty
:answer_c: an error will occur
:answer_d: 'z'
:correct: c
:feedback_a: You may want to check out the docs for isEmpty
:feedback_b: There is an odd number of things on the stack but each time through the loop 2 things are popped.
:feedback_c: Good Job.
:feedback_d: You may want to check out the docs for isEmpty
Given the following sequence of stack operations, what is the top item on the stack when the sequence is complete?
.. code-block:: python
m = Stack()
m.push('x')
m.push('y')
m.push('z')
while not m.isEmpty():
m.pop()
m.pop()
.. parsonsprob:: unqiue_problem_id_here
:maxdist:
:order:
:language:
:noindent:
:adaptive:
:numbered: left
Instructions for the user. These can include a textual description of how to solve the problem. You must leave a blank line before this.
-----
def findmax(alist):
=====
if len(alist) == 0:
return None
=====
curmax = alist[0]
for item in alist:
=====
if item > curmax:
=====
curmax = item
=====
return curmax
Option | Description |
---|---|
maxdist | The maximum number of distractors to use. They will be picked at random |
order | The order for the lines, they are displayed in a random order normally |
language | Specify the language: java, python |
noindent | Provide the indentation for the user by adding spaces to the left of the code |
adaptive | Provide help is the user is struggling and modify the difficulty of the problem based on the user’s performance on the previous problem |
numbered | Show numbered labels to the left of the code if you add left or to the right when you add right |
.. parsonsprob:: mt1dict1ex
:numbered: left
Complete the function greater_dictionary. Given a dictionary d and an integer cutoff, return a dictionary that contains only the key-value pairs where they key is greater than or equal to cutoff.
-----
def greater_dictionary(d, cutoff):
=====
def greater_dictionary(self, d, cutoff): #paired
=====
result = {}
=====
for key in d.keys():
=====
for key in range(d): #paired
=====
if key >= cutoff:
=====
if key > cutoff: #paired
=====
result[key] = d[key]
=====
d[key] = result[key] #paired
=====
return result
.. parsonsprob:: 2_swapex
:noindent:
The following has the correct code to 'swap' the values in x and y (so that x ends up with y's initial value and y ends up with x's initial value), but the code is mixed up and contains <b>one extra block</b> which is not needed in a correct solution. Drag the needed blocks from the left into the correct order on the right. Check your solution by clicking on the <i>Check Me</i> button. You will be told if any of the blocks are in the wrong order or if you need to remove one or more blocks.
-----
int x = 3;
int y = 5;
int temp = 0;
=====
temp = x;
=====
x = y;
=====
y = temp;
=====
y = x; #distractor
virtualenv runestone
- Creates a python virtual environment so you don’t need administrator privileges to install the rest of what you need. If virtualenv is not installed you should do pip install virtualenv
(Python 2.7.x) or python -m venv runestone
(Python > 3.5). If the pip command is not found then you should upgrade your version of Python to something newer. All modern version of Python come with pip pre-installed.. runestone/bin/activate
– this activates the virtual environment you created in the previous step.mkdir assignments
– Create an assignment to do your runestone work in.cd assignments
– Make assignments your working directory.runestone init
– Create a new project with some template directivesrunestone build
– Convert the restructuredText to htmlrunestone serve
– start a simple web server so you can access your new project in your browser at http://localhost:8000/index.html
runestone build
again to check your work.