awk function to compute median of an array? [closed] - awk

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
}
}

Related

Random number in time period [closed]

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)
}

Why can you not specify var/val loops in Kotlin? [closed]

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.

Calculus with Constants [closed]

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.

Objective-C to Swift Equivilant [closed]

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 8 years ago.
Improve this question
I'm trying to figure out how to write this block method in swift. I can't seem to get the closure syntax right:
[self.colorPickerView setDidChangeColorBlock:^(UIColor *color){
self.selectedColor.backgroundColor = self.colorPickerView.color;
}];
Thanks in advance!
What i tried:
self.colorPickerView.didChangeColorBlock({
(color: UIColor) in self.selectedColorView.backgroundColor = self.colorPickerView.color
})
Final Solution:
self.colorPickerView.didChangeColorBlock = {
(color: UIColor!) in
self.selectedColorView.backgroundColor = color
}
You probably need to use
self.colorPickerView.didChangeColorBlock = { ...
instead of
self.colorPickerView.didChangeColorBlock({ ...
Since didChangeColorBlock is a property, not a method.

Why does this code work in Objective-C? I'm getting BAD_ACCESS [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 9 years ago.
Improve this question
int RandomSource_next(int bits, double* seed) {
*seed = (((long long) *seed * 0x5DEECE66DLL) + 0xBLL) & ((1LL << 48) - 1);
return (int)((signed long long) *seed >> (48 - bits));
}
I think it got something to do with the address.
Most probably you are passing incorrect address as seed. Maybe you're passing not an address but a value?
The following should work
double seed = 0;
RandomSource_next(48, &seed);
The following should crash
RandomSource_next(48, 0);