DEV Community

manuel
manuel

Posted on • Originally published at defer.cc on

1 1

Shamazing

Find the longest string or integer in a SHA1 Hash (or whatever). It’s just sha-mazing and without any sense :)

package shamazing
import (
   "regexp"
   "strconv"
)

// FindLongestString will retrieve a string like a SHA1, MD5 or whatever
// and return the longest string (first one)
func FindLongestString(str string) string {
   var re = regexp.MustCompile("[a-zA-Z]+")
   var values = re.FindAll([]byte(str), -1)
   return string(findLongest(values))
}
// FindLongestInteger will retrieve a string like a SHA1, MD5 or whatever
// and return longest integer (first one)
func FindLongestInteger(str string) (int64, error) {
   var re = regexp.MustCompile("[0-9]+")
   var values = re.FindAll([]byte(str), -1)
   var max = findLongest(values)
   var a, err = strconv.Atoi(string(max))
   if err != nil {
      return 0, err
   }
   return int64(a), nil
}
func findLongest(values [][]byte) []byte {
   var max []byte
   for _, value := range values {
      if len(value) > len(max) {
         max = value
      }
   }
   return max
}

Enter fullscreen mode Exit fullscreen mode

shamazing on GitHub

Hostinger image

Get n8n VPS hosting 3x cheaper than a cloud solution

Get fast, easy, secure n8n VPS hosting from $4.99/mo at Hostinger. Automate any workflow using a pre-installed n8n application and no-code customization.

Start now

Top comments (0)

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay