CtCI - Simplify Selection Ranges

You are given an array of numbers and must simplify the array by replacing ranges of three or more items with the shorthand N-M equivalent and returning the result as a string, sorted from lowest to highest.

Input: [1,2,5,6,7,9,12,55,56,57,58,60,61,62,64,65,70]
Output: 1,2,5-7,9,12,55-58,60-62,64,65,70

We assume the list is already sorted, otherwise we can apply Quicksort on it. Then we can start by checking each number in the list, then adding them into a different (smaller) list A if the number is part of a sequence no matter the size. Every number will be added to the second list if this one is empty. For the subsequent index K in the list with value M we will check if M == A[K-1]+1 this means the number is next in the series, for example, if A=[1] and K=2 then 2 == A[1-1]+1.

var total int = len(list)
var sub = []int{list[0]}

for k := 1; k < total; k++ {
    if list[k] == (list[k-1] + 1) {
        sub = append(sub, list[k])
        continue
    }
}

At the end of each iteration we will check if the size of the sublist is greater than two as the size for a series is three or more items. At this point we already have the series collected, we will proceed with the merge using the first and last items on each sublist.

var result []string
if len(sub) >= 3 {
    section := fmt.Sprintf("%d-%d", sub[0], sub[len(sub)-1])
    result = append(result, section)
}

As for the numbers that are not part of a valid series we can simply add them to the result as they are, in this case we are converting from integers to strings so it is easier to iterate to execute the string formatting.

for _, num := range sub {
    result = append(result, fmt.Sprintf("%d", num))
}

At this point we already have a solution, but the last numbers in the original list are not being processed because the iteration stops at one level before the end. We need to execute the same conditionals once again... or we can simply move those conditions to a different function for extensibility.

func merge(result []string, sub []int) []string {
    if len(sub) >= 3 {
        section := fmt.Sprintf("%d-%d", sub[0], sub[len(sub)-1])
        result = append(result, section)
    } else {
        for _, num := range sub {
            result = append(result, fmt.Sprintf("%d", num))
        }
    }

    return result
}

func solution(list []int) []string {
    var result []string
    var total int = len(list)
    var sub = []int{list[0]}

    for k := 1; k < total; k++ {
        if list[k] == (list[k-1] + 1) {
            sub = append(sub, list[k])
            continue
        }

        result = merge(result, sub)
        sub = []int{list[k]}
    }

    result = merge(result, sub)

    return result
}
7 months ago
  • 8da57acFix minor bugs found by the code static ana…
7 months ago
  • caa417fAdd option to configure the malware scanner…
7 months ago
  • 9c86744Modify default value for some of the alert …
7 months ago
  • 84dd39dAdd option to stop sending the failed login…
7 months ago
  • eb05935Add pre-checks for every plugin page for si…
7 months ago
  • d21a062Modify mechanism to ignore files from integ…
7 months ago
  • b1a9169Add developer option to disable failed pass…
7 months ago
  • 4e3ef13Add support for other English and Spanish b…
7 months ago
  • 2d07b4eFix error interception for Firewall API err…
7 months ago
pushed to master at cixtor/slackapi
  • 4a2c1c8Modify data type for methods related to cha…
  • 6716199Add CLI handler for the users.identity API …
  • f9c448dAdd CLI handler for the mpim.open API endpo…
  • 305d1c4Add CLI handler for the mpim.mark API endpo…
  • 8bb89afAdd CLI handler for the mpim.close API endp…
  • 202a017Add CLI handler for the dnd.teamInfo API en…
  • 68819e9Add CLI handler for the dnd.info API endpoi…
  • 9a2b29aAdd CLI handler for the dnd.endSnooze API e…
  • e7dc86aAdd CLI handler for the dnd.setSnooze API e…
  • 111c53fAdd CLI handler for the dnd.endDnd API endp…
  • cdb620dFix token usage when there are no extra par…
  • View comparison for these 11 commits
7 months ago
pushed to master at cixtor/slackapi
7 months ago
  • 6497e80Remove unnecessary automatic blacklisting o…
7 months ago
opened pull request Sucuri/sucuri-wordpress-plugin#40
Fix multiple bugs with the API calls and queue system
18 commit with 793 additions and 293 deletion
7 months ago
7 months ago
7 months ago
  • 38cc02aModify timing for the dashboard alerts afte…
7 months ago
  • 350c074Fix infinite loop with email alerts and SMT…
7 months ago
  • acff4aaFix detection of base URL with built-in fun…
7 months ago
7 months ago
7 months ago
7 months ago
7 months ago
opened pull request Sucuri/sucuri-wordpress-plugin#39
Add queue system for the security logs and cache improvement
5 commit with 517 additions and 541 deletion
7 months ago
7 months ago
  • 4c51445Fix static function call of non-static Site…
7 months ago
7 months ago
  • af47581Add changelog to release version 1.8.6
7 months ago
opened pull request Sucuri/sucuri-wordpress-plugin#38
Add changelog to release version 1.8.5
6 commit with 4062 additions and 1841 deletion
7 months ago
  • dc1a05aAdd changelog to release version 1.8.5
7 months ago
Do you have a project idea? Let's make it together!