Kotlin Newbie To Pro - VARARG, DEFAULT AND NAMED PARAMETERS - Part 18

  Рет қаралды 22,732

Philipp Lackner

Philipp Lackner

Күн бұрын

Пікірлер: 43
@PB-op3tk
@PB-op3tk 4 жыл бұрын
Hi, i think there is a little mistake in the solution (14:20). In line 14, shouldn't it be: for (i in 0 until list.size), because until does not include the last number, so if you would search for 7 it would return -1 However, nice videos, thanks! 👍
@PB-op3tk
@PB-op3tk 4 жыл бұрын
Okay, I just saw that someone mentioned the problem in the next video.. so, that applies here, too :)
@veeecos
@veeecos 2 жыл бұрын
+1
@luismarez4792
@luismarez4792 4 жыл бұрын
first homework i share: fun alternatingSum(vararg numbers: Int) : Int { var n: Int = 1 var sum: Int = 0 for(item in numbers) { sum += (item * n) n *= -1 } return sum } and thank you for your tutorials they really help me
@willianrodrigohuber7968
@willianrodrigohuber7968 Жыл бұрын
Thanks for this video, my answer for the homework stay bellow fun main() { val sum = alternatingSum(3, 4, 5, 2, 1, 2, 3) println("The alternating sum is $sum") } fun alternatingSum(vararg values: Int): Int { var sum = 0 for (index in values.indices) { if (index % 2 == 0) sum += values[index] else sum -= values[index] } return sum }
@younesscoder
@younesscoder Жыл бұрын
Thanks again dude =>This is the solution of the Homework fun main() { val sum = alternatingSum(3, 4, 5, 2, 1, 2, 3) println("The alternating sum is $sum") } fun alternatingSum(vararg numbers: Int): Int { var result = numbers[0] var o = 0 while(o != (numbers.size - 1)) { o++ result -= numbers[o] o++ result += numbers[o] } return result }
@VijayRaj-pw2px
@VijayRaj-pw2px Жыл бұрын
fun main() { // create function that will take a vararg list of numbers and return an alternating sum val num = alternatingSum(2, 4, 8, 10, 14) println("The alternating sum is $num") } fun alternatingSum(vararg numbers: Int): Int { var total = 0 for(i in 0 until numbers.size step 2) { total += numbers[i] } for(i in 1 until numbers.size step 2) { total -= numbers[i] } return total }
@opdmin
@opdmin 2 жыл бұрын
fun main() { val sum = alternatingSum(16,64,47,2,9,18,3,1,74) println("The alternating sum is $sum") } fun alternatingSum(vararg numbers: Int): Int { var sum = 0 val numberTotal = numbers.size - 1 for(i in 0.. numberTotal) { if(i%2 != 0) { // odd indexes sum -= numbers[i] // subtract value } else { // positive indexes (includes index 0) sum += numbers[i] // add value } } return sum }
@swakshatmishra
@swakshatmishra 6 ай бұрын
fun alternatingSum(vararg numbers: Int): Int { var sum = numbers[0] for(i in 1..
@aakashkumaryadav6048
@aakashkumaryadav6048 Жыл бұрын
//HomeWork : fun main() { val result = alternatingSum(3, 4, 5, 2, 1, 2, 3) println("The alternating sum is: $result") } fun alternatingSum(vararg numbers: Int): Int { var evenSum = 0 var oddSum = 0 for(i in numbers.indices) { if(numbers[i]%2 == 0) { evenSum += numbers[i] } else { oddSum += numbers[i] } } return oddSum - evenSum } //Thankk you sir
@ekkawutemeelugsana7068
@ekkawutemeelugsana7068 3 жыл бұрын
fun main() { val sum = alternatingSum(3,4,5,2,1,2,3) println("The alternating sum is $sum") } fun alternatingSum(vararg numbers : Int) : Int { var result = 0 for((index,item) in numbers.withIndex()) if (index % 2 == 0) result += item else result -= item return result }
@Macias096
@Macias096 3 жыл бұрын
Is it okay to use, in for loop, the indices function instead of 0 until list.size-1? Looks like this: for (i in list.indices). Program works fine with that.
@paulvickers8059
@paulvickers8059 3 жыл бұрын
fun main() { val sum = alternatingSum(3, 4, 5, 2, 1, 2, 3) println("The alternating sum is $sum") } fun alternatingSum(vararg numbers: Int): Int { var result = numbers[0] for (i in 1 until numbers.size) { if (i % 2 == 0) result += numbers[i] else result -= numbers[i] } return result }
@PandleHandle
@PandleHandle 4 жыл бұрын
What does the asterisk in *array do? Are there any other uses for it?
@PandleHandle
@PandleHandle 4 жыл бұрын
The * operator is known as the Spread Operator in Kotlin. When we call a vararg-function, we can pass arguments one-by-one, e.g. asList(1, 2, 3), or, if we already have an array and want to pass its contents to the function, we use the spread operator (prefix the array with *):
@soualehmohamedaya6625
@soualehmohamedaya6625 3 жыл бұрын
Homework : fun alternatingSum(vararg numbers: Int) : Int { var sum = 0 var x = 0 for ( i in numbers ) { if ( x == 0 ) { sum += i x = 1 } else { sum -= i x = 0 } } return sum } fun main() { println(alternatingSum(1,2,3,4,5,6)) }
@TheImaginativeSachin
@TheImaginativeSachin 2 жыл бұрын
interesting
@itamarribeiro8865
@itamarribeiro8865 4 жыл бұрын
fun alternatingSum(vararg values: Int) { var result = 0 var sum = true for (i in values) { if (sum) result += i else result -= i sum = !sum } println("The alternating sum is $result") }
@MrSarma1991
@MrSarma1991 4 жыл бұрын
my homework :D fun alternatingSum(vararg niz: Int):Int { var plusminus=0 var rezultat=0 for(i in niz) { if (plusminus==0) { rezultat += i plusminus = 1 }else { rezultat -= i plusminus = 0 } } return rezultat }
@AniobiStanley
@AniobiStanley Жыл бұрын
fun alternatingSum(vararg numbers: Int): Int{ var sum = 0 for (i in numbers.indices){ if (i % 2 == 0) sum += numbers[i] else sum -= numbers[i] } return sum }
@alh311
@alh311 Жыл бұрын
Homework: fun alternatingSum(vararg numbers: Int): Int { var sum = 0 var signModifier = 1 for (number in numbers) { sum += (signModifier * number) signModifier *= -1 } return sum }
@shakibhasan858
@shakibhasan858 3 жыл бұрын
a little mistake in line number 14. in the for loop the until keyword don't include the last number. So it should be list.indices
@Jonathan1Brito
@Jonathan1Brito Жыл бұрын
fun main() { sumValue(1, 5) } fun sumValue(a:Int, b:Int){ var count = a var res = 0 while (count
@pranaydas3320
@pranaydas3320 Жыл бұрын
fun main() { val sum = alternatingSum(3, 4, 5, 2, 1, 2, 3, 20,12, 4, 6,7,9,34) println("The alternating sum is $sum") } fun alternatingSum(vararg numbers: Int): Int { var altSum = 0 for (i in numbers.indices) { if (i%2 == 0){ altSum += numbers[i] } else { altSum += numbers[i] * -1 } } return altSum }
@Twishma
@Twishma 12 күн бұрын
fun main() { val sum = alternatingSum(3,4,5,2,1,2,3) println("The alternating sum is $sum") } fun alternatingSum(vararg numbers: Int): Int { var sum = 0 for(i in 0 until numbers.size) { if(i % 2 == 0){ sum += numbers[i] }else{ sum -= numbers[i] } } return sum }
@jonievillejo6746
@jonievillejo6746 Жыл бұрын
fun main() { println("For this list [3, 4, 5, 2, 1, 2, 3]") println("The alternating sum is: ${alternatingSum(3, 4, 5, 2, 1, 2, 3)}") } fun alternatingSum(vararg numbers: Int): Int { var theSum = 0 for ( elementValue in numbers ) { theSum *= -1 theSum += elementValue } return theSum }
@LtMewS
@LtMewS Жыл бұрын
I Used boolean flag variable for this problem fun alternateSum(vararg numbers: Int): Int{ var flag = true var sum = 0 for(i in numbers){ if (flag){ sum+=i flag=false } else{ sum-=i flag=true } } return sum }
@callmejacrispy
@callmejacrispy 6 ай бұрын
fun main() { val sum = altSum(3, 4, 5, 2, 1, 2, 3) println(sum) // Output should be 0 } fun altSum(vararg numbers: Int): Int { var sum = 0 for (i in numbers.indices) { if (i % 2 == 0) { sum += numbers[i] // Add numbers at even index positions } else { sum -= numbers[i] // Subtract numbers at odd index positions } } return sum }
@feanor2718
@feanor2718 4 жыл бұрын
I'm Confused:Take a look at the junk i've written: fun alternatingSum(vararg number: Int): Int { var value: Int = number[0] for (i in number.indices) { if (i != number.size - 1) { when { i % 2 == 0 -> value -= number[i + 1] else -> value += number[i + 1] } } } return value }
@lifesurprise9313
@lifesurprise9313 2 жыл бұрын
lists and arrays is a very difficult subject, they make ne so confused
@bogite8734
@bogite8734 Жыл бұрын
fun main() { val sum = alternatingSum(3, 4, 5, 2, 1, 2, 3) println("The alternating sum is $sum") } fun alternatingSum(vararg numbers:Int):Int { var current_symbol = "+" var result = 0 for(n in 0..numbers.size-1) { val number = numbers[n] if(current_symbol == "+") { result += number current_symbol = "-" } else if (current_symbol == "-") { result -= number current_symbol = "+" } } return result }
@mkc0321
@mkc0321 4 жыл бұрын
amazing
@MrJonas04
@MrJonas04 5 жыл бұрын
Who watches this in 2020?
@SerhiiOr
@SerhiiOr 4 жыл бұрын
I
@MrJonas04
@MrJonas04 4 жыл бұрын
@@SerhiiOr ok
@jessajanson1344
@jessajanson1344 3 жыл бұрын
2021 !!
@LtMewS
@LtMewS Жыл бұрын
2023
@younesscoder
@younesscoder Жыл бұрын
2023 :^)
@DeathlabelCSS
@DeathlabelCSS 6 ай бұрын
fun main() { val sum = alternatingSum(3,4,5,2,1,2,3) println("The alternating sum is $sum") } fun alternatingSum(vararg list: Int): Int{ var sum = list[0] for (i in 1..list.size - 1){ if(i % 2 == 0) // for those that dont know this mathmatical equation its called MOD and essentially it says i divided by 2, but returns the remainer so i MOD 2 would = 0 for even numbers sum += list[i] else sum -= list[i] } return sum }
@roymustang7031
@roymustang7031 3 ай бұрын
//my solution for the homework fun main(){ var myNumbers=newp(3,4,5,2,1,2,3) println("The sum is $myNumbers") } fun newp(vararg numbers: Int): Int{ var result=numbers[0] for(i in 1..numbers.size-1){ if(i%2==0){ result+=numbers[i] } else{ result-=numbers[i] } } return result }
@Kids____Zone
@Kids____Zone 2 жыл бұрын
who watches in 2022?
@ekafrancium
@ekafrancium Жыл бұрын
fun main() { val list = listOf(1,2,3,4,5,6,7) println(list) println("Search for a number in the $list: ") val z = readlnOrNull()?.toInt() if(z!=null) { println("The index of the $z is: ${indexof(list, z)}") } } fun indexof(list: List, value: Int): Int{ for (i in 0 ..list.size){ if(list[i]==value){ return i } } return -1 }
Kotlin Newbie to Pro - EXTENSION FUNCTIONS - Part 19
10:58
Philipp Lackner
Рет қаралды 23 М.
Kotlin Newbie to Pro - CLASSES - Part 20
18:38
Philipp Lackner
Рет қаралды 23 М.
GIANT Gummy Worm #shorts
0:42
Mr DegrEE
Рет қаралды 152 МЛН
SLIDE #shortssprintbrasil
0:31
Natan por Aí
Рет қаралды 49 МЛН
OCCUPIED #shortssprintbrasil
0:37
Natan por Aí
Рет қаралды 131 МЛН
Solving one of PostgreSQL's biggest weaknesses.
17:12
Dreams of Code
Рет қаралды 225 М.
The symptoms of bad code - Robert C. Martin (Uncle Bob)
5:42
Dev Tools Made Simple
Рет қаралды 33 М.
Jobs, Waiting, Cancelation - Kotlin Coroutines
9:32
Philipp Lackner
Рет қаралды 62 М.
Python default arguments are awesome! 👍
6:07
Bro Code
Рет қаралды 40 М.
Kotlin Newbie To Pro - FUNCTIONS AND PARAMETERS - Part 16
13:12
Philipp Lackner
Рет қаралды 19 М.
ASMR Programming - Spinning Cube - No Talking
20:45
Servet Gulnaroglu
Рет қаралды 4,4 МЛН
STOP Using Classes In JavaScript | Prime Reacts
14:02
ThePrimeTime
Рет қаралды 262 М.
You can compile Kotlin to Javascript
5:03
Tom Delalande
Рет қаралды 6 М.
Kotlin Newbie to Pro - LAMBDA FUNCTIONS - Part 28
16:29
Philipp Lackner
Рет қаралды 35 М.
GIANT Gummy Worm #shorts
0:42
Mr DegrEE
Рет қаралды 152 МЛН