Skip to content

Commit e36c431

Browse files
authored
Merge 5ed1a48 into d46455c
2 parents d46455c + 5ed1a48 commit e36c431

File tree

4 files changed

+100
-4
lines changed

4 files changed

+100
-4
lines changed

.travis.yml

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
language: java
2+
sudo: true
23

34
jdk:
45
- oraclejdk8
@@ -10,10 +11,10 @@ cache:
1011

1112
env:
1213
global:
13-
- GRADLE_OPTS="-Xms128m"
14+
- GRADLE_OPTS="-Xms256m"
1415

1516
install: echo "skip 'gradle assemble' step"
16-
script: gradle build --continue
17+
script: ./gradlew checkScoverage --continue --info
1718

1819
after_success:
19-
- gradle checkScoverage coveralls
20+
- ./gradlew coveralls

gradle/wrapper/gradle-wrapper.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,4 @@ distributionBase=GRADLE_USER_HOME
33
distributionPath=wrapper/dists
44
zipStoreBase=GRADLE_USER_HOME
55
zipStorePath=wrapper/dists
6-
distributionUrl=https\://services.gradle.org/distributions/gradle-2.12-bin.zip
6+
distributionUrl=https\://services.gradle.org/distributions/gradle-3.4.1-bin.zip
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package org.bearmug.algo.course02.week03
2+
3+
import scala.collection.immutable.{SortedSet, TreeSet}
4+
5+
/**
6+
* Created by pavel on 3/12/17.
7+
*/
8+
class Median private(lHeap: SortedSet[Int], rHeap: SortedSet[Int]) {
9+
10+
type H = SortedSet[Int]
11+
12+
def +(e: Int): Median = {
13+
14+
def balance(l: H, r: H): (H, H) = (l.size, r.size) match {
15+
case (lS, rS) if lS < rS => (l + r.head, r.tail)
16+
case (lS, rS) if lS > rS + 1 => (l.tail, r + l.head)
17+
case (_, _) => (l, r)
18+
}
19+
20+
// use currying for explicit branching
21+
def add(e: Int)(f: (H, H) => (H, H)): (H, H) =
22+
(lHeap.headOption, rHeap.headOption) match {
23+
case (None, _) => f(lHeap + e, rHeap)
24+
case (Some(lHead), _) if e <= lHead => f(lHeap + e, rHeap)
25+
case (_, _) => f(lHeap, rHeap + e)
26+
}
27+
28+
Median(add(e)(balance))
29+
}
30+
31+
def calc: Int = lHeap.headOption match {
32+
case Some(m) => m
33+
case _ => Int.MaxValue
34+
}
35+
}
36+
37+
object Median {
38+
def apply(): Median = new Median(TreeSet.empty(Ordering.Int.reverse), TreeSet.empty(Ordering.Int))
39+
private def apply(lr: (SortedSet[Int], SortedSet[Int])) = new Median(lr._1, lr._2)
40+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package org.bearmug.algo.course02.week03
2+
3+
import org.junit.runner.RunWith
4+
import org.scalatest.FunSuite
5+
import org.scalatest.junit.JUnitRunner
6+
7+
import scala.io.Source
8+
import scala.util.Random
9+
10+
@RunWith(classOf[JUnitRunner])
11+
class MedianSuite extends FunSuite {
12+
13+
test("calc works for empty data") {
14+
assert(Median().calc == Int.MaxValue)
15+
}
16+
17+
test("calc works for single element") {
18+
assert((Median() + 1).calc == 1)
19+
}
20+
21+
test("calc works for two elements") {
22+
assert((Median() + 1 + 3).calc == 1)
23+
assert((Median() + 5 + 3).calc == 3)
24+
}
25+
26+
test("calc works for two same elements") {
27+
assert((Median() + 1 + 1).calc == 1)
28+
assert((Median() + 4 + 1 + 3 + 3).calc == 3)
29+
}
30+
31+
test("calc works for three elements") {
32+
assert((Median() + 1 + 3 + 2).calc == 2)
33+
assert((Median() + 3 + 2 + 1).calc == 2)
34+
assert((Median() + 2 + 3 + 1).calc == 2)
35+
}
36+
37+
test("calc works for four elements") {
38+
assert((Median() + 1 + 3 + 2 + 4).calc == 2)
39+
assert((Median() + 5 + 3 + 4 + 2).calc == 3)
40+
}
41+
42+
test("calc works for five elements") {
43+
assert((Median() + 1 + 3 + 2 + 4 + 5).calc == 3)
44+
assert((Median() + 5 + 3 + 4 + 2 + 1).calc == 3)
45+
}
46+
47+
test("calc works for even elements") {
48+
for (i <- 2 to 1000){
49+
val data: Vector[Int] = Seq.fill(i)(Random.nextInt(100000)).toSet.toVector
50+
assert(data.sorted.apply((data.length - 1)/2) == data.foldLeft(Median())((m, i) => m + i).calc,
51+
s", \nlength: $i," +
52+
s"\ninput: $data")
53+
}
54+
}
55+
}

0 commit comments

Comments
 (0)