Skip to content

Commit e8f24f7

Browse files
committed
22/08/2020: retrieved exercises from INGInious
1 parent 68f3dbb commit e8f24f7

File tree

16 files changed

+1087
-0
lines changed

16 files changed

+1087
-0
lines changed

MidTermQuiz/feedback_settings.yaml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# Example of file
2+
exercise_type: default
3+
has_feedback: True
4+
quorum: 1.0
5+
feedback_kind: JavaGrading
1.03 MB
Binary file not shown.

MidTermQuiz/public/LinkedList.java

Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
public class LinkedList {
2+
3+
private Node first;
4+
private Node second;
5+
private Node tail;
6+
private int len;
7+
private int nSwaps;
8+
private int nPops;
9+
10+
private static class Node{
11+
12+
private int value;
13+
private Node next;
14+
15+
private Node(int v, Node n){
16+
this.value = v;
17+
this.next = n;
18+
}
19+
20+
private int getValue() {
21+
return value;
22+
}
23+
24+
private void setNext(Node next) {
25+
this.next = next;
26+
}
27+
}
28+
29+
/**
30+
* Create a new "linkedlist" with the content of the given array
31+
* @param array
32+
*/
33+
LinkedList(int[] array) {
34+
nPops = 0;
35+
nSwaps = 0;
36+
len = 0;
37+
for(int elem: array)
38+
add(elem);
39+
}
40+
41+
/**
42+
* @return the first element of the list
43+
*/
44+
public int getFirst() {
45+
return first.value;
46+
}
47+
48+
/**
49+
* @return the second element of the list
50+
*/
51+
public int getSecond() {
52+
return second.value;
53+
}
54+
55+
/**
56+
* Swap the first and second element of the list
57+
*/
58+
public void swap(){
59+
Node oldFirst = this.first;
60+
Node third= this.second.next;
61+
this.first = this.second;
62+
oldFirst.next = third;
63+
this.first.next = oldFirst;
64+
this.second = oldFirst;
65+
nSwaps++;
66+
}
67+
68+
/**
69+
* Take the first element and put it at the end of the list
70+
*/
71+
public void pop(){
72+
Node oldFirst = this.first;
73+
this.first=this.second;
74+
this.second=this.first.next;
75+
oldFirst.next=null;
76+
this.tail.next = oldFirst;
77+
this.tail = oldFirst;
78+
nPops++;
79+
}
80+
81+
/**
82+
* Add an element to the list. You can make it public while debugging, if needed, but this will be private in
83+
* INGInious.
84+
* @param v element to add
85+
*/
86+
private void add(int v){
87+
if(len==0){
88+
this.first = new Node(v, null);
89+
this.len++;
90+
this.tail = this.first;
91+
}else if(len==1){
92+
this.second = new Node(v, null);
93+
this.first.setNext(this.second);
94+
this.len++;
95+
this.tail = this.second;
96+
}else{
97+
Node n = new Node(v, null);
98+
this.tail.next = n;
99+
this.tail = n;
100+
this.len++;
101+
}
102+
}
103+
104+
/**
105+
* @return True if the full list is sorted, False otherwise
106+
*/
107+
boolean isSorted() {
108+
Node current = this.first;
109+
if(current==null) return true;
110+
for(int i = 0 ; i < getSize()-1 && current.next!=null; i++){
111+
if(current.getValue()>current.next.getValue()) return false;
112+
current = current.next;
113+
}
114+
return true;
115+
}
116+
117+
public int getSize() {
118+
return len;
119+
}
120+
121+
public String toString(){
122+
String s = "[";
123+
Node current = this.first;
124+
while(current!=null){
125+
s+= current.value + " ";
126+
current = current.next;
127+
}
128+
129+
s += " ]";
130+
return s;
131+
}
132+
133+
public int getnSwaps() { return nSwaps; }
134+
public int getnPops() { return nPops; }
135+
}

MidTermQuiz/public/Sorter.java

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
public class Sorter {
2+
/**
3+
* Sorts a list in increasing order
4+
*
5+
* We expect this function to behave in O(n^2), where n is the size of the list.
6+
* Precisely, we expect that you make at MOST n^2 calls to list.pop() and at
7+
* MOST n^2 calls to list.swap().
8+
*
9+
* You will obtain the full score for a test case if
10+
*
11+
* - The list is sorted (2/4 points per test case)
12+
* - The list is sorted AND you make less than n^2 calls to swap (1/4 points)
13+
* - The list is sorted AND you make less than n^2 calls to pop (1/4 points)
14+
*
15+
* @param list: a list of integers to be sorted.
16+
*/
17+
public static void sort(LinkedList list) {
18+
// Here is a small loop with an invariant that you should try to respect
19+
// although it's not mandatory. Try to respect it, it will help you ;-)
20+
21+
for(int iter = 0; iter < list.getSize() - 1; iter++) {
22+
//invariant: the 'iter' biggest elements are at the end of the list and are sorted.
23+
//example, at iteration iter=3, the three lasts elements are the three biggest elements in the list, and
24+
//they are in the increasing order.
25+
26+
// TODO
27+
}
28+
29+
// here, if you followed the invariant proposed above, the list should be sorted!
30+
}
31+
}

MidTermQuiz/run

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
#! /bin/inginious-ipython
2+
import importlib.util
3+
import sys
4+
5+
6+
thecode = get_input("studentCode")
7+
if "static" not in thecode:
8+
with open('templates/Sorter.java', 'w') as f:
9+
f.write("""
10+
package templates;
11+
import src.*;
12+
13+
public class Sorter {
14+
public static void sort(LinkedList list) {
15+
@@studentCode@@
16+
}
17+
}
18+
""")
19+
20+
# Dynamically load modules we need
21+
# Credits to https://stackoverflow.com/a/67692/6149867
22+
# And for the explanation : http://www.blog.pythonlibrary.org/2016/05/27/python-201-an-intro-to-importlib/
23+
def dynamically_load_module(module, path):
24+
spec = importlib.util.spec_from_file_location(module, path)
25+
mod = importlib.util.module_from_spec(spec)
26+
spec.loader.exec_module(mod)
27+
return mod
28+
29+
30+
#####################################
31+
# Our import for common run file #
32+
#####################################
33+
sys.path.append("/course/common")
34+
35+
runfile = dynamically_load_module("runfile", "/course/common/runfile.py")
36+
runfile.main()

0 commit comments

Comments
 (0)