This question already has an answer here:
Karate - Select a random element from json
(1 answer)
Closed 2 years ago.
How can I define a variable as a random match?
item[]
0{}
1{
_id:item
}
2{}
3{}
I want to define a match not with item[1]._id but with item[random]._id.
Could you please help me with that?
Just a warning that trying to add such dynamic behavior to your tests makes them less maintainable in the long term.
Here is an example that works:
* def size = response.items.length
* def index = Math.floor(Math.random() * size)
* def item = response.items[index]
Related
This question already has answers here:
how to swap numbers in kotlin using function?
(2 answers)
Val cannot be reassigned a compile time error for a local variable in fun in Kotlin
(3 answers)
Closed 5 months ago.
I am a beginner, learning the basics of Kotlin. We were asked to swap the values of two variables (see the photo below). My question is, why is simply swapping the values of the variable not the expected solution as written in the image? It had to use a third variable.
Example:
Var x = 1
Var y = 2
println(x) // prints 1
println(y) // prints 2
x = 2
y = 1
println(x)
println(y)
Also, in the photo, was the Val tmp reassigned?
This will swap two integer:
x=x+y
y=x-y
x=x-y
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 3 years ago.
Improve this question
how can i make a calculation to say the sum of certain 4 numbers to give a certain result like 280.
all possible combinations of numbers from 1 to 80. display all the combinations of 4 numbers that have the sum I set in the textbox. I should find out the easiest way to do this, and I would be grateful if you could help me. there should be no repetitions in a instance, like 60 60, 80 80. thus eliminating the unimportant variants, and the important ones will remain.
Textbox1.Text
68,69,70,72 = 280
67,69,70,74 = 280
66,69,70,75 = 280
65,69,70,76 = 280
64,69,70,77 = 280
and so on...
I would need a model, how could I conceive such an algorithm? Thank you very much.
Get all combinations which declares a sum from a certain number
There may be a very large number of such numbers. Therefore I'm going to first get a concise data structure that describes it, then run code that executes on every one.
Since I don't have vb.net I will do it in Python. Hopefully the translation is not too hard. I have used closures and dictionaries. I did not use the natural Pythonic iterators.
#! /usr/bin/env python3
def find_combs_data (count, target, upper):
# First we create a cache of known answers.
cached = {}
def find_combs (count_left, target_left, lower):
cache_key = (count_left, target_left, lower)
# Only do the calculation when needed.
if cache_key not in cached:
# Base case and sanity check.
if count_left == 0:
if target_left == 0:
return []
else:
return None
elif upper * count_left < target_left:
return None
elif target_left < lower * count_left:
return None
answer = {}
for i in range(lower, upper + 1):
result = find_combs(count_left - 1, target_left - i, i + 1)
if result is not None:
answer[i] = result
if len(answer):
cached[cache_key] = answer
else:
cached[cache_key] = None
return cached[cache_key]
final = find_combs(count, target, 1)
return final
def execute_on_combs (fn, count, target, upper):
passed = []
def execute_on_data (d):
if d == []:
fn(passed)
else:
for i, d_inner in d.iteritems():
passed.append(i)
execute_on_data(d_inner)
passed.pop()
execute_on_data(find_combs_data(count, target, upper))
def show (x):
print(x)
execute_on_combs(show, 4, 270, 80)
This question already has answers here:
Reading console input in Kotlin
(8 answers)
Closed 5 years ago.
Note: Please do not downvote question as I tried searching but did not find anything. The question has already been marked duplicate.
How to read primitive datatype values in Kotlin?
We can use Scanner object of java but I want to implement using readLine function of kotlin.
How do I scan numbers e.g num1 and num2 and perform some operation like sum ?
How do it convert following code to koltin without using scanner?
val sc = Scanner(System.in)
val num1 = sc.nextInt()
val num2 = sc.nextInt()
val sum = sum(num1, num2)
Just do something like:
fun main(vararg args: String) {
val (a, b) = readLine()!!.split(' ')
println(a.toInt() + b.toInt())
}
I have a array of type ILArray thats comes as an output from FFT function.
I want to further perform some math operations on the real and imaginary parts.
For example:
complexArray.realPart * 2 + complexArray.imaginaryPart * 4 ???
You have found the solution already. I'll put the answer here for sake of completeness.
Using ILMath.real() and ILMath.imag() gives access to the real and imaginary part of ILArray. If you are operating on the elements, using the properties .real and .imag of ILNumerics.fcomplex might be another option:
// create test array of complex elements
ILArray<fcomplex> C = ccomplex(ones<float>(1,10), -ones<float>(1,10));
// using real(), imag()
ILArray<float> a1 = 2 * real(C) + 4 * imag(C);
// using direct access to real / imag part of complex elements
foreach (var c in C) {
float a2 = c.real * 2 + 4 * c.imag;
// ...
}
This question already has answers here:
Closed 11 years ago.
Possible Duplicates:
How to concatenate a number to a variable name in MATLAB?
MATLAB: How can I use a variables value in another variables name?
I want to name a variable using values of other variables given in a function.
So, if I have values for an x1,x2 I can make the new variable's name as:
x_(x1's value)_(x2's value) as a name.
I've checked out the eval, num2str, strcat functions, but as of yet I can't make it so that I have a variable with the name above which I can assign a value to.
Any help would be greatly appreciated.
Take a look at the following FAQ:
How can I create variables A1, A2,...,A10 in a loop?
It answers the "how" part of your question and recommends a better approach using arrays.
As Jonas suggests, if x1 and x2 are numbers this works:
x1 = 3;
x2 = 4;
newValue = 25;
eval(sprintf('x_%i_%i = newValue;',x1,x2));
If x1 and x2 are strings, this becomes:
x1 = 'foo';
x2 = 'bar';
newValue = 25;
eval(sprintf('x_%s_%s = newValue;',x1,x2));
or more simply (using concatenation instead of SPRINTF):
x1 = 'foo';
x2 = 'bar';
newValue = 25;
eval(['x_' x1 '_' x2 ' = newValue']);
I don't know what you're trying to accomplish, but this probably isn't the best way to go about it. EVAL should always be avoided. Creating variables in the using EVAL is (a.k.a. "poofing") is doubly bad.
If you're trying to associate parameters with values, structures are a much better solution:
x1 = 'foo';
x2 = 'bar';
newValue = 25;
x.([x1 '_' x2]) = newValue;
Assuming you have a really good reason why you'd want to do that (and assuming x1 and x2 have integer values), you can do this by combining EVAL and SPRINTF.
x1 = 3;
x2 = 4;
newValue = 25;
eval(sprintf('x_%i_%i = newValue;',x1,x2));
If x1 and x2 are floats, it'll be trickier since a variable name cannot have dots in it, though it would still be possible as long as you replace the dots with something else.
However, I really have to ask: Are you sure that you want to do that? Because at the moment I cannot imagine an application where would want to create variable names you don't know beforehand, which in turn makes it very hard to write an efficient program.
EDIT
There are many useful ways to store your data in arrays. If you really don't want that, you may be interested in accessing data via key/value pairs in a MAP, a feature which is available in more recent versions of Matlab. Thus, your key would become sprintf('%i_%i',x1,x2), and the corresponding value would be whatever it is you want to store.
You can also use dynamic field references. Loren at the Mathworks gives a writeup here:
Mathworks: use-dynamic-field-references