Skip to content

Instantly share code, notes, and snippets.

@caelifer
Created January 7, 2020 22:56
Show Gist options
  • Save caelifer/6af0ef7b74e955cb7a44773430a43edb to your computer and use it in GitHub Desktop.
Save caelifer/6af0ef7b74e955cb7a44773430a43edb to your computer and use it in GitHub Desktop.

Revisions

  1. caelifer created this gist Jan 7, 2020.
    73 changes: 73 additions & 0 deletions phone.go
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,73 @@
    // This program provides an answer for one of the Google interview questions described here:
    // https://www.businessinsider.com/answers-to-google-interview-questions-2011-11#you-need-to-check-that-your-friend-bob-has-your-correct-phone-number-10
    package main

    import (
    "fmt"
    "strings"
    )

    func main() {
    collisions := make(map[uint][]string)

    for _, p := range [...][10]rune{
    {'1', '1', '1', '1', '1', '1', '1', '1', '1', '1'},
    {'1', '1', '1', '1', '1', '1', '1', '1', '1', '2'},
    {'1', '1', '1', '1', '1', '1', '1', '1', '1', '3'},
    {'1', '1', '1', '1', '1', '1', '1', '1', '1', '4'},
    {'1', '1', '1', '1', '1', '1', '1', '1', '1', '5'},
    {'1', '1', '1', '1', '1', '1', '1', '1', '1', '6'},
    {'1', '1', '1', '1', '1', '1', '1', '1', '1', '7'},
    {'1', '1', '1', '1', '1', '1', '1', '1', '1', '8'},
    {'1', '1', '1', '1', '1', '1', '1', '1', '1', '9'},

    {'2', '1', '1', '1', '1', '1', '1', '1', '1', '1'},
    {'1', '2', '1', '1', '1', '1', '1', '1', '1', '1'},
    {'1', '1', '2', '1', '1', '1', '1', '1', '1', '1'},
    {'1', '1', '1', '2', '1', '1', '1', '1', '1', '1'},
    {'1', '1', '1', '1', '2', '1', '1', '1', '1', '1'},
    {'1', '1', '1', '1', '1', '2', '1', '1', '1', '1'},
    {'1', '1', '1', '1', '1', '1', '2', '1', '1', '1'},
    {'1', '1', '1', '1', '1', '1', '1', '2', '1', '1'},
    {'1', '1', '1', '1', '1', '1', '1', '1', '2', '1'},
    {'1', '1', '1', '1', '1', '1', '1', '1', '1', '2'},

    {'9', '1', '1', '1', '1', '1', '1', '1', '1', '1'},
    {'1', '9', '1', '1', '1', '1', '1', '1', '1', '1'},
    {'1', '1', '9', '1', '1', '1', '1', '1', '1', '1'},
    {'1', '1', '1', '9', '1', '1', '1', '1', '1', '1'},
    {'1', '1', '1', '1', '9', '1', '1', '1', '1', '1'},
    {'1', '1', '1', '1', '1', '9', '1', '1', '1', '1'},
    {'1', '1', '1', '1', '1', '1', '9', '1', '1', '1'},
    {'1', '1', '1', '1', '1', '1', '1', '9', '1', '1'},
    {'1', '1', '1', '1', '1', '1', '1', '1', '9', '1'},
    {'1', '1', '1', '1', '1', '1', '1', '1', '1', '9'},
    } {
    phone, sum := Phone(p), Sum(p)

    fmt.Printf("%s - %d\n", phone, sum)
    if phones, ok := collisions[sum]; ok {
    phones = append(phones, phone)
    collisions[sum] = phones
    } else {
    collisions[sum] = []string{phone}
    }
    }
    for k, v := range collisions {
    if len(v) > 1 {
    fmt.Printf("Found collision on %d for: %s\n", k, strings.Join(v, ", "))
    }
    }
    }

    func Sum(digits [10]rune) uint {
    res := uint(0)
    for _, n := range digits {
    res += (res + uint(n-'0')) * 10
    }
    return res
    }

    func Phone(digits [10]rune) string {
    return fmt.Sprintf("(%s) %s-%s", string(digits[:3]), string(digits[3:6]), string(digits[6:]))
    }