dqtu14636 2018-07-02 14:54
浏览 26

如何使用go识别给定号码的匹配模式?

I'm trying to identify pattern matches for a given telephone number range to use in a Cisco Communications Manager platform.

Essentially, an 'X' matches the numbers 0-9 in a telephone number, and you can specify a range of digits using the [x-y] notation.

Given a telephone number range of 02072221000-02072221149 consisting of 150 numbers, this would create and should output two patterns: 020722210XX and 020722211[0-4]X

Obviously I'd like it to work on any range provided. I just can't seem to get my head around how to generate those patterns given the number ranges.

Any thoughts would be greatly appreciated. Many thanks.

  • 写回答

1条回答 默认 最新

  • dougang6178 2018-07-02 20:09
    关注

    I believe I found a decent algorithm which should handle this for you. I apologize ahead of time if any of the explanation isn't detailed enough, but a lot of this came to intuition which can be hard to explain.

    I started with more simplified cases, figuring out a method for how to get the fewest number of patterns from a comparison. For my examples I'll be comparing 211234 to 245245. After a bit of thinking I worked out that you need to take the range of numbers from the smaller number up to 9 and handle the special case for the lowest digit in the smaller number. To explain in a bit more detail, in the number 211234 the ideal is to represent the last digit as an X but we can only do that for cases where the digit may be [0-9] the only case in this example where we can't use [0-9] is when our tens digit is 3 because we have a lower limit of 4. This logic then propagates up the rest of the number as we head toward the most significant digit. So for the tens digit in the next case we have a lower bound based on the previous example of 4 because we're handling the case when we allow a 3 specially. So for our tens range we end up with a 4-9 because the next digit over does not restrict our range.

    In fact we won't be restricted until the most significant digit which is bounded by the numbers in the range between the numbers we're comparing. After working a few problems out by hand I noticed a bit of a pattern of the pyramid of Xs in the cases where the numbers digits were significantly apart:

    compare: 211234
    to:      245245
    
    
    21123[4-9]
    2112[4-9]X
    211[3-9]XX
    21[2-9]XXX
    2[2-3]XXXX
    24[0-4]XXX
    245[0-1]XX
    2452[0-3]X
    24514[0-5]
    

    This was my first hint as to how to handle it. Starting from the least significant moving up, taking advantage of the symmetry, but handling the case where we hit the "top of the pyramid". This example is easy though, there are many corner cases which will cause issues. For the sake of brevity I'm not going to go into detail for each but I'll give a short explanation for each:

    What do you do when the 2 compared digits has one number between them, such as between 4 and 6?

    In this case simply use the single digit in place of a range.

    What do you do when the 2 compared digits have no number between them, such as between 4 and 5?

    In this case throw away the row in which you'd handle the numbers between the digits as all cases will be handled explicitly.

    What do you do when the minimum number in the range is 8?

    In this case when we add 1 to the number to get a lower bound for the range we get a 9, which means we can simply substitute in a 9 rather than a range of [9-9]

    What do you do when the minimum number in the range is 9?

    In this case we simply don't bother handling that number as when handling the next digit up it should be covered by its use of X

    I'm sure I'm missing some corner cases which I handle in the code which I simply didn't think to put in this list. I'm willing to clarify any part of the code if you just leave a comment asking.

    Below is my stab at it in Go. It could probably be a bit more DRY but this is what I came up with after fiddling for a bit. I'm also pretty new to Go so please notify me of any spirit fouls in the comments and I'll correct them.

    I don't guarantee this will handle every case, but it handled every case I threw at it. It's up to you to turn it into a script which takes in 2 strings ;)

    Edit: I just realized via the example in the question (which for some reason I never ran) that this doesn't always condense the provided range in to the smallest number of outputs, but it should always give patterns which cover every case. Despite this drawback I think it's a good step in the right direction for you to work on top of. I'll update the answer if I find the time to get it to condense cases where the previous range is 1-9 and the special case is 0. The best means for which might end up being after the initial generation condensing these cases "manually".

    package main
    
    import (
        "strconv"
        "fmt"
    )
    
    
    func getStringFromMinAndMax(min int, max int) (string, bool){
        minstr := strconv.Itoa(min)
        maxstr := strconv.Itoa(max)
        if max == min {
            return minstr, false
        }
        if max < min{
            return minstr, false
        }
        return "["+minstr+"-"+maxstr+"]", true
    }
    func main(){
        str1 := "211234"
        str2 := "245245"
    
        diffLength := 0
        for i := 0; i < len(str1); i++{
            diffLength = i+1
            number1, _ := strconv.Atoi(str1[:len(str1)-i-1])
            number2, _ := strconv.Atoi(str2[:len(str2)-i-1])
            if number1 == number2 {
                break
            }
    
        }
    
        elems := (diffLength * 2)-1
        output := make([]*[]string, elems+1)
        for i := 0; i < elems; i++ {
            newSlice := make([]string, diffLength)
            output[i] = &newSlice
        }
    
        for digit := 0; digit < diffLength; digit++ {
            for j := 0; j < diffLength; j++ {
                if j == digit {
                    if output[j] != nil {
                        min, _ := strconv.Atoi(string(str1[len(str1)-(digit+1)]))
                        max := 9
                        if digit == diffLength-1 {
                            max, _ = strconv.Atoi(string(str2[len(str1)-(digit+1)]))
                            max = max - 1
                        }
                        if digit != 0{
                            min = min+1
                        }
    
                        if min < 10 {
                            maxchar := strconv.Itoa(max)[0]
                            minchar := strconv.Itoa(min)[0]
                            newVal, safe := getStringFromMinAndMax(min, max)
                            if digit == diffLength-1 && !safe && (str1[len(str1)-(digit+1)] == maxchar || str2[len(str2)-(digit+1)] == minchar) {
                                output[j] = nil
                            } else {
                                (*output[j])[diffLength-digit-1] = newVal
                            }
                        } else {
                            output[j] = nil
                        }
                    }
                    if j != diffLength-1 && output[elems-1-j] != nil {
                        min := 0
                        max, _ := strconv.Atoi(string(str2[len(str1)-(digit+1)]))
                        if digit != 0{
                            max = max-1
                        }
                        if max >= 0{
                            newVal, _ := getStringFromMinAndMax(min, max)
                            (*output[elems-1-j])[diffLength-digit-1] = newVal
                        } else {
                            output[elems-1-j] = nil
                        }
                    }
                } else {
                    if j > digit {
                        if output[j] != nil {
                            (*output[j])[diffLength-digit-1] = "X"
                        }
                        if j != diffLength-1 && output[elems-1-j] != nil {
                            (*output[elems-1-j])[diffLength-digit-1] = "X"
                        }
                    } else {
                        if output[j] != nil {
                            (*output[j])[diffLength-digit-1] = string(str1[len(str1)-digit-1])
                        }
                        if j != diffLength-1 && output[elems-1-j] != nil {
                            (*output[elems-1-j])[diffLength-digit-1] = string(str2[len(str2)-digit-1])
                        }
                    }
                }
            }
        }
    
    
        for _, list := range output {
            if list != nil{
                if len(str1) != diffLength{
                    fmt.Printf(str1[:len(str1)-diffLength])
                }
                for _, element := range *list {
                    fmt.Printf(element)
                }
                fmt.Printf("
    ")
            }
        }
    }
    

    Footnotes:

    • diffLength is the number of characters on the end of the strings which differ, I couldn't think of a better way to get this number than what's in the script...
    • Me setting an output to nil is me saying, "This one will be handled explicitly, so throw it away"
    • j is a variable for which output I'm setting... But this also gets mirrored to the bottom, so I couldn't think of a concise name to give it thus I left it j.
    • digit is tracking which digit from the right we are modifying
    评论

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题