Skip to content

Instantly share code, notes, and snippets.

@Arnonrgo
Created September 14, 2014 11:25
Show Gist options
  • Save Arnonrgo/c8db8f45b3d077e5f6b5 to your computer and use it in GitHub Desktop.
Save Arnonrgo/c8db8f45b3d077e5f6b5 to your computer and use it in GitHub Desktop.
val entryBuilder=PhoneEntry.newBuilder()
val aggregateBuilder = Aggregate.newBuilder()
val phoneBook = new mutable.HashMap[Long,PhoneEntry]()
val balances = new ListBuffer[Double]()
val hoursBreakdown = new Array[Int](24)
val sample = calls.head
var sumDuration = 0L
var sumPricing = 0.0
var minDuration = Long.MaxValue
var maxDuration = Long.MinValue
var hadAnyDiscount = false
var sumDiscount = 0.0
var sumAddCharge = 0.0
var minBalance = Double.MaxValue
var maxBalance = Double.MinValue
for (call<-calls) {
sumDuration += call.duration
minDuration = if (call.duration<minDuration) call.duration else minDuration
maxDuration = if (call.duration>maxDuration) call.duration else maxDuration
sumPricing += call.pricing
sumDiscount += call.discount
hadAnyDiscount = if (call.discount>0) true else hadAnyDiscount
sumAddCharge += call.addCharge
minBalance = if (call.balance>minBalance) call.balance else minBalance
maxBalance = if (call.balance>maxBalance) call.balance else maxBalance
balances += call.balance
phoneBook(call.toNumber) = buildPhoneEntry(call)
hoursBreakdown(call.hour) += 1
}
val count =balances.size // we can use balances as it is the same size
val avgDuration = sumDuration.toDouble / count.toDouble
val avgPricing = sumPricing / count
val avgDiscount = sumDiscount / count
val avgAddCharge = sumAddCharge / count
val sortedBalances = balances.toList.sorted // second pass just on balanced -> to get median
val mid : Int= sortedBalances.size / 2
val medianBalance = if (calls.size % 2 ==0) (sortedBalances(mid)+sortedBalances(mid+1)) /2 else sortedBalances(mid)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment