Monday, January 20, 2020

Scala cookbook notes-STRING

Scala Strings:

scala> "Hello, world".getClass.getName
res0: String = java.lang.String

val s = "Hello, world"
s.length//12

"hello".foreach(println)

for (c <- c="" hello="" p="" println="">
s.getBytes.foreach(println)

val result = "hello world".filter(_ != 'l')
result: String = heo word

"scala".drop(2).take(2).capitalize

Testing String Equality
scala> val s1 = "Hello"
s1: String = Hello

val s2 = "Hello"

val s3 = "H" + "ello"

s1 == s2
s1 == s3

val s4: String = null

s3 == s4

s4 == s3

val s1 = "Hello"

val s2 = "hello"

s1.toUpperCase == s2.toUpperCase

val s1: String = null

val s2: String = null

s1.toUpperCase == s2.toUpperCase
Null pointer exception


val a = "Marisa"

val b = "marisa"

a.equalsIgnoreCase(b)


1.2. Creating Multiline Strings

val foo = """This is
a multiline
String""

This is    a multiline    String

val foo = """This is
a multiline
String"""

val speech= """Four score and
|seven years ago""".stripMargin

val speech= """Four score and#seven years ago""".stripMargin('#')

Four score and
seven years ago

val speech= """Four score and
|seven years ago
|our fathers""".stripMargin.replaceAll("\n", " ")

val s = """This is known as a
|"multiline" string
|or 'heredoc' syntax.""". stripMargin.replaceAll("\n", " ")

Splitting Strings:

scala> "hello world".split(" ")res0: Array[java.lang.String] = Array(hello, world)

cala> "hello world".split(" ").foreach(println)
hello
world

cala> val s = "eggs, milk, butter, Coco Puffs"
s: java.lang.String = eggs, milk, butter, Coco Puff

scala> s.split(",")
res0: Array[java.lang.String] = Array(eggs, " milk", " butter", " Coco Puffs"

s.split(",").map(_.trim)

"hello world, this is Al".split("\\s+")

"hello world".split(" ")

"hello world".split(' ')

1.4. Substituting Variables into Strings

 val name = "Fred"

 val age = 33

val weight = 200.00

println(s"$name is $age years old, and weighs $weight pounds.")

println(s"Age next year: ${age + 1}")

println(s"You are 33 years old: ${age == 33}")

case class Student(name: String, score: Int)

val hannah = Student("Hannah", 95)

println(s"${hannah.name} has a score of ${hannah.score}")

println(s"$hannah.name has a score of $hannah.score")

println(f"$name is $age years old, and weighs $weight%.2f pounds.")

println(f"$name is $age years old, and weighs $weight%.0f pounds.")

val out = f"$name, you weigh $weight%.0f pounds."

s"foo\nbar"

r"foo\nbar"

Before 2.11
val s = "%s is %d years old".format(name, age)

println("%s is %d years old".format(name, age))

<- c="" hello="" p="" println="">





Thursday, January 16, 2020

Bank Transaction Use Case

//Bank Transaction Use cases
import scala.io.Source


//Defining the Schema
case class Transaction(
     transactionId:String,
     accountId:String,
     trasactionDay:Int,
     category:Int,
     transactionAmount:Double
     )


//Read the file
val fileName="C:/Users/Nethra/Desktop/transactions.txt"
val transactionslines=Source.fromFile(fileName).getLines().drop(1)


//Spliting and stored as List of Transaction
val transactions:List[Transaction]=transactionslines.map{line=>
    val split=line.split(',')
    Transaction(split(0),split(1),split(2).toInt,split(3),split(4).toDouble)}.toList


//Question 1- Calculate the total transaction value for all transactions for each day
val q1=transactions.groupBy(t=>t.transactionDay).mapValues(t=>t.map(x=>x.transactionAmount).sum)
q1.foreach(println)


//Question 2-Calculate the average value of transactions per account for each type of transaction (there are seven in total)
val q2=transactions.groupBy(x=>(x.accountId,x.category)).mapValues(_.map(_.transactionAmount)).mapValues(list=>list.sum/list.size)
q2.foreach(println)


//Question 3-(a) The maximum transaction value in the previous 5 days of transactions per account
val q31 = transactions.filter(trans=>trans.transactionDay<=5).groupBy(x=>x.accountId).mapValues(trans => (trans.map(amount => amount.transactionAmount).max))
q31.foreach(println)


//Question -3(b) The average transaction value of the previous 5 days of transactions per account
val q32 = transactions.filter(trans=>trans.transactionDay<=5).groupBy(x=>x.accountId).mapValues(trans => (trans.map(amount => amount.transactionAmount))).mapValues(x=>x.sum/x.size)
q32.foreach(println)


////Question -3(c) The total transaction value of transactions types “AA”, “CC” and “FF” in the previous 5 days per account
val q33 = transactions.filter(trans=>trans.transactionDay<=5 && (trans.category=="AA"||trans.category=="FF" || trans.category=="CC")).groupBy(x=>x.accountId).mapValues(t=>t.map(x=>x.transactionAmount).sum)
q32.foreach(println)

Python Challenges Program

Challenges program: program 1: #Input :ABAABBCA #Output: A4B3C1 str1="ABAABBCA" str2="" d={} for x in str1: d[x]=d...