Skip to content
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

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions Test1/src/Problem1.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
fun main() {
Copy link
Member

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.

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
Copy link
Member

Choose a reason for hiding this comment

The 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)
}
22 changes: 22 additions & 0 deletions Test1/src/Problem2.kt
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
Copy link
Member

Choose a reason for hiding this comment

The 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)
}
56 changes: 56 additions & 0 deletions Test1/src/Problem3.kt
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])) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

contains most likely uses a loop in it's implementation, so you are still using a loop inside a loop, there is a clear way to solve this problem.

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")
}*/