This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Something { | |
var number: Int = 0 | |
get() { | |
println("someone is trying to read number") | |
return field | |
} | |
set(input: Int) { | |
println("someone is trying to change number") | |
if (input >= 0) | |
field = input |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Something { | |
fun doSth() { | |
println("no number") | |
} | |
fun doSth(input: Int) { | |
println(input) | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
open class Parent() { | |
open fun doSth() { | |
print("I'm Parent") | |
} | |
} | |
class Child() : Parent() { | |
override fun doSth() { | |
print("I'm Child") | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// we assume that n is never negative | |
fun calculateFactorialProcedural(n: Int): Int { | |
var factorial = 1 | |
var i = 1 | |
while (i <= n) { | |
factorial *= i | |
i++ | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
% Define family relationships | |
parent(john, mary). | |
parent(john, lisa). | |
parent(alex, john). | |
parent(lisa, tom). | |
parent(mary, ann). | |
% Define rules for various family relationships | |
father(X, Y) :- parent(X, Y), male(X). | |
mother(X, Y) :- parent(X, Y), female(X). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import io.reactivex.Observable | |
import io.reactivex.functions.BiFunction | |
fun main() { | |
val numbers = listOf(1, 2, 3, 4, 5, 6, 7, 8, 9, 10) | |
val sum = Observable.fromIterable(numbers) | |
.filter { it % 2 == 0 } // Filter even numbers | |
.reduce { total, next -> total + next } // Sum the filtered numbers |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fun main() { | |
val numbers = intArrayOf(1, 2, 3, 4, 5, 6, 7, 8, 9, 10) | |
val sum = sumOfEvenNumbers(numbers, 0, 0) | |
println("Sum of even numbers: $sum") | |
} | |
fun sumOfEvenNumbers(numbers: IntArray, index: Int, currentSum: Int): Int { | |
return when { | |
index == numbers.size -> currentSum | |
numbers[index] % 2 == 0 -> sumOfEvenNumbers(numbers, index + 1, currentSum + numbers[index]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
program SumOfEvenNumbers; | |
const | |
MaxArraySize = 100; // Maximum size of the array | |
var | |
Numbers: array[1..MaxArraySize] of Integer; | |
Size, Sum: Integer; | |
procedure CalculateSumOfEvenNumbers(arr: array of Integer; n: Integer; var result: Integer); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fun sumOfEvenNumbers(numbers: List<Int>): Int { | |
return numbers.filter { it % 2 == 0 }.sum() | |
} | |
fun main() { | |
val numbers = listOf(1, 2, 3, 4, 5, 6, 7, 8, 9, 10) | |
val result = sumOfEvenNumbers(numbers) | |
println("Sum of even numbers: $result") | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fun sumOfEvenNumbers(numbers: List<Int>): Int { | |
var sum = 0 | |
for (number in numbers) { | |
if (number % 2 == 0) { | |
sum += number | |
} | |
} | |
return sum | |
} |