Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
How to solve a problem with Kotlin when I want to generate random numbers in time period (eg. 1 sec) and than than that numbers collect somehow and make Sum of that numbers.
Thanks
The routines of what you wanted are very simple. First, run a loop of how many random numbers that you want. Run a for loop that many times. Each time you generate a random number, add the value of that random number to a variable to keep track of the sum. Add that random number to a list. Then let the program go to sleep for a specific amount of time in milliseconds. The program below is a short example.
Note that, if the random number generated is a negative number, it will be flip back to its positive value. That will not work for integer.min value. However, this is just an example.
import java.util.Random
import kotlinx.coroutines.*
fun main() {
val howManyNumber = 5
val delayTime = 1000L // ms
val randomNumbers = mutableListOf<Int>()
var sum = 0
for (i in 1..howManyNumber){
var randN = Random().nextInt()
randN = if (randN > 0) randN else randN * -1
sum += randN
randomNumbers.add(randN)
Thread.sleep(delayTime)
}
println(sum)
println(randomNumbers)
}
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 10 months ago.
Improve this question
Explain time complexity of the code given in the picture
So basically you have two loops, the top one is quite simple, but the second one is a tad bit more tricky.
for (i = n/2; i < n; i) // ~n/2 so O(n)
for(j = 0; j < n; j = 2 * j) // How many times does this run for n
So j doubles after every iteration until we reach n, so if we double n, j will only do one extra iteration! Alternatively, you could say that log_2 n is how many times we can double j to reach n.
So the time complexity is O(n log n).
This question already has answers here:
Why does floating-point arithmetic not give exact results when adding decimal fractions?
(31 answers)
Closed 11 months ago.
I'm trying to get the total amount of double values but it returns unexpected results. Check the following screenshot:
Link for the code
Code:
import java.math.BigDecimal
/**
* You can edit, run, and share this code.
* play.kotlinlang.org
*/
fun main() {
val items = listOf(MyItem(BigDecimal(3.6)), MyItem(BigDecimal(2.0)), MyItem(BigDecimal(4.3)), MyItem(BigDecimal(0.1)))
println(items.sumOf { it.amount })
}
data class MyItem(val amount: BigDecimal)
The expected result is: 10.0
The actual result is: 9.9999999999999999167332731531132594682276248931884765625
In most cases it is better to provide numbers to BigDecimal as strings, not floats:
val items = listOf(MyItem(BigDecimal("3.6")), MyItem(BigDecimal("2.0")), MyItem(BigDecimal("4.3")), MyItem(BigDecimal("0.1")))
It solves your problem.
Few words of explanation: floating-point numbers are by nature imprecise for storing decimal fractions. They only provide a close result, but not the one we would expect.
BigDecimal tries to fix this problem. It makes some tricks to represent the number as we would expect. But to do this it needs to understand how do we want to represent numbers, what is our expected precision, etc. It can deduct this from numbers provided by strings, but not from floats/doubles.
The problem is that you instantiate your BigDecimals using Double values which leads to rounding issues.
You can instead write:
val items = listOf(MyItem(BigDecimal("3.6")), MyItem(BigDecimal("2.0")), MyItem(BigDecimal("4.3")), MyItem(BigDecimal("0.1")))
Then your result is 10.0.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
I want to have an awk function to compute the median of an array (may not be sorted).
median of column with awk
The above is a similar post but it is not the same as this one because the input on the above post is a sorted stream.
Could anybody show me the function to compute the median of an array?
I got the following solution. I am not sure if it would fail in a corner case. If it does, please let me know.
function median(x, n, d) {
n = asort(x, d, "#val_num_asc")
if(n == 0) return "NA"
if(n%2) {
return d[(n+1)/2]
} else {
return (d[n/2] + d[n/2+1])/2
}
}
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 6 years ago.
Improve this question
Why can't you specify a val or var type in for loop's in Kotlin. For example, I would like to be able to do
for (var i in 0...data.size - 1) {
for (j in 0..bytes.size - 1) {
bytes[j] = data[i++]//cant do i++ in current kotlin because "i" is val
}
//do stuff
}
But instead I have to do this
var i = 0
while (i < data.size) {
for (j in 0..bytes.size - 1) {
bytes[j] = data[i++]
}
//do stuff
}
Your example is slightly different from Java's typical for(int i=0;i<data.size;i++) example.
In the Kotlin version 'i' is actually an element of the range in which case i++ doesn't make sense. It just so happens that the range you have is a list of indexes.
The way you are using the Kotlin for loop is much closer to Java's foreach loop for(i : indexes).
I think that because Kotlin is a language that tries to make easy to respect most of the functional programming concepts, it prefers to prohibit that sort of behaviour. Also, a possible problem that your initial code could encount is an OutOfBoundException in the situation where the bytes array has more elements than the data array.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 8 years ago.
Improve this question
An electric current, I, in amps, is given by
I=cos(wt)+√(8)sin(wt),
where w≠0 is a constant. What are the maximum and minimum values of I?
I have tried finding the derivative, but after that, I do not know how to solve for 0 because of the constant w.
Well David,you can convert this function into one trigonometric function by multiplying and dividing it by
√(1^2 + 8) i.e, 3. So your function becomes like this
I = 3*(1/3 cos(wt) + √8/3 sin(wt))
= 3* sin(wt + atan(1/√8))
Now, you can easily say its maximum value is
I = 3 amp
and minimum value is
I = 0 amp.