Arrays Swift-搜索数组中的数字模式

Arrays Swift-搜索数组中的数字模式,arrays,swift,algorithm,numbers,pattern-matching,Arrays,Swift,Algorithm,Numbers,Pattern Matching,我正在用Swift编写一个算法,其中一部分需要检查给定的数组,并确定是否有任何2(或3,4-注释掉的)数字序列在重复。我的源代码是来自Java的代码块,据我所知,我试图将其转换为Swift-Playgrounds。它可以正常工作,但在运行代码后会抛出一个错误 “无法形成上限

我正在用Swift编写一个算法,其中一部分需要检查给定的数组,并确定是否有任何2(或3,4-注释掉的)数字序列在重复。我的源代码是来自Java的代码块,据我所知,我试图将其转换为Swift-Playgrounds。它可以正常工作,但在运行代码后会抛出一个错误

“无法形成上限<下限的范围”

在崩溃之前,输出是正确的,并提供模式的序列。请问如何防止这种情况

let array1 = [12, 4, 5, 7, 1, 2]
let array2 = [1, 2, 3, 1, 2, 3, 1, 2, 3]
let array3 = [1, 1, 1, 1, 1, 1 ]
let array4 = [1, 2, 4, 12, 13, 1, 8, 4, 12, 4 ]
let array5 = [17,39,78,324,43,33,234,99,34,555,39,78,324,43,45,92 ]

func hasPattern(c:Array<Int>) {
for i in 0..<c.count {
    var jj = i + 1

    let step2 = (c.count - i) 
    for j in jj..<step2 {

        if(c[j] == c[i]){

            // pattern of 4 numbers repeating
            /*if(c[j+1] == c[i+1] && c[j+2] == c[i+2] && c[j+3] == c[i+3]){
                print("\(c[i]), \(c[i+1]), \(c[i+2]), \(c[i+3])")                    
            }*/

            // pattern of 3 numbers repeating
            /*
            if(c[j+1] == c[i+1] && c[j+2] == c[i+2]){
             print("\(c[i]), \(c[i+1]), \(c[i+2])")
             }
             */

            // pattern of 2 numbers repeating
            if(c[j+1] == c[i+1]){
                print("\(c[i]), \(c[i+1])")
            }
         }
      }
   }
}
/*
print(hasPattern(c: array1))
print(hasPattern(c: array2))
print(hasPattern(c: array3))*/
//print(hasPattern(c: array4))

print(hasPattern(c: array5))
让数组1=[12,4,5,7,1,2]
设array2=[1,2,3,1,2,3,1,2,3]
设array3=[1,1,1,1,1]
设array4=[1,2,4,12,13,1,8,4,12,4]
设数组5=[17,39,78324,43,33234,99,34555,39,78324,43,45,92]
func hasPattern(c:数组){

对于0..中的i,您的问题在于内部循环。对于let数组,例如大小5,循环如下所示:

for i in 0..<5
    for j in i+1..<5-i

array
是您正在搜索的数组。

我仍然觉得您的问题不完全清楚。数组
[2,3,2,3,2]
是否有两个重复的
[2,3,2]
子序列,即使它们共享一个元素

如果要避免“无法形成范围…”错误,请使用
stride

// A more convenient way to check if two arrays are equal
func ==(lhs: [Int], rhs: [Int]) -> Bool {
    guard lhs.count == rhs.count else { return false }

    for (l, r) in zip(lhs, rhs) {
        if l != r { return false }
    }
    return true
}

// If there's no matching pattern, this function will return nil
// instead of an aempty array. Change it if you want to
func hasPattern(array: [Int], length: Int) -> [[Int]]? {
    guard array.count >= length * 2 else { return nil }

    var result = [[Int]]()
    for i in 0..<(array.count - length) {
        let subarray1 = array[i..<(i+length)]

        for j in stride(from: i+1, to: array.count - length, by: 1) {
            let subarray2 = array[j..<(j+length)]
            if subarray1 == subarray2 {
                result.append(Array(subarray1))
            }
        }
    }
    return result.isEmpty ? nil : result
}

if let patterns = hasPattern(array: array5, length: 3) {
    print(patterns) // [[39, 78, 324], [78, 324, 43]]
} else {
    print("No repeating pattern found")
}
//检查两个数组是否相等的更方便的方法
func==(左:[Int],右:[Int])->Bool{
guard lhs.count==rhs.count else{return false}
用于拉链(左、右)中的(左、右){
如果l!=r{return false}
}
返回真值
}
//如果没有匹配的模式,此函数将返回nil
//而不是aempty数组。如果需要,请更改它
func hasPattern(数组:[Int],长度:Int)->[[Int]]{
guard array.count>=长度*2 else{return nil}
变量结果=[[Int]]()

对于0中的i.。您的问题非常不清楚。您希望输出是什么?您的
hasPattern
函数没有返回任何内容。您的5个样本数组的预期结果是什么?您是对的,我应该更好地解释它。我在Swift Playgrounds中编写了此块,因此我能够使用print()在线查看输出。在这一行,我将添加一个数组并附加结果数。然后将返回该数组。因此,目标是扫描数组中重复数的模式,将该模式添加到数组中并返回它。因此,对于array4,它将返回数组[4,12]中的数。换句话说,您正在检查每个数组是否包含子数组(长度不超过4)哪一种情况会发生多次?您想要的是满足该条件的最长子阵列还是满足该条件的所有子阵列?是的,正确,并且需要所有子阵列。因此,对于阵列5,它将返回2个模式,2个数字39、78和78、324。如果我选择3个数字的模式,它将只返回一个模式39、78、324谢谢。B但是我认为这个块缺少一些东西。首先,我传递了一个数组来搜索你的函数,你把它指定为“元素”。你是说“数组”吗相反?如果是这样,元素数组从何而来以及如何更新?我认为缺少一行,在这行中,您将带有模式的数字附加到元素数组,因此计数也会更新。当我将括号中的元素更改为数组时,它总是返回true,即使它应该是false。另一件事是,我实际上需要pa我不需要继续使用我的版本,你的版本很有希望更干净,但是我如何使它正常工作呢?它被命名为
elements
,这是有原因的。在公共语言中,将函数命名为
hasPattern:of elements
hasPattern:of array
更有意义如果您不是在寻找数组模式,您是在寻找元素模式。这是一种设计选择,但不影响功能,因为
数组
对象仍作为参数传递。为什么要附加任何内容?元素数组是一个参数。如果要确定数组是否包含数组模式,请调用函数元素。函数返回一个布尔值,因为您的问题是“检查给定数组并确定…”很抱歉显得很愚蠢,但在这种情况下,“数组”是什么?它来自何处以及如何增加其计数?您发布了“数组”是我正在搜索的数组,但它应该是“元素”这就是我想让你困惑的地方。一个优秀而快速的解决方案。正是我想要达到的!
// A more convenient way to check if two arrays are equal
func ==(lhs: [Int], rhs: [Int]) -> Bool {
    guard lhs.count == rhs.count else { return false }

    for (l, r) in zip(lhs, rhs) {
        if l != r { return false }
    }
    return true
}

// If there's no matching pattern, this function will return nil
// instead of an aempty array. Change it if you want to
func hasPattern(array: [Int], length: Int) -> [[Int]]? {
    guard array.count >= length * 2 else { return nil }

    var result = [[Int]]()
    for i in 0..<(array.count - length) {
        let subarray1 = array[i..<(i+length)]

        for j in stride(from: i+1, to: array.count - length, by: 1) {
            let subarray2 = array[j..<(j+length)]
            if subarray1 == subarray2 {
                result.append(Array(subarray1))
            }
        }
    }
    return result.isEmpty ? nil : result
}

if let patterns = hasPattern(array: array5, length: 3) {
    print(patterns) // [[39, 78, 324], [78, 324, 43]]
} else {
    print("No repeating pattern found")
}