Skip to content

Instantly share code, notes, and snippets.

@hhimanshu
Last active June 15, 2025 03:59
Show Gist options
  • Select an option

  • Save hhimanshu/55564b70dac97d0aa4eb to your computer and use it in GitHub Desktop.

Select an option

Save hhimanshu/55564b70dac97d0aa4eb to your computer and use it in GitHub Desktop.
Scala for the Impatient Exercises
/**
* 2. In the Scala REPL, compute the square root of 3, and then square that value. By how much does the result differ from 3? (Hint: The res variables are your friend.)
*/
scala> math.sqrt(3)
res1: Double = 1.7320508075688772
scala> res1*res1
res2: Double = 2.9999999999999996
scala> 3 - res2
res3: Double = 4.440892098500626E-16
// 3. Are the res variables val or var?
scala> res1 = 3
<console>:11: error: reassignment to val
res1 = 3
^
/**
* 4. Scala lets you multiply a string with a number—try out "crazy" * 3 in the REPL.
* What does this operation do? Where can you find it in Scaladoc?
*/
// StringOps.* : Return the current string concatenated n times.
scala> "crazy "*3
res6: String = "crazy crazy crazy "
/**
* 5. What does 10 max 2 mean? In which class is the max method defined?
*/
// Int.max: Returns this if this > that or that otherwise.
scala> 10 max 2
res7: Int = 10
scala> 10.max(2)
res8: Int = 10
/**
* 6. Using BigInt, compute 21024.
*/
scala> BigInt(2) pow 1024
res13: scala.math.BigInt = 179769313486231590772930519078902473361797697894230657273430081157732675805500963132708477322407536021120113879871393357658789768814416622492847430639474124377767893424865485276302219601246094119453082952085005768838150682342462881473913110540827237163350510684586298239947245938479716304835356329624224137216
/**
* 7. What do you need to import so that you can get a random prime as probablePrime(100, Random),
* without any qualifiers before probablePrime and Random?
*/
scala> import scala.util.Random
import scala.util.Random
scala> import scala.math.BigInt
import scala.math.BigInt
scala> import scala.math.BigInt.probablePrime
import scala.math.BigInt.probablePrime
scala> probablePrime(100, Random)
res14: scala.math.BigInt = 931669304734992063893063804797
/**
* 8. One way to create random file or directory names is to produce a random BigInt
* and convert it to base 36, yielding a string such as "qsnvbevtomcj38o06kul".
* Poke around Scaladoc to find a way of doing this in Scala.
*/
scala> probablePrime(100, Random) toString 36
res15: String = 2mojnrl2508jtnu26t01
/**
* 9. How do you get the first character of a string in Scala? The last character?
*/
scala> "Hello".head
res20: Char = H
scala> "Hello".last
res21: Char = o
/**
* 10. What do the take, drop, takeRight, and dropRight string functions do?
* What advantage or disadvantage do they have over using substring?
*/
// StringOps.drop: Selects all elements except first n ones.
// StringOps.dropRight: Selects all elements except last n ones.
// StringOps.take: Selects first n elements.
// StringOps.takeRight: Selects last n elements.
scala> "Hello World!" take 6
res22: String = "Hello "
scala> "Hello World!" drop 6
res23: String = World!
scala> "Hello World!" takeRight 6
res24: String = World!
scala> "Hello World!" dropRight 6
res25: String = "Hello "
/**
* 1. The signum of a number is 1 if the number is positive, –1 if it is negative, and 0 if it is zero.
* Write a function that computes this value.
*/
scala> def signum(num:Int) = if (num > 0) 1 else if (num < 0) -1 else 0
signum: (num: Int)Int
scala> signum(10)
res4: Int = 1
scala> signum(0)
res5: Int = 0
scala> signum(-123)
res6: Int = -1

This gist will contain all the exercises from the book

@B1llioner
Copy link

Пишу на питоне, ничо не понятно

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment