Retrieve the count of value from the global variable and select any random from the value - katalon-studio

I have defined a list variable in the global variable section in Katalon studio, I want to retrieve the count of the list item and select any random value from it .
I have around 25 values,
.

Please try the below code:
def patientName = []
patientName = GlobalVariable.patientList
int length = patientName.size()
println "---->"+ length
int min = 0;
int max = length-1;
int random_int = (int)Math.floor(Math.random()*(max-min+1)+min);
def patientRetrieve = patientName[random_int]
println "---->"+ patientRetrieve

Related

Flutter int value if first digit is not zero I want to get first value

Flutter int value if first digit is not zero I want to get first value
for example
int x=123
result =1;
another example
int y=234;
result=2;
if the first value is zero I want to get the second value
for example
int a=023;
result=2;
another example
int b=098;
result=9;
how can i do this using dart?
There surely is a more mathematical way, but one way to do this is to convert the int to a String, get the first character and parse it back to int :
var numbers = [
1234,
567,
89,
];
for(var number in numbers) {
var firstNumber = int.parse(number.toString()[0]);
print(firstNumber);
}
Output :
1
5
8
This will give you the first non-zero digit. Assuming you get your number as a String. Otherwise it is pointless because the int value = 0123 is the same as int value = 123.
String yourNumber = "0123";
final firstNonZero = int.tryParse(yourNumber.split('').firstWhere((element) => element != '0', orElse: () => ''));
print(firstNonZero);

How to convert / round a double value to int or String in flutter dart

here is a double value
final dblValue = 25.5;
and I want this number as this int or string.
I tried the replaceAll function to remove the value after the ., but that didn't work too, I just need the number only without. value. how do I do that in dart
You can use the .floor() method which goes to the int value without the decimal points. For a formal definition:
The greatest integer no greater than this number.
final d = 25.9;
final d2 = 25.1;
int i = d.floor(); // i = 25
int i2 = d2.floor(); // i2 = 25
If you then want it as a string you can use the .toString() method on the int you just did.
To convert it to a string use
dblValue.toString();
To round it use
dblValue.round();
Or
dblValue.toInt();
And to floor the value use
dblValue.floor();
You'll need to use the .floor() method to round up.
final dblValue = 25.5;
int value = d.floor(); // value = 25

Updating object value after applying Filter in Scala

I am having List of List's and calling it BAT in my code. Each BAT has 2 attribute. First one is Position and second is fitness. For Every List in BAT , i am computing its fitness using Sphere function. Based on fitness i have applied Filter which filter only those list whom fitness is less than a object called GF. This return me BAT.
My code is
var GlobalBest_Fitness = Double.PositiveInfinit
var BAT = List.fill(N)(new BAT1(d, MinVal, MaxVal))
BAT.map { x =>
x.fitness = sphere(x.position)
}
BAT.filter(_.Fitness < GF).map { x =>
GF = x.Fitness
}
def sphere(list: List[Double]): Double = {
list.foldLeft(0.0)((x, xs) => x + xs * xs)
}
class BAT1 ( dim:Int , min:Double , max:Double) {
val random = new Random()
var position : List[Double] = List.fill(dim)(random.nextDouble() * (max-min)+min )
var fitness :Double = math.random
}
This code set GF Fitness of last member of BAT but i want to set value of Object GF to Fitness of List with Lowest Fitness.
Here is some output to explain question. BAT with 5 Lists,
(List(-67.33460898977961, -71.09215709663737, 55.89607430834903, -43.23771807116002),14581.91575554507)
(List(90.12684307743376, 43.946793301728036, -93.06789837138616, -76.86083905559525),24623.390772205956)
(List(12.619843833260006, -86.17961848282789, 48.99208107528267, 24.69991428409682),10596.496873950442)
(List(96.24721330545535, 54.598176031247306, -92.20930457845513, -42.450241098519385),22549.06571516962)
(List(71.10095207554104, 74.02738064902607, 93.76767384566747, 40.917896190085656),21002.04935885428)
Output ==>> GF = 21002.04935885428
This is seting value of GF to last List fitness, it should instead set it to Lowest value that is 10596.496873950442 that is fitness of third List.
This List could be very large and have to iterate over it millions of time. I want to find optimal solution.
According to what I understood from the question, you want a minimum fitness value from a List of BAT1 objects ( List[BAT1] ).
Problems noticed:
No need to set fitness later by mapping, .map is used for
transformation of a list/collection/monad (e.g., you want to convert a
List[A] to List[B]).
So, your BAT1 class should look like:
class BAT1(dim:Int, min:Double, max:Double) {
val random = new Random()
var position: List[Double] = List.fill(dim)(random.nextDouble() * (max-min)+min )
val fitness: Double = sphere(position) //no need of var and can be directly computer here only
}
To get the lowest fitness, you don't need to filter
val minFitness = BAT.map(_.fitness).min
/*
we will first convert the list BAT which has type List[BAT1] to a
List[Double] containing fitness of each BAT1 object in the list
we will then get the minimum value in the List[Double] by .min method
List[BAT1] -> List[Double] -> Double
*/
Another way:
val minFitness = BAT.minBy(_.fitness).fitness //please read about minBy, maxBy in Scala documentations.

Merging to dataset together according to key

I have two datasets stored in a cell array and a double array, respectively. The design of the two arrays is:
Array 1 (name: res) (double) is composed of two columns; a unique id column and a data column.
Array 2 (name: config) (cell array) contains 3 column cells, each with a string inside. The last cell in the array contains a id double integer matching the id's in Array 1. The double integer in the cell array is converted to a double when necessary.
I want to merge the two datasets in order to have the 3 cells in the cell array AND the result column in Array 1 in one common cell array. How do I do this?
I have the following code. The code does not return the correct order of the results.
function resMat = buildResultMatrix(res, config)
resMat = {};
count = 1;
count_max = size(res,1)/130;
for i = 1 : size(res,1)
for j = 1 : size(res,1)
if isequal(res(i),str2double(config{j,3}))
if i == 1
resMat(end+1,:) = {config{j,:} res(j,2:end)};
else
if count == 1
resMat(end+1,:) = {config{j,:} res(j,2:end)};
elseif count == count_max
resMat(end+1,:) = {config{j,:} res(j,2:end)};
else
resMat(end+1,:) = {config{j,:} res(j,2:end)};
end
count = count + 1;
end
end
end
count = 1;
end
end
First convert the id in config to numbers:
config(:,3) = num2cell(str2double(config(:,3)));
Then run this:
res = sortrows(res,1);
config(:,4) = num2cell(res(cell2mat(config(:,3)),2))
this will put the data from res in the 4th column in config in the row with the same id.

How to convert string to integer in JES

I am trying to do an assignment in JES a student jython program. I need to convert our student number taken as a string input variable to pass through our function i.e.
def assignment(stringID) and convert it into integers. The exact instructions are:
Step 1
Define an array called id which will store your 7 digit number as integers (the numbers you set in the array does not matter, it will be over written with your student number in the next step).
Step 2 Your student number has been passed in to your function as a String. You must separate the digits and assign them to your array id. This can do this manually line by line or using a loop. You will need to type cast each character from stringID to an integer before storing it in id.
I have tried so many different ways using the int and float functions but I am really stuck.
Thanks in advance!
>>> a = "545.2222"
>>> float(a)
545.22220000000004
>>> int(float(a))
545
I had to do some jython scripting for a websphere server. It must be a really old version of python it didn't have the ** operator or the len() function. I had to use an exception to find the end of a string.
Anyways I hope this saves someone else some time
def pow(x, y):
total = 1;
if (y > 0):
rng = y
else:
rng = -1 * y
print ("range", rng)
for itt in range (rng):
total *= x
if (y < 0):
total = 1.0 / float(total)
return total
#This will return an int if the percision restricts it from parsing decimal places
def parseNum(string, percision):
decIndex = string.index(".")
total = 0
print("decIndex: ", decIndex)
index = 0
string = string[0:decIndex] + string[decIndex + 1:]
try:
while string[index]:
if (ord(string[index]) >= ord("0") and ord(string[index]) <= ord("9")):
times = pow(10, decIndex - index - 1)
val = ord(string[index]) - ord("0")
print(times, " X ", val)
if (times < percision):
break
total += times * val
index += 1
except:
print "broke out"
return total
Warning! - make sure the string is a number. The function will not fail but you will get strange and almost assuredly, useless output.