Scala Exercises Solutions

Exercise 1 – Get all the distinct constituencies
Exercise 2 – Get number of constituencies by state sorted in descending order by number of constituencies
Exercise 3 – Get the number of seats for each party in each state – output should be state,bjp,inc,….

Solutions :slight_smile:

I have just given try to the first two problem statements and below is the solutoin
Only thing I have subsetted the data to first 2000 lines as my REPL starts breaking

" Exercise 2 – Get number of constituencies by state sorted in descending order by number of constituencies “
val results=Source.fromFile(”/Users/shona/results.tsv").getLines
val cons = results.map(r => (r.split("\t")(0),r.split("\t")(1))).toSet
val step3 = cons.map(r => (r.1,r.2)).toList.groupBy(r => r.1).map(r => (r.1,r.2.map(._2).length)
RESULT SET
step3: scala.collection.immutable.Map[String,Int] = Map(Arunachal Pradesh -> 2, Bihar -> 40, Goa -> 2, Gujarat -> 26, Haryana -> 6, state -> 1, Andhra Pradesh -> 42, Assam -> 14)

===================================================
Exercise 1 – Get all the distinct constituencies

Again the answer set corresponds to the first 2000 lines of source file provided

val results=Source.fromFile("/Users/shona/results.tsv").getLines.map(r => (r.split("\t")(1))).toSet

results: scala.collection.immutable.Set[String] = Set(Surat, Nellore, Valmiki Nagar, Kurnool, Bardoli, "Mahabubabad ", Gauhati, Arrah, Saran, ARUNACHAL EAST, Barpeta, Vadodara, Buxar, Narsapuram, "Tirupati ", Gaya (SC), Secundrabad, Hajipur (SC), "Bhongir ", Purvi Champaran, Madhubani, Kheda, Navsari, "Khammam ", Rajkot, …

==================================================

Exercise 3 – Get the number of seats for each party in each state – output should be state,bjp,inc,….
val results=Source.fromFile("/Users/shona/results.tsv").getLines
val statepartyvote= results.map(rec => ((rec.split("\t")(0),rec.split("\t")(6)),rec.split("\t")(13))).toList
val statepartygrp = statepartyvote.groupBy(r => r._1)
al sort = statepartygrp.map(r=>(r._1,r._2.map(p => p._2.toInt).sum)).toList.sortBy(r => r._1)

Results ( REPL Output)

sort: List[((String, String), Int)] = List(((Andhra Pradesh,AAAP),34473019), ((Andhra Pradesh,ABML(S)),4078905), ((Andhra Pradesh,AIACP),1823217), ((Andhra Pradesh,AIFB),4981959), ((Andhra Pradesh,AIMIM),9969226), ((Andhra Pradesh,ANC),1550440), ((Andhra Pradesh,APRSSP),5368247), ((Andhra Pradesh,ARPS),4744801), ((Andhra Pradesh,BCBDP),3183083), ((Andhra Pradesh,BCUF),2921548), ((Andhra Pradesh,BJP),18858804), ((Andhra Pradesh,BMUP),6478522), ((Andhra Pradesh,BSP),62215302),

Have verified data in source file swell

shunalis-MacBook-Air:~ Shona$ awk -F’\t’ ‘$1 == “Andhra Pradesh” && $7 == “AIACP” {sum += $14 ;print $1,$7,sum }’ results.tsv
Andhra Pradesh AIACP 1823217

1 Like

For Exercise-3:
Why did you take rec.split("\t")(13) which is total voters?

I assume Number of seats for each party in a state means, Number of contestants participating from a party in a state

val fileName = “F:/ls2014.tsv"
val results = Source.fromFile(fileName).getLines.drop(1)
val statePartySeats =
results.map(a => (a.split(”\t")(0),a.split("\t")(6)))
.toList.groupBy(a => (a._1, a._2))
.map(a => (a._1._1, a._1._2, a._2.size)).toList.sorted

Results

First 10 results of seats of each party in each state in alphabetical order: statePartySeats.take(10)
.foreach(println)

(Andaman & Nicobar Islands,AAAP,1)
(Andaman & Nicobar Islands,AIFB,1)
(Andaman & Nicobar Islands,AITC,1)
(Andaman & Nicobar Islands,BJP,1)
(Andaman & Nicobar Islands,BSP,1)
(Andaman & Nicobar Islands,CPI(ML)(L),1)
(Andaman & Nicobar Islands,CPM,1)
(Andaman & Nicobar Islands,INC,1)
(Andaman & Nicobar Islands,IND,5)
(Andaman & Nicobar Islands,NCP,1)

First 10 seats in state of “Andhra Pradesh” in the descending order of number of seats: statePartySeats.filter(a => a._1 == “Andhra Pradesh”)
.sortBy[Int](a =>._3.toInt)(Ordering[Int])
.reverse .take(10).foreach(println)
(Andhra Pradesh,IND,233)
(Andhra Pradesh,NOTA,42)
(Andhra Pradesh,INC,41)
(Andhra Pradesh,BSP,40)
(Andhra Pradesh,YSRCP,38)
(Andhra Pradesh,PPOI,34)
(Andhra Pradesh,TDP,30)
(Andhra Pradesh,JaSPa,27)
(Andhra Pradesh,AAAP,21)
(Andhra Pradesh,TRS,17)

For Exercise-2
To sort them

val fileName = “F:/ls2014.tsv"
val results = Source.fromFile(fileName).getLines.drop(1)
val stateNumConsti = results.map(a => (a.split(”\t")(0), a.split("\t")(1)))
.toList.groupBy(a => a._1)
.map(a =>(a._1,a._2.toSet.size))
.toList.sortBy(a => a._2)

Results:

Size: println(stateNumConsti.size
33

First 10 States in the ascending order of number of constituencies: stateNumConsti.take(10).foreach(println)

(Dadra & Nagar Haveli,1)
(Chandigarh,1)
(Daman & Diu,1)
(Mizoram,1)
(Puducherry,1)
(Andaman & Nicobar Islands,1)
(Nagaland,1)
(Lakshadweep,1)
(Sikkim,1)
(Tripura,2)