public
Description: An implementation of a solution to Ruby Quiz 65 (http://rubyquiz.com/quiz65.html)
Homepage:
Clone URL: git://github.com/rrees/ruby-tuesday-loot-splitter.git
ruby-tuesday-loot-splitter / scala / src / splitter.scala
100644 31 lines (22 sloc) 0.718 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
import gems._
 
package splitter {
 
class LootSplitter
 
object LootSplitter {
def splitLoot(gembag: GemBag, shares: Int): List[GemBag] = {
 
if((gembag.totalValue % shares) != 0) {
return List[GemBag]()
}
 
val individualShareValue = gembag.totalValue / shares
 
var partShares: List[GemBag] = List.make(shares, new GemBag(List()))
 
gembag.gems.sort(_.value > _.value).foreach((gem: Gem) => {
assignGemToBag(gem, partShares)
partShares = partShares.sort(_.totalValue < _.totalValue)
partShares = List(partShares(0) add gem) ::: (partShares drop 1)
})
 
partShares
}
 
private def assignGemToBag(gem:Gem, bags: List[GemBag]): List[GemBag] = {
bags
}
}
}