-
Notifications
You must be signed in to change notification settings - Fork 33
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
My solution to problems 1, 2 and 3 #50
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
fun main() { | ||
val N = 10 | ||
var sum = 0 | ||
val arr = IntArray(N) { i -> i } | ||
for(i in arr) { | ||
if (i % 2 == 0) { | ||
sum += arr[i] | ||
} | ||
} | ||
Comment on lines
+5
to
+9
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There a couple of usefull functions for arrays in kotlin scout the docs maybe you can find a way to make this more idiomatic. |
||
println("suma = " + sum) | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
fun main() { | ||
var max1 = 0 | ||
var max2 = 0 | ||
|
||
val arr = intArrayOf(17,11,10,19,15,23,1,9,100) | ||
for(valoare_i in arr) { | ||
if(valoare_i > max1) { | ||
max1 = valoare_i | ||
}else { | ||
max1 = max1 | ||
} | ||
} | ||
for(valoare_j in arr) { | ||
if (valoare_j > max2 && valoare_j != max1) { | ||
max2 = valoare_j | ||
} else { | ||
max2 = max2 | ||
} | ||
} | ||
Comment on lines
+6
to
+19
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you think of a way to do this in one loop? Also what about some edge cases? empty array, one item array .... |
||
println("max1 = " + max1) | ||
println("max2 = " + max2) | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
// Am incercat sa rezolv in mai multe metode problema deorece nu eram sigur daca prima solutie era cea ideala | ||
// Rezolvarea ideala cu Stringuri | ||
fun main() { | ||
var a = intArrayOf(1204, 66, 7423, 1, 4, 850, 4, 55, 10003, 55, 78364, 93, 403, 1789, 1, 3850, 778, 850) | ||
|
||
var distincte = "" | ||
var duplicate = "" | ||
|
||
for(i in a.indices) { | ||
if(i == 0) { | ||
distincte += (a[i].toString() + " ") | ||
} else if(distincte.contains(" " + a[i])) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
duplicate += (a[i].toString() + " ") | ||
} else { | ||
distincte += (a[i].toString() + " ") | ||
} | ||
} | ||
if (duplicate != "") { | ||
println("S-au gasit duplicatele : " + duplicate) | ||
}else { | ||
println("Nu s-au gasit duplicate") | ||
} | ||
} | ||
|
||
|
||
|
||
//Rezolvare cu functie implicita | ||
/*fun main() { | ||
var arr = intArrayOf(1204, 66, 7423, 1, 4, 850, 4, 55, 10003, 55, 78364, 93, 403, 1789, 1, 3850, 778, 850) | ||
for(i in arr) { | ||
if((arr.count{i == it}) > 1) { | ||
println("S-au gasit duplicate") | ||
return | ||
} | ||
} | ||
println("Nu s-au gasit duplicate") | ||
}*/ | ||
|
||
|
||
|
||
//Rezolvare cu for in for | ||
/*fun main() { | ||
var arr = intArrayOf(1204, 66, 7423, 1, 4, 850, 4, 55, 10003, 55, 78364, 93, 403, 1789, 1, 3850, 778, 850) | ||
|
||
for (i in arr.indices) { | ||
for (j in i + 1 until arr.size) { | ||
if (arr[i] == arr[j]) { | ||
println("S-au gasit duplicate") | ||
return | ||
} | ||
} | ||
} | ||
println("Nu s-au gasit duplicate") | ||
}*/ | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The implementation is straight forward
The idea was that you get an array of n elements as an input, you don't have to generate the input.