Skip to content

Instantly share code, notes, and snippets.

@Nachasic
Created April 14, 2020 19:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Nachasic/c162807080b287496b3665dfa9a02bfc to your computer and use it in GitHub Desktop.
Save Nachasic/c162807080b287496b3665dfa9a02bfc to your computer and use it in GitHub Desktop.
Zhavoronok tough problems
fun <T> assert (exerciseName: String, fnResult: T, expectedResult: T) {
if (fnResult == expectedResult) {
println(exerciseName + "| checks out, GOOD JOB\n\n");
} else {
println(exerciseName + "| something's wrong: result \n (" + fnResult + ") is not equal to expected result \n (" + expectedResult + ")\n\n" )
};
};
/** Problem #1 - List Basics
* 1) Function is provided with a list of numbers;
* 2) Function return a string of these numbers separated by space:
* "4 8 15 16 23 42 "
*
* NOTE: Trailing space is acceptable
* Right now it doesn't work - FIX IT
**/
fun listToString (numbers: List<Int>): String {
var result: String = "";
for (number in numbers) {
result = result + number + " ";
};
return result;
};
/** Problem #2 - Max Number in a List
* 1) Function is provided with a list of numbers;
* 2) Function returnes the biggest number in the list.
*
* Right now it doesn't work - FIX IT
**/
fun findMax (numbers: List<Int>): Int {
var maxNumber = 0;
for (number in numbers) {
if (maxNumber > number) {
maxNumber = number;
};
};
return maxNumber;
};
/** Problem #3 - Fibonacci
* 1) Function is provided with a number N;
* 3) Function returns a List of first N numbers of a Fibonacci sequence.
*
* NOTE: Fibonnaci sequense is a sequense of numbers where each number
* is a sum of two previous numbers. The first Fibonnaci number is 1. Example sequence:
* 1, 1, 2, 3, 5, 8, 13.
* NOTE: enumeration of numbers starts with 0, so in sequence above 0th number is 1,
* 3rd number is 2, 6th number is 13
* Right now it doesn't work - FIX IT
**/
fun fibonacci (number: Int): List<Int> {
var sequence: MutableList<Int> = mutableListOf(1, 1);
for (i in 0..number) {
sequence.add(sequence[i - 1] + sequence[i - 2]);
};
return sequence;
}
fun main() {
// Problem #1 - List Basics
val list1: List<Int> = listOf(4, 8, 15, 16, 23, 42);
val resultString1 = listToString(list1);
assert("#1.1 - List Basics", resultString1, "4 8 15 16 23 42 ");
val list2: List<Int> = listOf();
val resultString2 = listToString(list2);
assert("#1.2 - List Basics", resultString2, "");
val list3: List<Int> = listOf(1, 2, 3);
val resultString3 = listToString(list3);
assert("#1.3 - List Basics", resultString3, "1 2 3 ");
// Problem #2 - Max Number in a List
val arr1: List<Int> = listOf(0, 2, 16, 11, 100, 2);
assert("#2 - Max Number: simple case", findMax(arr1), 100);
val arr2: List<Int> = listOf(99, 99, 12, 1, 1, 0);
assert("#2 - Max Number: doubles", findMax(arr2), 99);
val arr3: List<Int> = listOf(-2, -12, 0, -100, -4);
assert("#2 - Max Number: negatives", findMax(arr3), 0);
// Problem #3 - Fibonacci sequence
val sequence1 = listToString( fibonacci(4) );
assert("#3 - Fibonacci: simple case", sequence1, "1 1 2 3 5 ");
val sequence2 = listToString( fibonacci(0) );
assert("#3 - Fibonacci: low index", sequence2, "1 ");
val sequence3 = listToString( fibonacci(1) );
assert("#3 - Fibonacci: low index again", sequence3, "1 1 ");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment