Skip to content

Instantly share code, notes, and snippets.

@akshanshjain95
Created July 17, 2017 11:54
Show Gist options
  • Save akshanshjain95/76ccb6745ac38d21622719e74eef57c6 to your computer and use it in GitHub Desktop.
Save akshanshjain95/76ccb6745ac38d21622719e74eef57c6 to your computer and use it in GitHub Desktop.
package com.knoldus.kip.models
class Stack(stackWithList: List[Int]) {
def pop: Stack = {
if(stackWithList.isEmpty) {
throw new RuntimeException
}
new Stack(stackWithList.tail)
}
def push(x: Int): Stack = {
new Stack(x :: stackWithList)
} //returns the top after pushing the element on the stack
}
----------------
package com.knoldus.kip.models
class Queue(queueWithList: List[Int]) {
def enqueue(x: Int): Queue = {
val newQueue = x :: queueWithList.reverse
new Queue(newQueue.reverse)
}
def dequeue: Queue = {
new Queue(queueWithList.tail)
}
}
@sajitgupta1008
Copy link

def generateScorecards: Map[String, AnyRef] = {

def computeMarks(id: Long): Map[Long, Float] = {
  val marks = marksList.filter(id == _.studentId)

  marks.map(x => x.subjectId.toLong -> x.marksObtained).toMap[Long, Float]
}

def computePercentage(id: Long) = {
  val marks: List[Float] = marksList.filter(id == _.studentId).map(_.marksObtained)
  marks.sum / marks.length
}

def computeScoreCard(student: List[Student]): List[ScoreCard] = {
  for (s <- student)
    yield new ScoreCard(s.id, computeMarks(s.id), computePercentage(s.id))
}

def compute(list: List[Student], map: Map[String, AnyRef]): Map[String, AnyRef] = {

  if (list.isEmpty)
    map
  else if (!list.tail.exists(_.name == list.head.name)) {
    val newmap = map + (list.head.name ->
      ScoreCard(list.head.id, computeMarks(list.head.id), computePercentage(list.head.id)))
    compute(list.tail, newmap)
  }
  else {
    val newmap = map + (list.head.name -> computeScoreCard(list.head :: list.tail.filter(_.name == list.head.name)))
    compute(list.tail.filter(_.name != list.head.name), newmap)
  }

}
compute(studentList, Map[String, AnyRef]())

}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment