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

Created an autograder for R programming language #17

Open
wants to merge 2 commits into
base: main
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
Binary file added R-calculator/.DS_Store
Binary file not shown.
41 changes: 41 additions & 0 deletions R-calculator/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#
# Makefile to manage the example Hello Lab
#

# Get the name of the lab directory
LAB = $(notdir $(PWD))

all: handout handout-tarfile

handout:
# Rebuild the handout directory that students download
(rm -rf $(LAB)-handout; mkdir $(LAB)-handout)
cp -p src/README-handout $(LAB)-handout/README
cp -p src/calculator.R-handout $(LAB)-handout/calculator.R
cp -p src/driver.R $(LAB)-handout/driver.R
cp -p src/tests.R-handout $(LAB)-handout/tests.R

handout-tarfile: handout
# Build *-handout.tar and autograde.tar
tar cvf $(LAB)-handout.tar $(LAB)-handout
tar cvf autograde.tar src

clean:
# Clean the entire lab directory tree. Note that you can run
# "make clean; make" at any time while the lab is live with no
# adverse effects.
rm -f *~ *.tar
rm -rf $(LAB)-handout
rm -f autograde.tar
#
# CAREFULL!!! This will delete all student records in the logfile and
# in the handin directory. Don't run this once the lab has started.
# Use it to clean the directory when you are starting a new version
# of the lab from scratch, or when you are debugging the lab prior
# to releasing it to the students.
#
cleanallfiles:
# Reset the lab from scratch.
make clean
rm -f log.txt
rm -rf handin/*
Binary file added R-calculator/R-calculator-handout.tar
Binary file not shown.
30 changes: 30 additions & 0 deletions R-calculator/R-calculator-handout/README
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
For this lab, you should write a Python program, called "twosum.py".

Given an array of integers `nums` and an integer `target`, your program should return indices of the two numbers such that they add up to target.

You may assume that each input has at least one solution, and you may not use the same element twice.

You can return the answer in any order. Your solution should be in O(n) time for full credit.

**Example 1:**

```
Input: nums = [2,7,11,15], target = 9
Output: [0,1]
Output: Because nums[0] + nums[1] == 9, we return [0, 1].
```

```
Input: nums = [3, 2, 4], target = 6
Output: [1, 2]
Output: Because nums[1] + nums[2] == 6, we return [1, 2].
```

To compile your work:
linux> make clean; make;

Files:
README This file
twosum.py Empty Python file that you will edit
driver.py Autograding file used to test your submission
tests.py File for writing custom test cases
Leelavathi-R marked this conversation as resolved.
Show resolved Hide resolved
12 changes: 12 additions & 0 deletions R-calculator/R-calculator-handout/calculator.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
add <-function(a,b){
return 0
}
subtract <-function(a,b){
return 0
}
multiplication <-function(a,b){
return 0
}
division <-function(a,b){
return 0
}
Comment on lines +1 to +12
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Implement arithmetic operations.

The functions add, subtract, multiplication, and division currently return 0. These are placeholders and should be implemented to perform the respective arithmetic operations.

If these are intended as stubs for now, ensure that this is documented and that there is a plan to implement the actual logic.

add <- function(a, b) {
  return(a + b)
}

subtract <- function(a, b) {
  return(a - b)
}

multiplication <- function(a, b) {
  return(a * b)
}

division <- function(a, b) {
  if (b == 0) {
    stop("Division by zero error")
  }
  return(a / b)
}

26 changes: 26 additions & 0 deletions R-calculator/R-calculator-handout/driver.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
source("calculator.R")
source("tests.R")

run_testcases <- function(test_cases) {
is_passed <- 0
for (case in test_cases) {
print(case$description)
a <- case$inputs[1]
b <- case$inputs[1]
Comment on lines +8 to +9
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fix input assignment for test cases.

The variable b is incorrectly assigned the value of a. It should be assigned the second element of case$inputs.

-  b <- case$inputs[1]
+  b <- case$inputs[2]
Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
a <- case$inputs[1]
b <- case$inputs[1]
a <- case$inputs[1]
b <- case$inputs[2]


if ((a + b == add(a, b)) &&
(a - b == subtract(a, b)) &&
(a * b == multiplication(a, b)) &&
(a / b == division(a, b))) {
Comment on lines +10 to +14
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add error handling for division by zero.

Currently, there is no check for division by zero, which could cause a runtime error.

if (b == 0) {
  print('---Failed! Division by zero---')
} else if ((a + b == add(a, b)) &&
           (a - b == subtract(a, b)) &&
           (a * b == multiplication(a, b)) &&
           (a / b == division(a, b))) {
  is_passed <- is_passed + 1
  print('---Passed!---')
} else {
  print('---Failed!---')
}

is_passed <- is_passed + 1
print('---Passed!---')
} else {
print('---Failed!---')
}
}
return(is_passed)
}

# Calculate the score
test_score <- (run_testcases(test_cases) / length(test_cases)) * 100
cat(sprintf('{"scores": {"Correctness": %f}}', test_score))
11 changes: 11 additions & 0 deletions R-calculator/R-calculator-handout/tests.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#Write your custom test cases in this file.

#Each test case is formatted as an array of 3 elements:
#0. test case number
#1. Integer list
#2. Output

test_cases <- list(
list(description = "case 1", inputs = c(1, 2)),
list(description = "case 2", inputs = c(6, 3))
)
11 changes: 11 additions & 0 deletions R-calculator/R-calculator.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
require "AssessmentBase.rb"

module Hello
include AssessmentBase

def assessmentInitialize(course)
super("hello",course)
@problems = []
end

end
23 changes: 23 additions & 0 deletions R-calculator/R-calculator.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
---
general:
name: calculator
description: 'Basic calculator functions'
display_name: calculator
handin_filename: calculator.R
handin_directory: handin
max_grace_days: 0
handout: calculator-handout.tar
writeup: writeup/calculator.html
max_submissions: -1
disable_handins: false
max_size: 2
category_name: Lab
problems:
- name: Correctness
description: ''
max_score: 100.0
optional: false
autograder:
autograde_timeout: 180
autograde_image: autograding_r
release_score: true
27 changes: 27 additions & 0 deletions R-calculator/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<h1>calculator Lab</h1>

<h2>Assessment Language</h2>
R

<h2>Autograder Language</h2>
R

<h2>Autograding Environment Packages</h2>
Rscript is needed for the autograding environment.

<h2>Assessment Scenario</h2>
In this lab, students are required to implement functions add(), Subtract(),
multiplication(), and division().
which takes in two integers as input.
The function returns the answer.

<h2>Hand-in Format</h2>
calculator.R

<h2>Autograder.tar directory content</h2>
Makefile Builds the lab from src/
autograde-Makefile Makefile that runs the autograder
src/ Contains all src files and solutions
writeup/ Lab writeup that students view from Autolab


7 changes: 7 additions & 0 deletions R-calculator/autograde-Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
all:
tar xvf autograde.tar
cp -r calculator.R src
(cd src && Rscript driver.R;)

clean:
rm -rf *~ src
Binary file added R-calculator/autograde.tar
Binary file not shown.
Binary file added R-calculator/src/.DS_Store
Binary file not shown.
Empty file added R-calculator/src/.Rhistory
Empty file.
3 changes: 3 additions & 0 deletions R-calculator/src/README
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
For this lab, you should write a R program, called "calculator.R".

Given two integers a, b, your program should return indices of the final result as a single integer.
3 changes: 3 additions & 0 deletions R-calculator/src/README-handout
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
For this lab, you should write a R program, called "calculator.R".

Given two integers a, b, your program should return indices of the final result as a single integer.
12 changes: 12 additions & 0 deletions R-calculator/src/calculator.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
add <-function(a,b){
return(a+b)
}
subtract <-function(a,b){
return(a-b)
}
multiplication <-function(a,b){
return(a*b)
}
division <-function(a,b){
return(a/b)
}
12 changes: 12 additions & 0 deletions R-calculator/src/calculator.R-handout
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
add <-function(a,b){
return 0
}
subtract <-function(a,b){
return 0
}
multiplication <-function(a,b){
return 0
}
division <-function(a,b){
return 0
}
Comment on lines +1 to +12
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Implement arithmetic operations in functions.

The functions add, subtract, multiplication, and division are currently placeholders returning 0. They need to be implemented to perform the respective arithmetic operations.

Would you like guidance on how to implement these functions, or should I open a GitHub issue to track this task?

26 changes: 26 additions & 0 deletions R-calculator/src/driver.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
source("calculator.R")
source("tests.R")

run_testcases <- function(test_cases) {
is_passed <- 0
for (case in test_cases) {
print(case$description)
a <- case$inputs[1]
b <- case$inputs[1]
Comment on lines +8 to +9
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fix incorrect variable assignment.

The variable b is incorrectly assigned the value of case$inputs[1]. It should be assigned case$inputs[2] to correctly use the second input value.

-  b <- case$inputs[1]
+  b <- case$inputs[2]
Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
a <- case$inputs[1]
b <- case$inputs[1]
a <- case$inputs[1]
b <- case$inputs[2]


if ((a + b == add(a, b)) &&
(a - b == subtract(a, b)) &&
(a * b == multiplication(a, b)) &&
(a / b == division(a, b))) {
is_passed <- is_passed + 1
print('---Passed!---')
} else {
print('---Failed!---')
}
}
return(is_passed)
}

# Calculate the score
test_score <- (run_testcases(test_cases) / length(test_cases)) * 100
cat(sprintf('{"scores": {"Correctness": %f}}', test_score))
6 changes: 6 additions & 0 deletions R-calculator/src/tests.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#Write your custom test cases in this file.

test_cases <- list(
list(description = "case 1", inputs = c(1, 2)),
list(description = "case 2", inputs = c(6, 3))
)
11 changes: 11 additions & 0 deletions R-calculator/src/tests.R-handout
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#Write your custom test cases in this file.

#Each test case is formatted as an array of 3 elements:
#0. test case number
#1. Integer list
#2. Output

test_cases <- list(
list(description = "case 1", inputs = c(1, 2)),
list(description = "case 2", inputs = c(6, 3))
)
Binary file added R-calculator/writeup/.DS_Store
Binary file not shown.
12 changes: 12 additions & 0 deletions R-calculator/writeup/calculator.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<h2>Calculator Lab</h2>

In this lab, you will write a R program, called <kbd>calcualtor.R</kbd> which performs addition, subtraction, multiplication and
division.

<p>
Download the lab materials from Autolab using the "Download handout" link.

<p>
Submit your Calculator.R file to Autolab using the "Submit file" link.