Skip to content

Commit 22fd796

Browse files
committed
ADD: new exercise
1 parent af8519f commit 22fd796

File tree

8 files changed

+327
-0
lines changed

8 files changed

+327
-0
lines changed
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
# Instructions
2+
3+
The first line of the CSV file contains the headings, the other lines contain the results of the participants.
4+
Each line has the following elements separated by commas: an id, a name, then 10 points for 10 exercises.
5+
A maximum of 10 points can be achieved in each exercise. A value of -1 means that no solution has been
6+
submitted.
7+
The lines of the files can be read by:
8+
```scala
9+
val lines = readCSV()
10+
```
11+
For the following calculations should be solved using Scala’s collections with their higher-order
12+
functions. Try to implement each calculation by a single chain of operations.
13+
14+
## Task 1: Building a list of Results objects
15+
The first task is to build a list of Results objects from the read lines. Implement a case class Results
16+
with an id of type Int, as name of type String and an list of points of type List[Int]. Then compute
17+
the list of Results objects for the participants
18+
```scala
19+
val resultList : List[Results] = ???
20+
```
21+
22+
## Task 2: Number of solved tasks
23+
From the list of Results objects, create a map that contains the number of solved exercises for each
24+
student. An exercise is considered solved if at least 3 points have been achieved.
25+
26+
## Task 3: Sufficient tasks solved
27+
A student must solve at least 8 exercises to pass the course. Determine which students have submitted
28+
enough solutions and which have not enough solutions.
29+
```scala
30+
val sufficientSolved : (Set[String], Set[String]) = ...
31+
```
32+
_Hint: Use method partition._
33+
34+
## Task 4: Grading
35+
36+
Next, the grades should be determined. The grades are calculated based on the points: There must be at least eight solutions with at least 3
37+
points. If this is the case, the grading is done based on the average of the best eight solutions according
38+
to the following scheme:
39+
```
40+
- [0.0 .. 5.0) : INSUFFICIENT
41+
- [5.0 .. 6.25) : SUFFICIENT
42+
- [6.25 .. 7.5) : SATISFACTORY
43+
- [7.5 .. 8.75) : GOOD
44+
- [8.75 .. 1.0] : EXCELLENT
45+
```
46+
```scala
47+
val grades: Map[String, Grade] = ...
48+
```
49+
50+
## Task 5: Grade statistics
51+
52+
Compute statistic measurement from the grading. For each grade, compute the number of students with
53+
that grade:
54+
```scala
55+
val nStudentsWithGrade : Map[Grade, Int] = ...
56+
```
57+
58+
## Task 6: Number solved per assignment
59+
For the 10 exercises, compute how many students have solved them (at least 3 points). Result is a list of
60+
10 numbers, which indicate how many students have solved the exercises:
61+
```scala
62+
val nSolvedPerAssnmt : List[(Int, Int)] = ...
63+
```
64+
65+
## Task 7: Average points per assignment
66+
For the 10 exercises, compute the average of the achieved points. Only the submitted exercises (points
67+
!= -1) should be considered:
68+
```scala
69+
val avrgPointsPerAssnmt : List[(Int, Double)] = ...
70+
```
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# Processing Results with Collections
2+
In this assignment, we want to use the Scala collections API for analyzing the results of a programming
3+
course. The starting point is a CSV file in which the points achieved in 10 programming exercises are
4+
stored for the course participants:
5+
ID,Name,1,2,3,4,5,6,7,8,9,10
6+
1,Alred Maier,10,8,2,7,10,9,7,6,7,2
7+
2,Fritz Michalik,8,6,5,7,-1,-1,8,6,8,10
8+
...
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
object ResultsAnalysis {
2+
3+
}
4+
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
{
2+
"authors": [
3+
"mrtob"
4+
],
5+
"contributors": [
6+
"mrtob"
7+
],
8+
"files": {
9+
"solution": [
10+
"src/main/scala/ResultsAnalysis.scala"
11+
],
12+
"test": [
13+
"src/test/scala/ResultsAnalysisTest.scala"
14+
],
15+
"example": [
16+
".meta/Example.scala"
17+
],
18+
"invalidator": [
19+
"build.sbt"
20+
]
21+
},
22+
"blurb": "Analyze the points of students in a class to ´genrate statistics.",
23+
"source": "Exercism",
24+
"source_url": ""
25+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
scalaVersion := "3.4.2"
2+
3+
libraryDependencies += "org.scalatest" %% "scalatest" % "3.2.19" % Test
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
sbt.version=1.10.1
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
def readCSV(): List[String] = {
2+
List(
3+
"ID,Name,1,2,3,4,5,6,7,8,9,10",
4+
"1,Jane Doe,9,10,9,6,10,9,10,10,10,9",
5+
"2,John Doe,8,6,5,7,-1,-1,8,6,8,10",
6+
"3,Jake Doe,10,9,9,-1,5,-1,8,-1,10,9",
7+
"4,Jill Doe,10,8,2,7,10,9,7,6,7,2",
8+
)
9+
}
10+
11+
case class Results(id: Int, name: String, points: IndexedSeq[Int])
12+
13+
enum Grade:
14+
case EXCELLENT, GOOD, SATISFACTORY, SUFFICIENT, INSUFFICIENT
15+
16+
object ResultsAnalysis {
17+
18+
def task1(lines: List[String]): List[Results] =
19+
//TODO: Implement this method
20+
???
21+
22+
def task2(resultList: List[Results]): Map[String, Int] =
23+
//TODO: Implement this method
24+
???
25+
26+
def task3(nSolvedPerStnd: Map[String, Int]): (Set[String], Set[String]) =
27+
//TODO: Implement this method
28+
???
29+
30+
def task4(resultList: List[Results]): Map[String, Grade] =
31+
//TODO: Implement this method
32+
???
33+
34+
def task5(resultList: List[Results]): Map[Grade, Int] =
35+
//TODO: Implement this method
36+
???
37+
38+
def task6(resultList: List[Results]): List[(Int, Int)] =
39+
//TODO: Implement this method
40+
???
41+
42+
def task7(resultList: List[Results]): List[(Int, Double)] =
43+
//TODO: Implement this method
44+
???
45+
46+
def main(args: Array[String]): Unit = {
47+
val lines = readCSV()
48+
49+
// Task 1
50+
val resultList: List[Results] = task1(lines)
51+
52+
// Task 2
53+
val nSolvedPerStnd = task2(resultList)
54+
55+
// Task 3
56+
val sufficientSolved = task3(nSolvedPerStnd)
57+
58+
// Task 4
59+
val grades = task4(resultList)
60+
61+
//Task 5
62+
val nStudentsWithGrade = task5(resultList)
63+
64+
//Task 6
65+
val nSolvedPerAssnmt = task6(resultList)
66+
67+
//Task 7
68+
val avrgPointsPerAssnmt = task7(resultList)
69+
}
70+
71+
private def computeGrade(points: IndexedSeq[Int]): Grade = {
72+
if (points.count(p => p >= 3) < 8) then Grade.INSUFFICIENT
73+
else {
74+
val avrg = points.sorted.drop(2).sum / 8
75+
if (avrg < 5.0) then Grade.INSUFFICIENT
76+
else if (avrg < 6.5) then Grade.SUFFICIENT
77+
else if (avrg < 8.0) then Grade.SATISFACTORY
78+
else if (avrg < 9.0) then Grade.GOOD
79+
else Grade.EXCELLENT
80+
}
81+
}
82+
}
Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
import org.scalatest.funsuite.AnyFunSuite
2+
import org.scalatest.matchers.should.Matchers
3+
4+
import scala.collection.immutable.HashMap
5+
6+
7+
/** @version 1.2.0 */
8+
class ResultsAnalysisTest extends AnyFunSuite with Matchers {
9+
10+
test("Task 1") {
11+
val lines = List(
12+
"ID,Name,1,2,3,4,5,6,7,8,9,10",
13+
"1,Jane Doe,9,10,9,6,10,9,10,10,10,9",
14+
"2,John Doe,8,6,5,7,-1,-1,8,6,8,10",
15+
"3,Jake Doe,10,9,9,-1,5,-1,8,-1,10,9",
16+
"4,Jill Doe,10,8,2,7,10,9,7,6,7,2",
17+
)
18+
19+
val expectedResults = List(
20+
Results(1, "Jane Doe", IndexedSeq(9, 10, 9, 6, 10, 9, 10, 10, 10, 9)),
21+
Results(2, "John Doe", IndexedSeq(8, 6, 5, 7, -1, -1, 8, 6, 8, 10)),
22+
Results(3, "Jake Doe", IndexedSeq(10, 9, 9, -1, 5, -1, 8, -1, 10, 9)),
23+
Results(4, "Jill Doe", IndexedSeq(10, 8, 2, 7, 10, 9, 7, 6, 7, 2))
24+
)
25+
26+
val resultList = ResultsAnalysis.task1(lines)
27+
resultList shouldEqual expectedResults
28+
}
29+
30+
test("Task 2") {
31+
val results = List(
32+
Results(1, "Jane Doe", IndexedSeq(9, 10, 9, 6, 10, 9, 10, 10, 10, 9)),
33+
Results(2, "John Doe", IndexedSeq(8, 6, 5, 7, -1, -1, 8, 6, 8, 10)),
34+
Results(3, "Jake Doe", IndexedSeq(10, 9, 9, -1, 5, -1, 8, -1, 10, 9)),
35+
Results(4, "Jill Doe", IndexedSeq(10, 8, 2, 7, 10, 9, 7, 6, 7, 2))
36+
)
37+
38+
val expectedResults = Map(
39+
"Jane Doe" -> 10,
40+
"John Doe" -> 8,
41+
"Jake Doe" -> 7,
42+
"Jill Doe" -> 8
43+
)
44+
45+
val resultList = ResultsAnalysis.task2(results)
46+
resultList shouldEqual expectedResults
47+
}
48+
49+
test("Task 3") {
50+
val results = Map(
51+
"Jane Doe" -> 10,
52+
"John Doe" -> 8,
53+
"Jake Doe" -> 7,
54+
"Jill Doe" -> 8
55+
)
56+
57+
val expectedResults = (Set("Jane Doe", "John Doe", "Jill Doe"), Set("Jake Doe"))
58+
59+
val resultList = ResultsAnalysis.task3(results)
60+
resultList shouldEqual expectedResults
61+
}
62+
63+
test("Task 4") {
64+
val results = List(
65+
Results(1, "Jane Doe", IndexedSeq(9, 10, 9, 6, 10, 9, 10, 10, 10, 9)),
66+
Results(2, "John Doe", IndexedSeq(8, 6, 5, 7, -1, -1, 8, 6, 8, 10)),
67+
Results(3, "Jake Doe", IndexedSeq(10, 9, 9, -1, 5, -1, 8, -1, 10, 9)),
68+
Results(4, "Jill Doe", IndexedSeq(10, 8, 2, 7, 10, 9, 7, 6, 7, 2))
69+
)
70+
71+
val expectedResults = Map(
72+
"Jane Doe" -> Grade.EXCELLENT,
73+
"John Doe" -> Grade.SATISFACTORY,
74+
"Jake Doe" -> Grade.INSUFFICIENT,
75+
"Jill Doe" -> Grade.GOOD
76+
)
77+
78+
val resultList = ResultsAnalysis.task4(results)
79+
resultList shouldEqual expectedResults
80+
}
81+
82+
test("Task 5") {
83+
val results = List(
84+
Results(1, "Jane Doe", IndexedSeq(9, 10, 9, 6, 10, 9, 10, 10, 10, 9)),
85+
Results(2, "John Doe", IndexedSeq(8, 6, 5, 7, -1, -1, 8, 6, 8, 10)),
86+
Results(3, "Jake Doe", IndexedSeq(10, 9, 9, -1, 5, -1, 8, -1, 10, 9)),
87+
Results(4, "Jill Doe", IndexedSeq(10, 8, 2, 7, 10, 9, 7, 6, 7, 2))
88+
)
89+
90+
val expectedResults = HashMap(
91+
Grade.EXCELLENT -> 1,
92+
Grade.SATISFACTORY -> 1,
93+
Grade.INSUFFICIENT -> 1,
94+
Grade.GOOD -> 1
95+
)
96+
97+
val resultList = ResultsAnalysis.task5(results)
98+
resultList shouldEqual expectedResults
99+
}
100+
101+
test("Task 6") {
102+
val results = List(
103+
Results(1, "Jane Doe", IndexedSeq(9, 10, 9, 6, 10, 9, 10, 10, 10, 9)),
104+
Results(2, "John Doe", IndexedSeq(8, 6, 5, 7, -1, -1, 8, 6, 8, 10)),
105+
Results(3, "Jake Doe", IndexedSeq(10, 9, 9, -1, 5, -1, 8, -1, 10, 9)),
106+
Results(4, "Jill Doe", IndexedSeq(10, 8, 2, 7, 10, 9, 7, 6, 7, 2))
107+
)
108+
109+
val expectedResults = List(
110+
(1, 4), (2, 4), (3, 3), (4, 3), (5, 3), (6, 2), (7, 4), (8, 3), (9, 4), (10, 3)
111+
)
112+
113+
val resultList = ResultsAnalysis.task6(results)
114+
resultList shouldEqual expectedResults
115+
}
116+
117+
test("Task 7"){
118+
val results = List(
119+
Results(1, "Jane Doe", IndexedSeq(9, 10, 9, 6, 10, 9, 10, 10, 10, 9)),
120+
Results(2, "John Doe", IndexedSeq(8, 6, 5, 7, -1, -1, 8, 6, 8, 10)),
121+
Results(3, "Jake Doe", IndexedSeq(10, 9, 9, -1, 5, -1, 8, -1, 10, 9)),
122+
Results(4, "Jill Doe", IndexedSeq(10, 8, 2, 7, 10, 9, 7, 6, 7, 2))
123+
)
124+
125+
val expectedResults = List(
126+
(1,9.25), (2,8.25), (3,6.25), (4,6.666666666666667), (5,8.333333333333334), (6,9.0), (7,8.25), (8,7.333333333333333), (9,8.75), (10,7.5)
127+
)
128+
129+
val resultList = ResultsAnalysis.task7(results)
130+
resultList shouldEqual expectedResults
131+
132+
}
133+
134+
}

0 commit comments

Comments
 (0)