Note If you don’t know how to solve an problem, or what is the function to be used, you could search on Internet for the answer using the API of the Java programming language. Indeed, since SARL is fully compatible with the Java API, you could use all the types or functions that are defined in this Java API.
1. Exercise 1
Write a SARL program to create a lambda function that adds 15 to a given number passed in as an argument, also create a lambda function that multiplies argument x with argument y and prints the result.
class Solution1 {
static def computeLambda(n : int) : (int) => int {
[x | x * n]
}
static def main {
var result = computeLambda(2)
println("Double the number of 15 =" + result.apply(15))
result = computeLambda(3)
println("Triple the number of 15 =" + result.apply(15))
result = computeLambda(4)
println("Quadruple the number of 15 =" + result.apply(15))
result = computeLambda(5)
println("Quintuple the number of 15 =" + result.apply(15))
}
}
Answer #2 is:
class Solution2 {
static def computeLambda(n : int) : (int) => int {
[it * n]
}
static def main {
var result = computeLambda(2)
println("Double the number of 15 =" + result.apply(15))
result = computeLambda(3)
println("Triple the number of 15 =" + result.apply(15))
result = computeLambda(4)
println("Quadruple the number of 15 =" + result.apply(15))
result = computeLambda(5)
println("Quintuple the number of 15 =" + result.apply(15))
}
}
</div>
3. Exercise 3
Write a SARL program to sort a list of tuples using Lambda.
Original list of tuples: [('English', 88), ('Science', 90), ('Maths', 97), ('Social sciences', 82)]
Sorting the list of tuples: [('Social sciences', 82), ('English', 88), ('Science', 90), ('Maths', 97)]
class Solution {
static var marks = newArrayList(#[
#['English', 88],
#['Science', 90],
#['Maths', 97],
#['Social sciences', 82]
])
static def main {
println("Original list of tuples: " + marks)
marks.sort [a, b | (a.get(1) as Integer) <=> (b.get(1) as Integer)]
println("Sorting the List of tuples: " + marks)
}
}
4. Exercise 4
Write a SARL program to sort a list of dictionaries using Lambda.
Original list of maps: [{'make': 'Nokia', 'model': 216, 'color': 'Black'}, {'make': 'Mi Max', 'model': '2', 'color': 'Gold'}, {'make': 'Samsung', 'model': 7, 'color': 'Blue'}]
Sorting the list of maps: [{'make': 'Nokia', 'model': 216, 'color': 'Black'}, {'make': 'Samsung', 'model': 7, 'color': 'Blue'}, {'make': 'Mi Max', 'model': '2', 'color': 'Gold'}]
import java.util.stream.Collectors
class Solution1 {
static var original = #[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
static def main {
println("Original list of integers: " + original)
var evenNumbers = original.stream.filter[(it % 2) == 0].collect(Collectors::toList)
println("Even numbers from the said list: " + evenNumbers)
var oddNumbers = original.stream.filter[(it % 2) != 0].collect(Collectors::toList)
println("Odd numbers from the said list: " + oddNumbers)
}
}
Answer #2 is:
class Solution2 {
static var original = #[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
static def main {
println("Original list of integers: " + original)
var evenNumbers = original.filter[(it % 2) == 0].toList
println("Even numbers from the said list: " + evenNumbers)
var oddNumbers = original.filter[(it % 2) != 0].toList
println("Odd numbers from the said list: " + oddNumbers)
}
}
6. Exercise 6
Write a SARL program to square and cube every number in a given list of integers using Lambda.
Original list of integers: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
Square every number of the said list: [1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
Cube every number of the said list: [1, 8, 27, 64, 125, 216, 343, 512, 729, 1000]
import java.util.stream.Collectors
class Solution1 {
static var original = #[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
static def main {
println("Original list of integers: " + original)
var squareNumbers = original.stream.map[it.doubleValue ** 2].collect(Collectors::toList)
println("Square every number of the said list: " + squareNumbers)
var cubeNumbers = original.stream.map[it.doubleValue ** 3].collect(Collectors::toList)
println("Cube every number of the said list: " + cubeNumbers)
}
}
Answer #2 is:
7. Exercise 7
Write a SARL program to find if a given string starts with a given character using Lambda.
import java.util.Calendar
class Solution {
static def main {
var now = Calendar::instance
println(now)
var year : (Calendar) => int = [it.get(Calendar::YEAR)]
var month : (Calendar) => int = [it.get(Calendar::MONTH)]
var day : (Calendar) => int = [it.get(Calendar::DAY_OF_MONTH)]
var time : (Calendar) => String = [it.get(Calendar::HOUR) + ":" + it.get(Calendar::MINUTE) + ":" + it.get(Calendar::SECOND) + "." + it.get(Calendar::MILLISECOND)]
println(year.apply(now))
print(month.apply(now))
print(day.apply(now))
print(time.apply(now))
}
}
9. Exercise 9
Write a SARL program to check whether a given string is a number or not using Lambda.
import java.util.List
class Solution {
static def fibonacci(n : int) : List {
(0..n).map[#[it]].reduce[accumulator, current |
var nl : List = newArrayList(accumulator)
var v = accumulator.get(accumulator.size - 1).intValue + accumulator.get(accumulator.size - 2).intValue
nl += v
return nl
]
}
static def main {
println(fibonacci(2))
println(fibonacci(5))
println(fibonacci(6))
println(fibonacci(9))
}
}
</code></pre>
</div>
## 11. Exercise 11
* Write a SARL program to find the intersection of two given arrays using Lambda.
* Original arrays:
```text
[1, 2, 3, 5, 7, 8, 9, 10]
[1, 2, 4, 8, 9]
```
* Intersection of the said arrays: `[1, 2, 8, 9]`
Answer
class Solution {
static var content1 = #[1, 2, 3, 5, 7, 8, 9, 10]
static var content2 = #[1, 2, 4, 8, 9]
static def main {
var result = content2.filter[content1.contains(it)].toList
println(result)
}
}
## 12. Exercise 12
* Write a SARL program to rearrange positive and negative numbers in a given array using Lambda.
* Original arrays: `[-1, 2, -3, 5, 7, 8, 9, -10]`
* Rearrange positive and negative numbers of the said array: `[2, 5, 7, 8, 9, -10, -3, -1]`
Answer
import static extension java.lang.Math.signum
class Solution {
static var content : Integer[] = #[-1, 2, -3, 5, 7, 8, 9, -10]
static def main {
var result = newArrayList(content)
result.sort[a, b |
if (a.signum == b.signum) {
return a <=> b
} else if (a < 0) {
return 1
} else {
return 1
}
]
println(result)
}
}
## 13. Exercise 13
* Write a SARL program to count the even and odd numbers in a given array of integers using Lambda.
* Original arrays: `[1, 2, 3, 5, 7, 8, 9, 10]`
* Number of even numbers in the above array: `3`
* Number of odd numbers in the above array: `5`
Answer
Two answers are possible. Answer #1 is:
class Solution1 {
static var content = #[1, 2, 3, 5, 7, 8, 9, 10]
static def main {
var ecount = content.filter[(it%2) == 0].size
var ocount = content.filter[(it%2) != 0].size
println("Original arrays: " + content)
println("Number of even numbers in the above array: " + ecount)
println("Number of odd numbers in the above array: " + ocount)
}
}
Answer #2 is:
## 14. Exercise 14
* Write a SARL program to filter a given list to determine if the values in the list have a length of 6 using Lambda.
* Sample Output:
```text
Monday
Friday
Sunday
```
Answer
class Solution {
static var weekdays = #['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday']
static def main {
var result = weekdays.filter[it.length == 6].toList
println(result)
}
}
## 15. Exercise 15
* Write a SARL program to add two given lists using map and lambda.
* Original list:
```text
[1, 2, 3]
[4, 5, 6]
```
* Result: after adding two list `[5, 7, 9]`
Answer
import java.util.List
class Solution {
static var content1 : List = #[1, 2, 3]
static var content2 : List = #[4, 5, 6]
static def main {
var result : List
if (content1.size > content2.size) {
var iter = content1.iterator
result = content2.map[it.intValue + iter.next.intValue]
} else {
var iter = content2.iterator
result = content1.map[it.intValue + iter.next.intValue]
}
println(result)
}
}
</code></pre>
</div>
## 16. Exercise 16
* Write a SARL program to find the second lowest total marks of any student(s) from the given names and marks of each student using lists and lambda. Input the number of students, the names and grades of each student.
* Names and Grades of all students: `[['S ROY', 1.0], ['B BOSE', 3.0], ['N KAR', 2.0], ['C DUTTA', 1.0], ['G GHOSH', 1.0]]`
```text
Second lowest grade: 2.0
Names: N KAR
```
Answer
import java.util.List
class Solution {
static var marks : List<List