Skip to content

Latest commit

 

History

History
142 lines (89 loc) · 5.7 KB

ch2-WorkingWithData.asciidoc

File metadata and controls

142 lines (89 loc) · 5.7 KB

1) Write a new centigrade-to-fahrenheit conversion (using the formula (x * 9/5) + 32), saving each step of the conversion into separate values. What do you expect the type of each value will be?

Answer

Let’s pick a centigrade temperature at random, say 7 degrees. For optimal accuracy we’ll use 5.0, a floating-point number, to ensure the remainder of the division is preserved.

scala> val celsius = 7
celsius: Int = 7

scala> val fahr1 = celsius * 9
fahr1: Int = 63

scala> val fahr2 = fahr1 / 5.0
fahr2: Double = 12.6

scala> val fahrenheit = fahr2 + 32
fahrenheit: Double = 44.6

We now have named values for each step of the calculation, with a final answer of 44.6 degrees fahrenheit.

2) Modify the centigrade-to-fahrenheit formula to return an integer instead of a floating-point number.

Answer

The "5.0" floating-point number value resulted in the final result having the same type. To return an integer, we can switch the one floating-point number to an integer.

scala> val fahr2 = fahr1 / 5
fahr2: Int = 12

scala> val fahrenheit = fahr2 + 32
fahrenheit: Int = 44

The result, 44, is pretty darn close to the value calculated with a floating-point divisor, 44.6.

3) Using the input value 2.7255, generate the string "You owe $2.73 dollars". Is this doable with string interpolation?

Answer

String interpolation, the act of inserting placeholders for named values, is the right solution here. It will let us use printf formatting to convert the input value down to only two decimal values. The printf format for doing so is "%.2f", which specifies that exactly two of the most significant decimal digits will be printed.

The really hard part is getting the dollar sign that prefixes the amount to print, as it is right up against the dollar sign we’ll need to trigger string interpolation. Fortunately a double dollar sign will be recognized as a request to print a literal dollar sign, which we can follow with another dollar sign to begin interpolation.

scala> val amount = 2.7255
amount: Double = 2.7255

scala> val s = f"You owe $$$amount%.2f dollars"
s: String = You owe $2.73 dollars

4) Is there a simpler way to write the following?

val flag: Boolean = false
val result: Boolean = (flag == false)

Answer

This was a deliberately easy question, written with the goal to prod you to look for ways to reduce unnecessary code (even in the simple value assignments given here). A correct answer is that both the explicit types and the lengthy comparison can be simplified, requiring one to check both sides of the equation for code that can be removed.

scala> val flag = false
flag: Boolean = false

scala> val result = !flag
result: Boolean = true

5) Convert the number 128 to a Char, a String, a Double, and then back to an Int. Do you expect the original amount to be retained? Do you need any special conversion functions for this?

Answer

You’ll need a host of conversion functions for this one! If you’re unsure of the exact function name, you can find them in the Scaladoc pages for the source type. You can also try the REPL’s tab-based. code completion feature. Go ahead and type "128." followed by a tab to see the available functions for that integer.

scala> val orig = 128
orig: Int = 128

scala> val c: Char = orig.toChar
c: Char = �

scala> val s: String = c.toString
s: String = �

scala> val d: Double = s(0).toDouble
d: Double = 128.0

scala> val i: Int = d.toInt
i: Int = 128

6) Using the input string "Frank,123 Main,925-555-1943,95122" and regular expression matching, retrieve the telephone number. Can you convert each part of the telephone number to its own integer value? How would you store this in a tuple?

Answer

Let’s break this into separate steps to keep the parsing code simple. First, we’ll extract the phone number into three separate strings.

scala> val s = "Frank,123 Main,925-555-1943,95122"
s: String = Frank,123 Main,925-555-1943,95122

scala> val p = """.*,(\d{3})-(\d{3})-(\d{4}),.*""".r
p: scala.util.matching.Regex = .*,(\d{3})-(\d{3})-(\d{4}),.*

scala> val p(p1,p2,p3) = s
p1: String = 925
p2: String = 555
p3: String = 1943

Second, we’ll convert the strings to integers and put them in a new tuple.

scala> val phoneNumber = (p1.toInt, p2.toInt, p3.toInt)
phoneNumber: (Int, Int, Int) = (925,555,1943)

Our new 3-sized tuple, "phoneNumber", contains all three integer segments of the phone number in the input string.