I am performing below task in jenkinsfile using groovy. where i have below variables
list C=[1.1,1.2,1.3]
String A=1.1
String B=1.3
if ("${A}" == "${B}") {
echo "No Action needed"
} else if ("${A}" != "${B}") {
then check in the variable list C and then iterate the list in sequential order.
}
Need Help in this condition. I am newbie in groovy.
When the A is != to B then it check the version from list C
Expected Output:-
1.1 == 1.3
then check in list C [1.1,1.2,1.3]
Iterate
1.2 ==1.3
print "still need iteration"
1.3 == 1.3
print " Matched"
I have Tried below code but its not doing iteration in for loop for If condition.
def C = ['1.1', '1.2', '1.3']
A=1.1
B=1.3
if ('${A}' == '${B}') {
println "equal"
}
else if ('${A}' != '${B}') {
println "not equal"
for (i = 0; i < C.size(); i++) {
println C[i]
if ('C[i]' == '${B}') {
println "equal"
}
}
}
there are several issues in your code
A,B are numeric and C is array of strings. to compare objects you need same type.
'C[i]' == '${B}' is incorrect because you are comparing literal string 'C[i]' to B converted to GString "1.3"
better to avoid this kind of comparison '${A}' == '${B}' - result could be tricky. better to use A == B
so, your code with changes
def C = ['1.1', '1.2', '1.3']
A='1.1'
B='1.3'
if (A == B) {
println "equal"
} else {
println "not equal"
for (i = 0; i < C.size(); i++) {
println C[i]
if (C[i] == B) {
println "equal"
}
}
}
result:
not equal
1.1
1.2
1.3
equal
Related
Could use some help understanding (or help with corrections), and either an explanation or link as to how you know so I can figure these out when I run into them again. None of the tutorials I found were terribly helpful. Here's the operation. The bracket denotes a time series (array):
a = b==1 ? c ? d : e > a[1] ? e : a[1] : c ? d : a[1]
Translated, would be:
a = if b==1 and c then d
else if e > a[1] then e
else a[1]
else if c then d
else a[1]
...right? If correct, I don't understand how an else can come between two else-ifs. Doesn't the chain stop evaluating after the first else? I thought else essentially means "otherwise."
I believe the execution would be equivalent to this instead of b == 1 and c:
function test() {
if (b==1) {
if (c) {
return d;
} else {
if (e > a[1]) {
return e;
} else {
return a[1];
}
}
} else {
if (c) {
return d;
} else {
return a[1];
}
}
}
a = test();
I had a coding challenge as one of the process for recruitment into a company. In that coding challenge, one of the question was to inverse an expression.
For Example,
Input : 14-3*2/5
Output : 5/2*3-14
I used stack to put each number say 14 or 3 and expressions and then popped it out again to form the output.
Input format is : num op num op num op num
So we need not worry about input being -2.
num can be between -10^16 to 10^16. I was dealing with strings completely, so even if the number exceeds the 10^16 limit, my algorithm wouldn't have any problem.
My algorithm passed 7 test cases and failed in 2 of them.
I couldn't figure it out what the corner case would be. I couldn't see the test cases as well. Any idea what that might be. I know there isn't enough information, but unfortunately I too don't have them.
// Complete the reverse function below.
static String reverse(String expression) {
expression = expression.trim();
if(expression == ""){
return "";
}
Stack<String> stack = new Stack<String>();
String num = "";
for(int i=0; i<expression.length(); i++){
char c = expression.charAt(i);
if(c==' '){
continue;
}
if(c == '+' || c == '-' || c == '*' || c == '/'){
if(num != "") {
stack.push(num);
}
num = "";
stack.push(Character.toString(c));
} else{
num += c;
}
}
if(num != "") {
stack.push(num);
}
String revExp = "";
while(! stack.empty()){
revExp = revExp + stack.pop();
}
return revExp;
}
I am new to groovy and trying to find the indexes of all sublists in a list.
I am trying to use something like Collections.indexOfSubList like in java but it gives exception saying it applies on Lists and not ArrayLists.
So I am trying to define my own function. I am finding all the indices of all the elements in the smaller list existing in the longer list and then subtracting the indices of the result array. If it comes to 1 then I am considering that index to a sublist.
I know that I have the logic a little twisted. Can somebody guide with a better and efficient way of doing this.
Below is my code:
List list1 = [1,2,3,4,5,6,1,2,3]
List list2 = [1,2]
index1 = list1.findIndexValues {
it == list2[0];
}
index2 = list1.findIndexValues {
it == list2[1];
}
println index1
println index2
result = []
for (int i = 0; i < index1.size(); i++) {
result.add(index2[i]-index1[i]);
}
println result
Edit: no longer uses Collections due to new issue re: Elastic Search.
The following code traverses along the source list, creating a sublist. It checks the sublist to see if it starts with the target list. See the asserts below (e.g. the indexes are 0-based):
def listStartsWithSubList = { source, target ->
def result = false
if (source.size() >= target.size()) {
result = true
target.eachWithIndex { item, index ->
result = result && (item == source[index])
}
}
result
}
def indexOfSubLists = { source, target ->
def results = []
source.eachWithIndex { item, index ->
def tmpList = source[index..source.size()-1]
if (listStartsWithSubList(tmpList, target)) {
results << index
}
}
results
}
assert [1] == indexOfSubLists([1,2,3], [2,3])
assert [2] == indexOfSubLists([1,2,3], [3])
assert [] == indexOfSubLists([1,2,3], [4])
assert [0,6] == indexOfSubLists([1,2,3,4,5,6,1,2,3], [1,2])
I know we cannot return from .each{} closure in groovy like .find{} closure. Still I am curious why the below code execute only first iteration of the .find{}.
def findlist = [1,2,3,4,5]
def eachlist = [7,6,5]
findlist.find
{
int findelem = it
println "findelem : " + findelem
eachlist.each
{
int eachelem = it
println "eachelem : " + eachelem
if(it == findelem)
{
return true
}
return false
}
}
It prints:
findelem : 1
eachelem : 7
eachelem : 6
eachelem : 5
Why find{} exits after first iteration?
PS: I understand this code might not have any practical significance, just curious about groovy behavior.
Because each returns unmodified collection is iterating on. The returned collection evaluates to true, hence find stops after first iteration.
Have a look at the code below:
assert [1, 2].each { println it } == [1,2]
assert [1,2].find { println it; [3, 4].each { e -> println e } }
You need to nest find instead of each.
Just to complete Opal's answer (each returns the collection so it evaluates to true if it is not empty), you can use a local variable inside the find closure to return the found value. Simplifying your code a bit:
assert 5 == findlist.find { findelem ->
println "findelem : " + findelem
boolean found
eachlist.each { eachelem ->
println "eachelem : " + eachelem
found = (eachelem == findelem)
}
found
}
However, there is a nicer and groovier way:
assert 5 == findlist.find { it in eachlist }
I want to find a way by which I can map "b m w" to "bmw" and "ali baba" to "alibaba" in both the following examples.
"b m w shops" and "bmw"
I need to determine whether I can write "b m w" as "bmw"
I thought of this approach:
remove spaces from the original string. This gives "bmwshops". And now find the Largest common substring in "bmwshop" and "bmw".
Second example:
"ali baba and 40 thieves" and "alibaba and 40 thieves"
The above approach does not work in this case.
Is there any standard algorithm that could be used?
It sounds like you're asking this question: "How do I determine if string A can be made equal to string B by removing (some) spaces?".
What you can do is iterate over both strings, advancing within both whenever they have the same character, otherwise advancing along the first when it has a space, and returning false otherwise. Like this:
static bool IsEqualToAfterRemovingSpacesFromOne(this string a, string b) {
return a.IsEqualToAfterRemovingSpacesFromFirst(b)
|| b.IsEqualToAfterRemovingSpacesFromFirst(a);
}
static bool IsEqualToAfterRemovingSpacesFromFirst(this string a, string b) {
var i = 0;
var j = 0;
while (i < a.Length && j < b.Length) {
if (a[i] == b[j]) {
i += 1
j += 1
} else if (a[i] == ' ') {
i += 1;
} else {
return false;
}
}
return i == a.Length && j == b.Length;
}
The above is just an ever-so-slightly modified string comparison. If you want to extend this to 'largest common substring', then take a largest common substring algorithm and do the same sort of thing: whenever you would have failed due to a space in the first string, just skip past it.
Did you look at Suffix Array - http://en.wikipedia.org/wiki/Suffix_array
or Here from Jon Bentley - Programming Pearl
Note : you have to write code to handle spaces.