Browse Source

Adding solution for 1108

Vinicius Teshima 1 week ago
parent
commit
710191e357
2 changed files with 43 additions and 0 deletions
  1. 36 0
      Go/1108.go
  2. 7 0
      Go/build.sh

+ 36 - 0
Go/1108.go

@@ -0,0 +1,36 @@
+package main;
+// 1108. Defanging an IP Address
+
+import (
+	"os"
+	"fmt"
+)
+
+func defangIPaddr(address string) string {
+	var res [3+3+3+3+9]rune
+	var i int = 0
+
+	for _, c := range address {
+		if c == '.' {
+			res[i] = '['; i += 1
+			res[i] = '.'; i += 1
+			res[i] = ']'; i += 1
+			continue
+		}
+		res[i] = c
+		i += 1
+	}
+
+	return string(res[:i])
+}
+
+func main() {
+	r := func (addr string) {
+		fmt.Printf("defangIPaddr(\"%s\") -> \"%s\"\n", addr, defangIPaddr(addr))
+	}
+
+	r("1.1.1.1")
+	r("255.100.50.0")
+
+	os.Exit(0)
+}

+ 7 - 0
Go/build.sh

@@ -0,0 +1,7 @@
+#!/bin/sh
+
+newest_file="$(find ./ -type f -printf "%T@ %p\n" | sort -n | cut -d' ' -f 2- | tail -n 1)"
+
+set -x
+go run $newest_file
+