Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fixed question examples #13

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions infoCourse.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"uuid": "REPLACE ME WITH A VALID UUID by running uuidgen in a shell",
"name": "CS170",
"title": "Efficient Algorithms and Intractable Problems",
"uuid": "99999999-9999-9999-9999-999999999999",
"name": "CS XXX",
"title": "Example Course",
"options": {
"useNewQuestionRenderer": true
},
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
14 changes: 14 additions & 0 deletions questions/pl-faded-parsons-examples/2x2_determinant.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
"""Write a function <code>det</code> that calculates the determinant of a
2 by 2 matrix of the form,
ie, evaluate
$$\bf M =
\begin{bmatrix}
a & b \\
c & d
\end{bmatrix}
$$"""


def det(a, b, c, d): #0given
"""Calculates the determinant of a 2x2 matrix"""
return ?a * c + b * d?
17 changes: 17 additions & 0 deletions questions/pl-faded-parsons-examples/2x2_determinant/info.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"uuid": "a7093a23-ca93-4d22-9a28-c81c0b375fa0",
"title": "2x2 Determinant",
"topic": "",
"tags": [
"berkeley",
"fp"
],
"type": "v3",
"gradingMethod": "External",
"externalGradingOptions": {
"enabled": true,
"image": "prairielearn/grader-python",
"entrypoint": "/python_autograder/run.sh",
"timeout": 5
}
}
18 changes: 18 additions & 0 deletions questions/pl-faded-parsons-examples/2x2_determinant/question.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<!-- AUTO-GENERATED FILE -->
<pl-question-panel>
Write a function <code>det</code> that calculates the determinant of a
2 by 2 matrix of the form,
ie, evaluate
$$\bf M =
\begin{bmatrix}
a & b \\
c & d
\end{bmatrix}
$$
</pl-question-panel>

<!-- see README for where the various parts of question live -->
<pl-faded-parsons>
def det(a, b, c, d): #0given
return !BLANK
</pl-faded-parsons>
18 changes: 18 additions & 0 deletions questions/pl-faded-parsons-examples/2x2_determinant/server.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# AUTO-GENERATED FILE
# go to https://prairielearn.readthedocs.io/en/latest/python-grader/#serverpy for more info

def generate(data):
# Define incoming variables here
names_for_user = [
# ex: student receives a matrix m
# {"name": "m", "description": "a 2x2 matrix", "type": "numpy array"}
]
# Define outgoing variables here
names_from_user = [
{"name": "det", "description": "Calculates the determinant of a 2x2 matrix", "type": "python function"},
]

data["params"]["names_for_user"] = names_for_user
data["params"]["names_from_user"] = names_from_user

return data
14 changes: 14 additions & 0 deletions questions/pl-faded-parsons-examples/2x2_determinant/source.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
"""Write a function <code>det</code> that calculates the determinant of a
2 by 2 matrix of the form,
ie, evaluate
$$\bf M =
\begin{bmatrix}
a & b \\
c & d
\end{bmatrix}
$$"""


def det(a, b, c, d): #0given
"""Calculates the determinant of a 2x2 matrix"""
return ?a * c + b * d?
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
def det(a, b, c, d):
"""Calculates the determinant of a 2x2 matrix"""
return a * c + b * d
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# AUTO-GENERATED FILE
# go to https://prairielearn.readthedocs.io/en/latest/python-grader/#testssetup_codepy for more info
27 changes: 27 additions & 0 deletions questions/pl-faded-parsons-examples/2x2_determinant/tests/test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# AUTO-GENERATED FILE
# go to https://prairielearn.readthedocs.io/en/latest/python-grader/#teststestpy for more info

from pl_helpers import name, points
from pl_unit_test import PLTestCase
from code_feedback import Feedback


class Test(PLTestCase):
@points(1)
@name("test 0")
def test_0(self):
points = 0
# ex: calling a student defined function det
# with args=(1, 2, 3, 4)
# case = [1, 2, 3, 4]
# user_val = Feedback.call_user(self.st.det, *case)

# ex: calling a function defined in ans.py called det
# with the same arguments
# ref_val = self.ref.det(*case)

# ex: test correctness, update points
# if Feedback.check_scalar('case: ' + case, ref_val, user_val):
# points += 1

Feedback.set_score(points)
12 changes: 12 additions & 0 deletions questions/pl-faded-parsons-examples/compute_poly/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# Faded Parsons example

The `serverFilesQuestion/compute_poly.yaml` file is the original representation used by the
standalone FPP tool...in a normal FPP question (once this element is
implemented), this file should never be needed.

The problem prompt (`problem_description` key in the file) just goes
into a `pl-question-panel` element in `question.html`.

See the element directory's `pl-faded-parsons.md` for documentation on
what other files are needed.

14 changes: 14 additions & 0 deletions questions/pl-faded-parsons-examples/compute_poly/info.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"uuid": "be987220-ca54-11eb-a3a2-acde48001122",
"title": "Faded Parsons example 1",
"topic": "",
"tags": ["berkeley", "fp"],
"type": "v3",
"gradingMethod": "External",
"externalGradingOptions": {
"enabled": true,
"image": "prairielearn/grader-python",
"entrypoint": "/python_autograder/run.sh",
"timeout": 60
}
}
31 changes: 31 additions & 0 deletions questions/pl-faded-parsons-examples/compute_poly/question.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<pl-question-panel>
A polynomial function is a function with coefficients, variables and
constants. A polynomial function is said to be the nth degree polynomial
if there is a term in the function with the variable to the nth degree.
For example, a 4th degree polynomial must contain the term x^4 with some
coefficient multiplied to it.<br><br>

Write a function that takes a list of coefficients (from 0th to nth degree)
and a value <code>x</code>, and returns the polynomial computed at
<code>x</code>.<br><br>

Write this function using a <b>for</b> loop, not a <b>while</b> loop.<br><br>

<pl-code language="python">
>> poly([4], 2) # 4*(2**0)
4
>> poly([4, 5], 2) # 4*(2**0) + 5*(2**1)
14
>> poly([2,4,7], 6) # 2*(6**0) + 4*(6**1) + 7*(6**2)
276</pl-code>

</pl-question-panel>

<pl-faded-parsons>
def poly(coeffs, x): #0given
total = !BLANK
for power, coeff in enumerate(coeffs):
!BLANK = total + coeff * (x ** power)
return total #1given
</pl-faded-parsons>

17 changes: 17 additions & 0 deletions questions/pl-faded-parsons-examples/compute_poly/server.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import numpy as np
import prairielearn as pl
import json


def generate(data):

# Define the variables here
names_for_user = []
names_from_user = [
{"name": "poly", "description": "compute polynomial", "type": "python function"}
]

data["params"]["names_for_user"] = names_for_user
data["params"]["names_from_user"] = names_from_user

return data
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
Now you know why **this** is the _correct_ answer!

Also note the following:

* Thing 1.
* Thing 2.
9 changes: 9 additions & 0 deletions questions/pl-faded-parsons-examples/compute_poly/tests/ans.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
def poly(coeffs, x):
# Keep track of the total as we iterate through each term.
# Each term is of the form coeff*(x**power).
total = 0
# Extract the power and coefficient for each term.
for power, coeff in enumerate(coeffs):
# Add the value of the term to the total.
total = total + coeff * (x ** power)
return total
Empty file.
111 changes: 111 additions & 0 deletions questions/pl-faded-parsons-examples/compute_poly/tests/test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
from pl_helpers import name, points
from pl_unit_test import PLTestCase
from code_feedback import Feedback
from unittest import mock
from io import StringIO
import numpy as np
from random import randrange


class Test(PLTestCase):

@points(1)
@name("testing single case")
def test_0(self):
case = [[10], 3]
points = 0
user_val = Feedback.call_user(self.st.poly, *case)
ref_val = self.ref.poly(*case)
if Feedback.check_scalar(f"args: {case}", ref_val, user_val):
points += 1
Feedback.set_score(points)

@points(4)
@name("testing multiple cases")
def test_1(self):
cases = [
[[4, 5], 2],
[[2, 4, 7], 6],
[[2, 4, 7], 1],
[[1,2,3,4,5,6], 7]
]
points = 0
for case in cases:
user_val = Feedback.call_user(self.st.poly, *case)
ref_val = self.ref.poly(*case)
if Feedback.check_scalar(name=f"args: {case}", ref=ref_val, data=user_val, report_success=False, report_failure=False):
points += 1
Feedback.add_feedback('Well Done')
else:
Feedback.add_feedback('Whoops!')
Feedback.add_feedback(f'Your answer: {user_val}\nReference Answer: {ref_val}')

Feedback.set_score(points/len(cases))

@points(3)
@name("testing hidden cases")
def test_2(self):
cases = [
[[10], 3],
[[6,5,4,3,2,1], 7],
[[1,2,3,4,5,6], 8],
]
points = 0
for case in cases:
user_val = Feedback.call_user(self.st.poly, *case)
ref_val = self.ref.poly(*case)
if Feedback.check_scalar(name=f"hidden case", ref=ref_val, data=user_val, report_success=False, report_failure=False):
points += 1
Feedback.add_feedback('Check')
else:
Feedback.add_feedback('Wrong')
Feedback.set_score(points/len(cases))

@points(3)
@name("testing user output")
def test_3(self):
cases = [
[[10], 3],
[[6,5,4,3,2,1], 7],
[[1,2,3,4,5,6], 8],
]
points = 0
for case in cases:
user_val = Feedback.call_user(self.st.poly, *case)
ref_val = self.ref.poly(*case)
if Feedback.check_scalar(name=f"args: {case}", ref=ref_val, data=user_val, report_success=False, report_failure=False):
points += 1
Feedback.add_feedback('Well Done')
else:
Feedback.add_feedback('Whoops!')
Feedback.add_feedback(f'Your answer: {user_val}\nReference Answer: {ref_val}')
with mock.patch('sys.stdout', new_callable=StringIO) as mock_stdout:
try:
self.st.poly(*case)
Feedback.add_feedback(f"stdout\n---------\n{mock_stdout.getvalue()}---------")
except Exception as e:
Feedback.add_feedback(f"error: {e}")
Feedback.set_score(points/len(cases))

@points(20)
@name("testing random cases")
def test_4(self):
cases = []
num_cases = 20
for case in range(num_cases):
coeffs = []
for i in range(10):
coeffs.append(randrange(0, 101))
x = randrange(0, 10)
cases.append([coeffs, x])
points = 0
for case in cases:
user_val = Feedback.call_user(self.st.poly, *case)
ref_val = self.ref.poly(*case)
if Feedback.check_scalar(name=f"args: {case}", ref=ref_val, data=user_val, report_success=True, report_failure=True):
points += 1
Feedback.add_feedback('Well Done')
else:
Feedback.add_feedback('Whoops!')
Feedback.add_feedback(f'Your answer: {user_val}\nReference Answer: {ref_val}')
Feedback.set_score(points/len(cases))
33 changes: 33 additions & 0 deletions questions/pl-faded-parsons-examples/make_four.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
""" Your goal is to make <code>a</code> equal 4 by the end of the script!"""

a = 1
a += 1
a *= 1
?a? += a

## res/four.txt ##
4
## res/four.txt ## the contents will be only "4"

## res/empty.txt ## the empty file
## res/empty.txt ##

## res/seuss ## just these four lines
fish(1)
fish(2)
fish('red')
fish('blue')
## res/seuss ## will be made as a .py by default

## test ##
from pl_helpers import name, points
from pl_unit_test import PLTestCase
from code_feedback import Feedback


class Test(PLTestCase):
@points(10)
@name("testing a")
def test_0(self):
Feedback.set_score(1 if self.st.a == 4 else 0)
## test ##
17 changes: 17 additions & 0 deletions questions/pl-faded-parsons-examples/make_four/info.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"uuid": "8caf2d95-a815-4d7e-b294-bad2cff70b1a",
"title": "Make Four",
"topic": "",
"tags": [
"berkeley",
"fp"
],
"type": "v3",
"gradingMethod": "External",
"externalGradingOptions": {
"enabled": true,
"image": "prairielearn/grader-python",
"entrypoint": "/python_autograder/run.sh",
"timeout": 5
}
}
Loading
Loading