Skip to content

Instantly share code, notes, and snippets.

@s0nerik
Created April 9, 2015 00:21
Show Gist options
  • Save s0nerik/9c52175dcd68ad8807b8 to your computer and use it in GitHub Desktop.
Save s0nerik/9c52175dcd68ad8807b8 to your computer and use it in GitHub Desktop.
def string1 = """\
001001
000101
000101
"""
def string2 = """\
10
01
01
"""
def string3 = """\
010
010
010
"""
def string4 = """\
10001000
00100100
00000100
"""
def includes(String s1, String s2) {
def linesS1 = s1.readLines()
def linesS2 = s2.readLines()
def s1Width = linesS1[0].length()
def s2Width = linesS2[0].length()
assert linesS1.size() == linesS2.size() && s1Width >= s2Width
def height = linesS1.size()
def s1Column = 0
def s2Column = 0
while (s1Column <= s1Width - s2Width) { // maxColumn is smaller than biggest string width
if (linesS1[0][s1Column] == linesS2[0][0]) { // Upper char of current s1 column equals first char in s2
def success = true
loop:
for (int row = 0; row < height; row++) { // Cycle through each row
for (int col = 0; col < s2Width; col++) { // Cycle through each column
if (linesS2[row][col] != linesS1[row][col + s1Column]) {
success = false
break loop
}
}
}
if (success) {
return true
}
}
s1Column++
}
return false
}
//string1.contains(string2) > true
//string1.contains(string3) > false
//string4.contains(string2) > true
//string4.contains(string3) > false
println includes(string1, string2)
println includes(string1, string3)
println includes(string4, string2)
println includes(string4, string3)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment