0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, ……..
object Fibonacci {
def fibonacci(n: Int): Int =
if (n < 3) 1
else fibonacci(n - 1) + fibonacci(n - 2)
def main(args: Array[String]) {
for {i <- List.range(1, 17)}
print(fibonacci(i) + ", ")
println("...")
}
}
Plindrom
object demo
{
def palindrome(s : String) : Boolean = {
val sLetters = s.toLowerCase.filter(c => c.isLetter)
(sLetters == sLetters.reverse)
}
def main(args :Array[String])
{
println("enter a line of text\n")
val input = readLine
if (palindrome(input))
println("is a palindrome")
else
println("is not a palindrome")
}
}
object test
{
def fact(n:Int):Int={
if (n < 1)
1
else
n * fact(n-1)
}
def main(args:Array[String])
{
for ( i <- 1 to 10)
println("Factorial" +"=" + i + ":" + fact(i))
}
}
// Inserting Objects
scala> val name = "Jose"
name: String = Jose
// String Interploation
scala> val greet = s"Hello ${name}"
greet: String = Hello Jose
scala> val greet = f"Hello $name"
greet: String = Hello Jose
//////////////////////////
/// Regular Expressions ///
///////////////////////////
// Index Location
scala> val st = "This is a long string"
st: String = This is a long string
scala> st.charAt(0)
res18: Char = T
scala> st.indexOf("a")
res19: Int = 8
// Pattern matching
scala> val st = "term1 term2 term3"
st: String = term1 term2 term3
scala> st contains "term1"
res20: Boolean = true
scala> st matches "term1"
res11: Boolean = false
scala> st matches "term1 term2 term3"
res12: Boolean = true
// Slicing
scala> val st = "hello"
st: String = hello
scala> st slice (0,2)
res2: String = he
scala> st slice (2,st.length)
res4: String = llo
object Fibonacci {
def fibonacci(n: Int): Int =
if (n < 3) 1
else fibonacci(n - 1) + fibonacci(n - 2)
def main(args: Array[String]) {
for {i <- List.range(1, 17)}
print(fibonacci(i) + ", ")
println("...")
}
}
Plindrom
object demo
{
def palindrome(s : String) : Boolean = {
val sLetters = s.toLowerCase.filter(c => c.isLetter)
(sLetters == sLetters.reverse)
}
def main(args :Array[String])
{
println("enter a line of text\n")
val input = readLine
if (palindrome(input))
println("is a palindrome")
else
println("is not a palindrome")
}
}
object test
{
def fact(n:Int):Int={
if (n < 1)
1
else
n * fact(n-1)
}
def main(args:Array[String])
{
for ( i <- 1 to 10)
println("Factorial" +"=" + i + ":" + fact(i))
}
}
// Inserting Objects
scala> val name = "Jose"
name: String = Jose
// String Interploation
scala> val greet = s"Hello ${name}"
greet: String = Hello Jose
scala> val greet = f"Hello $name"
greet: String = Hello Jose
//////////////////////////
/// Regular Expressions ///
///////////////////////////
// Index Location
scala> val st = "This is a long string"
st: String = This is a long string
scala> st.charAt(0)
res18: Char = T
scala> st.indexOf("a")
res19: Int = 8
// Pattern matching
scala> val st = "term1 term2 term3"
st: String = term1 term2 term3
scala> st contains "term1"
res20: Boolean = true
scala> st matches "term1"
res11: Boolean = false
scala> st matches "term1 term2 term3"
res12: Boolean = true
// Slicing
scala> val st = "hello"
st: String = hello
scala> st slice (0,2)
res2: String = he
scala> st slice (2,st.length)
res4: String = llo
No comments:
Post a Comment