Finally a video that explain differences in between list types. Thank you so much!
@AniobiStanley Жыл бұрын
This is my solution to the difficult assignment: val list = mutableListOf(0, 1) var num = 1 while (num
@ahmadosama2652 Жыл бұрын
this is my simple solution of Fibonacci homework val list = mutableListOf() println("Enter a number n > 1 : ") val n = readln().toInt() var a = 0 ; var b = 1; var temp = 0 for(i in 1..n){ list.add(a) temp = a + b b = a a = temp } println(list)
@anmoltrvd068 ай бұрын
Easier Version of Homework :- fun main(){ var myList = mutableListOf() for (i in 1..5){ val x = readln().toInt() myList.add(x) } myList.reverse() println(myList) }
@leqso7005 Жыл бұрын
That feeling when your code works...
@karllviii10 ай бұрын
Thanks bro that's pleasure of programming.
@arunmk83492 ай бұрын
fun main() { var array = mutableListOf() println("How many numbers you want to enter") var number = readLine()?.toInt() var x = 0 println("Enter Numbers") if(number != null) { for(i in 1..number) { var x = readLine()?.toInt() if(x != null){ array.add(x) } } } println(array) println(array.reversed()) }
@moyaalhayek32882 жыл бұрын
What is the difference between readLine()?.toInt() and if (x != null) {...} ? isn't it technically impossible for x to be null when reaching (x != null)? basically my question is why are we double checking if x == null? once before passing it toInt and once before adding it to list.
@dropinscience6584 Жыл бұрын
readLine()?.toInt() is simply to prevent null.toInt() which won't work. When that happens, you skip calling .toInt() but you still need to return something, so null?.toInt() returns null.
@sanjuswami227 Жыл бұрын
@@dropinscience6584 thanku
@daiyrkanybekov85754 жыл бұрын
Great Tutorial.But I have Question: What is difference between 'val' and 'var' when declare mutableListOf ? Because when I declare mutableList with val and var they behave similar (both of them changing list elements).
@saravanaprabhualagappan9264 жыл бұрын
val valList = mutableListOf(1,2,3,4) var varList = mutableListOf(5,6,7,8) valList = varList //Error varList = valList //Can execute
@DeathlabelCSS6 ай бұрын
easy homework: fun main() { println("Enter 5 numbers:") val list = mutableListOf() for(i in 1..5){ val x = readln()?.toInt() if(x != null){ list.add(x) } } println("The reverse order of the numbers entered is ${list.asReversed()}") }
@cancion2able4 жыл бұрын
Hello, Philipp! Thanks for these series, your content is awesome! Please help me resolve a confusion about the quiz question. The question is what do we need Arrays for? You're saying that we can mix types in Arrays while Lists don't allow such behavior. So I see this as a correct answer. However, the quiz claims that the right answer is about the lists being less performant as they store more information about indexes and so on. I'm not sure it's a true statement and I am even less sure that it affects performance. Can you shed some light?
@PhilippLackner4 жыл бұрын
You can mix types in lists as well, that hasn't something to do with the data structure. But I agree, that your second point is valid and my answer is a little misleading. I guess I should replace performant with efficient. Lists need to save information about the next (and maybe previous) item. That doesn't make them slower, but they need more memory, so I guess efficient would be better here. Thanks for the info :)
@ziyadmansy60165 жыл бұрын
Why is there an error in my code? println("Enter a number") val n = readLine()?.toInt() val list = mutableListOf() list[0] = 0 list[1] = 1 if (n !== null) { for (i in 2..n-1) { list.add(list[i-1] + list[i-2]) } } println(list) The error: Exception in thread "main" java.lang.IndexOutOfBoundsException: Index 0 out of bounds for length 0
@karllviii10 ай бұрын
Becouse your list does't have any variable, instead of list[0] use list.add(), again, you can't set to first index to it cuz list doesn't have any index
@dawiddebowski43464 жыл бұрын
Hi! Is there any difference in typing "val list = mutableListOf..." and "var list = mutableListOf..."?
@PhilippLackner4 жыл бұрын
yes, if you use var, you can assign a new list to your list, if you write val, you can't
@dawiddebowski43464 жыл бұрын
@@PhilippLackner thanks. And what about elements of list? I mean are they affected somehow?
@emresubasi43483 жыл бұрын
@@dawiddebowski4346 No it doesn't matter for single list
@AniobiStanley Жыл бұрын
Why is it that we can change the values of an array and a mutabke list even when they were declared with the "Vall" keyword?
@aakashkumaryadav6048 Жыл бұрын
Thank you sir for the amazing video. //HomeWork 1: fun main() { val list = mutableListOf() println("Please enter 5 numbers: ") for(i in 1..5) { val x = readlnOrNull()?.toInt() if(x != null) { list.add(x) } } println("The 5 numbers in reverse order: ") for(i in 4 downTo 0) { println(list[i]) } } //HomeWork 2: fun main() { val list = mutableListOf() println("Enter a number n > 1:") val x = readlnOrNull()!!.toInt() var first = 0 list.add(first) var second = 1 list.add(second) for(i in 3..x) { val third = first + second list.add(third) first = second second = third } println(list) }
@arthurmsiska38002 жыл бұрын
Noob question, what does the += operator mean
@thiagogomes90432 жыл бұрын
maybe you can understand with this example: var x = 5 var x = 5 x += 10 x = x + 10 if you println(x) in both cases, it results 15.
@arthurmsiska38002 жыл бұрын
@@thiagogomes9043 Thanks, I get it now
@wellingtonschutz67522 жыл бұрын
easy and simple: fun main() { var list = mutableListOf(0, 1) println("Entre com o valor a ser fibonatizado:") var contador = readLine()?.toInt() var in1 = 0 var in2 = 1 var memoria = 0 if (contador != null) { for (i in 3..contador) { memoria = list[in1] + list[in2] list.add(memoria) in1++ in2++ } } println(list)
@miguelr17844 жыл бұрын
Although a bit late, here is my solution to Fibonacci´s series println("Enter a range number") val range = readLine()?.toInt() val list = mutableListOf() if (range != null) { for (i in 0 until range) { when (list.size) { 0 -> list.add(i) 1 -> list.add(list.get(i - 1) + 1) else -> list.add(list.get(i - 2) + list.get(i - 1)) } } } println(list)
@DeathlabelCSS6 ай бұрын
hard homework: Probably a much clearer way of accomplishing this, but it works well so im not mad at it. fun main() { print("Enter a number n > 1:") var x = readln()?.toInt() val list = mutableListOf() if(x != null){ var secondToLast = 0 var last = 1 if(x 2){ val curLen = list.size secondToLast = list[curLen - 2] last = list[curLen - 1] list.add(secondToLast + last) x-- } } println(list) }
@patilpatadeАй бұрын
fun main(){ val myList = mutableListOf() println("Enter elements in a list: ") for (i in 1..5){ var num = readLine()?.toInt() if(num!=null){ myList.add(num) } } print(myList.reversed()) }
@alh311 Жыл бұрын
Hard homework: val list = mutableListOf(0, 1) println("Enter a number n > 1:") val n = readLine()?.toInt() if (n == null) { return } for (i in 2 until n) { list.add(list[i - 2] + list[i - 1]) } println(list)
@ahmadosama2652 Жыл бұрын
this one using recursion function } for(i in 0.. n-1) list.add(fibonacci(i)) println(list) } fun fibonacci(n : Int): Int{ if(n== 0 || n == 1) return n return fibonacci(n - 1) + fibonacci(n - 2) }
@Twishma16 күн бұрын
Easy Version fun main(){ val list = mutableListOf() for (i in 1..5){ val inputs = readlnOrNull()?.toInt() if (inputs != null){ list.add(inputs) list.reverse() } } println(list) }
@filip_g3 жыл бұрын
Thanks for this tutorial Philipp. Fibonacci homework wasn't easy but I somehow managed to make it work on my own. Let me know if you have any feedback. print("Please enter number n > 1: ") var n = readLine()?.toInt() if (n != null) { var list = mutableListOf(0, 1) var n1 = 0 var n2 = 1 for (i in 3..n) { var n3 = n1 +n2 n1 = n2 n2 = n3 list.add(n3) } println(list) }
@anmoltrvd068 ай бұрын
Harder Version of Homework:- fun main() { var t = readln().toInt() var n1 = 0 var n2 = 1 var myList = mutableListOf() myList.add(n1) myList.add(n2) for (i in 1..t-2){ var nextTerm = n1 + n2 n1 = n2 n2 = nextTerm myList.add(nextTerm) } println(myList) }
@17barifchoudhary887 ай бұрын
fun main() { val list=mutableListOf() println("enter") for(i in 1..5) { val x = readLine()?.toInt() if(x != null) { list.add(x) } } println(list) } is their any bug in this code
@VijayRaj-pw2px Жыл бұрын
fun main() { // user to enter 5 numbers which are added to list and printed in reverse order println("Please enter 5 numbers:") val numList = mutableListOf() for(i in 1..5) { val x = readLine()?.toInt() if(x != null) { numList.add(x) } } var i = 5 for (item in numList) { i-- println(numList[i]) } // user to enter a number (n) > 1 and then calculate the first n fibonacci numbers and print them in a list println("Enter a number (n) that is > 1:") val n = readLine()?.toInt() if(n != null) { val newList = mutableListOf() newList.add(0) newList.add(1) while(n > newList.size) { val x = newList[newList.size - 1] + newList[newList.size - 2] newList.add(x) } println(newList) } }
@Aaditya-b6h2 ай бұрын
fun main(){ var myList = mutableListOf() println("Please enter 5 numbers: ") for ( i in 1..5){ val number = readLine()?.toInt() if(number != null){ myList.add(number) } } println("The number in reverse are: ") for(item in myList.asReversed()){ println(item) } }
@AniobiStanley Жыл бұрын
val newList = mutableListOf() for (i in 0..4) newList.add(readln().toInt()) for (i in newList.size-1..0) println(newList[i]) I tried using this solution for the easy assignment, but the last line is not executing. Please, an you help me figure out what I may have done wrong?
@willianrodrigohuber7968 Жыл бұрын
the challenge of this homework are really greats, my answer stay bellow. This video is such a helpfull, thanks for it. fun main() { reversedList() fibonacci() } fun fibonacci() { var values = mutableListOf(0, 1) println("Enter a number n > 1") var number = readlnOrNull()?.toInt() var i = 0 number?.let { while (i < (number - 2) ) { if (i == 0) { values.add(values[0] + values[1]) } else { values.add(values[i] + values[i + 1]) } i++ } } println("fibonacci value: $values") } fun reversedList() { var values = mutableListOf() var reverseList = mutableListOf() println("Please enter 5 number") for (i in 0..4) { var input = readlnOrNull() input?.let { values.add(it.toInt()) } } for (i in 4 downTo 0) { reverseList.add(values[i]) } println("Using for loop: $reverseList") println("Using reversed method: ${values.reversed()} ") }
@SuccessMotivation921 Жыл бұрын
fun main() { var list = mutableListOf(0,1) var x = readLine()?.toInt() if (x != null) { for (i in 0..x-3) { list.add(list[i] + list[i + 1]) } } println(list) }
@harsh-np8jf10 ай бұрын
fun main() { var list= mutableListOf(0,1) var n= readln()?.toInt() if (n!=null){ for (i in 2..n-1){ list.add(list[i-2]+list[i-1]) } } println(list) }
@bogite8734 Жыл бұрын
Homework 1: fun main () { println("Please enter 5 numbers:") val myList = mutableListOf() for (i in 1..5) { val num = readLine()?.toInt() if (num != null) myList.add(num) } println("The 5 numbers in reverse order are: ") for (i in 4 downTo 0) println(myList[i]) } Homework 2: fun main () { println("Please enter a n > 1:") val fib_length = readLine()?.toInt() var fib_list = mutableListOf(0, 1) if (fib_length != null) { var index1 = 0 var index2 = 1 while (fib_list.size < fib_length) { val traversed = fib_list.size-2 val new_number = fib_list[index1+traversed] + fib_list[index2+traversed] fib_list.add(new_number) } } println(fib_list) }
@nathanaelmoh58483 ай бұрын
fun main() { var myList = mutableListOf() println("Please enter the amount of numbers you want for your Fibonacci sequence:") val listLength = readLine()?.toInt() if (listLength != null) { if (listLength < 1) { println("Value is too low for sequence length.") } else if (listLength == 1) { myList.add(0) } else { myList.add(0) myList.add(1) var index = 0 while (index < listLength - 2) { var nextNum = myList[index] + myList[index + 1] myList.add(nextNum) index++ } } println("The Fibonacci sequence up to $listLength numbers is:") println(myList) } else { println("Invalid value entered for sequence length.") } }
@younesscoder Жыл бұрын
Thanks again for this great tutorial 👏 => This is my solution for the easy Homework: fun main() { println("Please Enter 5 numbers: ") var numbersList = mutableListOf() for(i in 1..5) { var number = readLine()?.toInt() if(number != null) { numbersList.add(number) } } println("The 5 numbers in reverse orderis: ") var x = numbersList.size - 1 for(i in numbersList) { println(numbersList[x]) x -= 1 } } => The hard Homework: fun main() { println("Enter a number n > 1") var number = readLine()?.toInt() var numbersList = mutableListOf(0, 1) var x = 0 while(number != null && numbersList.size
@guy14074 жыл бұрын
since you stated that we know that the list/array is 10 in size, it is possible to use array and not list: fun main() { val arr = IntArray(10) var t = 10 var i = 0 while (i < t) { println("Enter a number :") var n = readLine() if (n != null) { arr[i] = n.toInt() } i++ } for (item in arr) { println(item) } }//fun main(){
@wenjin9813 Жыл бұрын
Interesting, println cannot print array but can print list
@harrisaliqamar4 жыл бұрын
For Fibonacci println("Enter a n > 1 for fibonacci") val input = readLine()?.toInt()!! val list = mutableListOf(0, 1) for (i in 1 until input - 1) { list.add(list.elementAt(i) + list.elementAt(i - 1)) } println(list)
@angelmauriciorivas61602 жыл бұрын
We already saw that you are very cool but let the rest of us learn by trying to solve the exercises
@mdmeraaz53353 ай бұрын
fun main () { val list = mutableListOf() for(i in 1..5){ val x = readLine()?.toInt() if(x!=null){ list.add(x) } } var i = 4; while(i>=0){ println(list[i]) i--; } }
@saadwadea4030 Жыл бұрын
fun main() { println("Enter a number n > 1:") val number= readLine()?.toInt() val array= mutableListOf() var t1=0 var t2=1 if (number!=null) for (item in 1..number){ array.add(t1) val sum =t1+t2 t1=t2 t2=sum } println(array) }
@alh311 Жыл бұрын
Easy homework: val list = mutableListOf() println("Please enter 5 numbers:") for (i in 1..5) { print("Input $i: ") val input = readLine()?.toInt() if (input != null) { list.add(input) } } println("The 5 numbers in reverse order are:") list.reversed().forEach { println(it) }
@ekkawutemeelugsana70683 жыл бұрын
Hi this is my homework, I've just tried to only use everything I've learn so far I don't know if this is a good solution or not. feel free to review. thanks fun main() { val reverseMenuId = "1" val fibonacciMenuId = "2" val exitMenuId = "E" var exitProgram = false val menuItem = arrayOf( "1 : [Reverse order]", "2 : [Fibonacci numbers]", "E : [Exit program]" ) println("Kotlin Newbie To Pro - LISTS - Part 14 Homework") while (exitProgram == false) { println("Press key to select menu...") for (item in menuItem) println(item) val userSelectedMenu = readLine()?.uppercase() val reverseMenuItemSelected = (userSelectedMenu == reverseMenuId) val fibonacciMenuItemSelected = (userSelectedMenu == fibonacciMenuId) val exitProgramMenuSelected = (userSelectedMenu == exitMenuId) if (reverseMenuItemSelected) { println("Let the user enter 5 numbers and save them in a list," + " and print them in reverse order.") println("Please enter 5 numbers : ") val numberList = mutableListOf() while (numberList.size < 5){ val userInputNumber = readLine()?.toIntOrNull() if (userInputNumber != null) numberList.add(userInputNumber) } print("Your input numbers : ") for(number in numberList) print(" $number ") println() print("Reverse order result : ") while (numberList.size > 0) { print(" ${numberList.last()} ") numberList.removeAt(numberList.size - 1) } println() } else if (fibonacciMenuItemSelected) { println("Let the user enter a number that is larger then 1" + " and create Fibonacci numbers") println("Enter a number > 1 : ") var userInput : Int? = readLine()?.toIntOrNull() var validNumberInput : Int? = if(userInput != null && userInput > 1) userInput else null val fibonacciList = arrayListOf() while (validNumberInput == null){ println("[Invalid input please Enter a number > 1 ]") userInput = readLine()?.toIntOrNull() if(userInput != null && userInput > 1){ validNumberInput = userInput } } for (i in 0 until validNumberInput){ if (fibonacciList.size < 2) fibonacciList.add(i) else{ val previousNumber = fibonacciList[i - 1] val beforePreviousNumber = fibonacciList[i - 2] val fibonacciNumber = previousNumber + beforePreviousNumber fibonacciList.add(fibonacciNumber) } } println("Fibonacci numbers result : $fibonacciList" ) } else if (exitProgramMenuSelected) exitProgram = true else println("[Invalid input]") } }
@aka-Monster012 жыл бұрын
look wtf i have done in last video homework without watching solution😅 fun main() { println("enter any 5 numbers") val a = readLine()?.toInt() val b = readLine()?.toInt() val c = readLine()?.toInt() val d = readLine()?.toInt() val e = readLine()?.toInt() val myArray = arrayOf(a, b, c, d, e) var x = 0F for(i in myArray) { if (i != null && x != null) x += i } if (x != null) { println("The average of theose number is ${x / 5}") } }
@userglls11 ай бұрын
fun main() { //Homework val list = mutableListOf() println("Please Enter 5 numbers: ") for (i in 1..5) { val g = readLine()?.toInt() if (g != null) { list.add(g) } } println(list.reversed()) }
@karllviii10 ай бұрын
println("Enter a number, n > 1") var x = readln().toInt() var list = mutableListOf(0,1) var y = 0 for (i in 0..x-3){ y = list[i] + list[i+1] list.add(y) } println(list)
@jonievillejo6746 Жыл бұрын
fun main(){ var fiboList = mutableListOf() print("Enter a number > 1: ") val userInput = readLine()?.toInt() if ( (userInput != null) && userInput > 1 ) { var prevNumber:Int = -1 var currNumber:Int = 1 for ( i in 1..userInput ) { val theValue = prevNumber + currNumber fiboList.add(theValue) prevNumber = currNumber currNumber = theValue } } println(fiboList) }