-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRecursion.scala
More file actions
117 lines (92 loc) · 2.96 KB
/
Recursion.scala
File metadata and controls
117 lines (92 loc) · 2.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
import scala.annotation.tailrec
object Recursion {
/**
* Computes the factorial of a non-negative integer using traditional recursion.
*
* @param x The non-negative integer for which factorial is computed.
* @return The factorial of the given integer.
*/
def factorial(x: Int): Int = {
// Ensure that x is non-negative
require(x >= 0, "x must be greater than or equal to zero!")
// Base case
if x < 1 then 1
else
// Recursive step
x * factorial(x - 1)
}
/**
* Computes the factorial of a non-negative integer using tail recursion.
*
* @param x The non-negative integer for which factorial is computed.
* @return The factorial of the given integer.
*/
def factorialTail(x: Int): Int = {
require(x >= 0, "x must be greater than or equal to zero!")
/**
* Helper function for tail-recursive factorial computation.
*
* @param n Accumulator for intermediate results.
* @param x Current value.
* @return The result of factorial computation.
*/
@tailrec
def factorialHelper(n: Int, x: Int): Int = {
// Base case
if x <= 1 then n
else
// Purely recursive call
factorialHelper(n * x, x - 1)
}
factorialHelper(1, x)
}
/**
* Checks whether parentheses in a given list are balanced.
*
* @param chars The list of characters containing parentheses.
* @return True if parentheses are balanced, false otherwise.
*/
def balance(chars: List[Char]): Boolean = {
/**
* Helper function for checking balanced parentheses.
*
* @param chars The list of characters.
* @param openCount The count of open parentheses.
* @return True if parentheses are balanced, false otherwise.
*/
@tailrec
def balanceHelper(chars: List[Char], openCount: Int): Boolean =
if chars.isEmpty then openCount == 0
else if chars.head.equals(')') && openCount == 0 then false
else if chars.head.equals('(') then balanceHelper(chars.tail, openCount + 1)
else if chars.head.equals(')') && openCount > 0 then balanceHelper(chars.tail, openCount - 1)
else balanceHelper(chars.tail, openCount)
balanceHelper(chars, 0)
}
/**
* Computes the sum of a function over a range using tail recursion.
*
* @param f The function to sum.
* @param a The start of the range.
* @param b The end of the range.
* @return The sum of applying the function over the specified range.
*/
def sum(f: Int => Int, a: Int, b: Int): Int =
/**
* Helper function for tail-recursive sum computation.
*
* @param a Current value.
* @param acc Accumulator for the sum.
* @return The result of sum computation.
*/
@tailrec
def sumHelper(a: Int, acc: Int): Int =
if a > b then acc
else sumHelper(a + 1, acc + f(a))
sumHelper(a, 0)
def main(args: Array[String]): Unit = {
println(balance("(if (zero? x) max (/ 1 x))".toList))
println(balance("())(".toList))
println(sum(x => x*x , 1, 5)) //sum of squares of numbers in 1 to 5
}
}