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.
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.
import java.util.List
class Solution {
static var content1 : List<Integer> = #[1, 2, 3]
static var content2 : List<Integer> = #[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<Object>> = #[
#['S ROY', 1.0],
#['B BOSE', 3.0],
#['N KAR', 2.0],
#['C DUTTA', 1.0],
#['G GHOSH', 1.0]
]
static def main {
var result = marks
.sortWith [a, b | (b.get(1) as Double) <=> (a.get(1) as Double)]
var firstMark = result.get(0).get(1) as Double
var secondMark : Double
var studentsNames = newArrayList
for (student : result) {
var m = student.get(1) as Double
if (secondMark === null) {
if (m != firstMark) {
secondMark = m
studentsNames += student.get(0) as String
}
} else if (m == secondMark) {
studentsNames += student.get(0) as String
} else {
break;
}
}
println("Second lowest grade: " + secondMark)
println("Names: " + studentsNames)
}
}
## 17. Exercise 17
* Write a SARL program to find numbers divisible by nineteen or thirteen from a list of numbers using Lambda.
* Orginal list: `[19, 65, 57, 39, 152, 639, 121, 44, 90, 190]`
* Numbers of the above list divisible by nineteen or thirteen: `[19, 65, 57, 39, 152, 190]`
Answer
class Solution {
static var original = #[19, 65, 57, 39, 152, 639, 121, 44, 90, 190]
static def main {
var result = original.filter [(it % 19) == 0 || (it % 13) == 0].toList
println("Orginal list: " + original)
println("Numbers of the above list divisible by nineteen or thirteen: " + result)
}
}
## 18. Exercise 18
* Write a SARL program to find palindromes in a given list of strings using Lambda.
* Orginal list of strings: `['php', 'w3r', 'SARL', 'abcd', 'Java', 'aaa']`
* List of palindromes: `['php', 'aaa']`
Answer
class Solution {
static var original = #['php', 'w3r', 'SARL', 'abcd', 'Java', 'aaa']
static def main {
var result = original.filter[it.palindrome].toList
println("List of palindromes: " + result)
}
static def palindrome(value : String) : boolean {
var i = 0
var j = value.length - 1
while (i < j) {
if (value.charAt(i) != value.charAt(j)) {
return false
}
}
return true
}
}
## 19. Exercise 19
* Write a SARL program to find all anagrams of a string in a given list of strings using Lambda.
* Original list of strings: `['bcda', 'abce', 'cbda', 'cbea', 'adcb']`
* Anagrams of 'abcd' in the above string: `['bcda', 'cbda', 'adcb']`
Answer
import java.util.Map
class Solution {
static var original = #['bcda', 'abce', 'cbda', 'cbea', 'adcb']
static def main {
var search_for = "abcd".counter
var result = original.filter[
(search_for == it.counter)
].toList
println("List of palindromes: " + result)
}
static def counter(value : String) : Map<Character, Integer> {
var counter = newHashMap
for (c : value.bytes) {
var n = counter.getOrDefault(c as char, 0)
n++
counter.put(c as char, n)
}
return counter
}
}
## 20. Exercise 20
* Write a SARL program to find the numbers in a given string and store them in a list. Afterward, display the numbers that are longer than the length of the list in sorted form. Use the lambda function to solve the problem.
* Original string: `sdf 23 safs8 5 sdfsd8 sdfs 56 21sfs 20 5`
* Numbers in sorted form: `20 23 56`
Answer
class Solution {
static var original = "sdf 23 safs8 5 sdfsd8 sdfs 56 21sfs 20 5"
static def main {
var words = original.split("\s+")
var numbers = words.filter[isInteger].map[it as Integer].sort.toList
println("Numbers in sorted form: " + numbers)
var length = words.size
var result = numbers.filter[it > length].toList
println(result.join(' '))
}
static def isInteger(value : String) : boolean {
try {
Long::parseLong(value)
return true
} catch (ex : Throwable) {
return false
}
}
}
## 21. Exercise 21
* Write a SARL program that multiplies each number in a list with a given number using lambda functions. Print the results.
* Original list: `[2, 4, 6, 9, 11]`
* Given number: `2`
* Result: `4 8 12 18 22`
Answer
class Solution {
static var original = #[2, 4, 6, 9, 11]
static def main {
var search_for = 2
var result = original.map[it * search_for].toList
println(result)
}
}
## 22. Exercise 22
* Write a SARL program that sums the length of a list of names after removing those that start with lowercase letters. Use the lambda function.
* Result: `16`
Answer
import static extension java.lang.Character.isUpperCase
class Solution {
static var original = #['sally', 'Dylan', 'rebecca', 'Diana', 'Joanne', 'keith']
static def main {
var result = original.filter[it.charAt(0).isUpperCase].map[it.length]
.reduce[accumulator, current | accumulator + current]
println(result)
}
}
## 23. Exercise 23
* Write a SARL program to calculate the sum of the positive and negative numbers of a given list of numbers using the lambda function.
* Original list: `[2, 4, -6, -9, 11, -12, 14, -5, 17]`
* Sum of the positive numbers: `-32`
* Sum of the negative numbers: `48`
Answer
import java.util.List
class Solution {
static var original = #[2, 4, -6, -9, 11, -12, 14, -5, 17]
static def main {
var positive = original.filter[it >= 0].sum
var negative = original.filter[it < 0].sum
println("Sum of the positive numbers: " + positive)
println("Sum of the negative numbers: " + negative)
}
static def sum(value : Iterable) : int {
var sum = 0
for (c : value) {
sum += c
}
return sum
}
}
</code></pre>
</div>
## 24. Exercise 24
* Write a SARL program to find numbers within a given range where every number is divisible by every digit it contains.
* Input Range: `1..22`
* Sample Output: `[1, 2, 3, 4, 5, 6, 7, 8, 9, 11, 12, 15, 22]`
Answer
import java.util.List
class Solution {
static def divisible_by_digits(start_num : int, end_num : int) : List {
(start_num..end_num).filter[n |
((n as String).bytes).forall[digit | (n%(digit as int)) == 0]
].toList
}
static def main {
println(divisible_by_digits(1,22))
}
}
</code></pre>
</div>
## 25. Exercise 25
* Write a SARL program to create the next bigger number by rearranging the digits of a given number.
* Original number: `12`
* Next bigger number: `21`
* Original number: `10`
* Next bigger number: `null`
* Original number: `201`
* Next bigger number: `210`
* Original number: `102`
* Next bigger number: `120`
* Original number: `445`
* Next bigger number: `454`
Answer
class Solution {
static def rearrange_bigger(n : int) : Integer {
var nums = (n as String).bytes.map[it as Integer]
for (i : (nums.size-2)..0) {
if (nums.get(i) < nums.get(i+1)) {
var z = nums.subList(i, nums.size)
var y = z.filter[it > z.get(0)].min
z -= y
z = z.sort
var new_nums = (nums.subList(0, i) + #[y] + z)
return (new_nums.join("")) as Integer
}
}
return null
}
static def dotest(n : int) {
println("Original number: " + n)
println("Next bigger number: " + rearrange_bigger(n))
}
static def main {
dotest(12)
dotest(10)
dotest(201)
dotest(102)
dotest(445)
}
}
## 26. Exercise 26
* Write a SARL program to find a list with maximum and minimum length using lambda.
* Original list: `[[0], [1, 3], [5, 7], [9, 11], [13, 15, 17]]`
* List with maximum length of lists: `(3, [13, 15, 17])`
* List with minimum length of lists: `(1, [0])`
Answer
import java.util.List
class Solution {
static var original = #[#[0], #[1, 3], #[5, 7], #[9, 11], #[13, 15, 17]]
static def max_length_list(input_list : List<List>) : List