Last active
March 20, 2023 05:22
-
-
Save pwener/6036f4fc08a20d45333775f2fa03afa7 to your computer and use it in GitHub Desktop.
Code to invert words and numbers escaping special characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package main | |
| import ( | |
| "fmt" | |
| "regexp" | |
| "strings" | |
| ) | |
| func invert(str string) string { | |
| result := "" | |
| for i := len(str) - 1; i >= 0; i-- { | |
| result = result + string(str[i]) | |
| } | |
| return result | |
| } | |
| func invertPhrase(phrase string) string { | |
| re := regexp.MustCompile(`[ !.,;#]`) | |
| words := re.Split(phrase, -1) | |
| inverted := []string{} | |
| for _, w := range words { | |
| inverted = append(inverted, invert(w)) | |
| } | |
| result := strings.Join(inverted, " ") | |
| tokerize := strings.Split(result, "") | |
| for i, c := range tokerize { | |
| space := " " | |
| if c == space { | |
| tokerize[i] = string(phrase[i]) | |
| } | |
| } | |
| return strings.Join(tokerize, "") | |
| } | |
| // eht tibbar spmuj" | |
| func main() { | |
| r1 := invertPhrase("the rabbit jumps") | |
| fmt.Println(r1 == "eht tibbar spmuj") | |
| r2 := invertPhrase("Hello World. Hi Phelipe.") | |
| fmt.Println(r2) | |
| fmt.Println(r2 == "olleH dlroW. iH epilehP.") | |
| r3 := invertPhrase(" Lorem ipsum") | |
| fmt.Println(r3 == " meroL muspi") | |
| r4 := invertPhrase(" Lorem ipsum ") | |
| fmt.Println(r4 == " meroL muspi ") | |
| r5 := invertPhrase(" ") | |
| fmt.Println(r5 == " ") | |
| r6 := invertPhrase("5") | |
| fmt.Println(r6 == "5") | |
| r7 := invertPhrase("5 10") | |
| fmt.Println(r7 == "5 01") | |
| r8 := invertPhrase("#. !") | |
| fmt.Println(r8 == "#. !") | |
| r9 := invertPhrase("Hello Wo.rld. Hi Phelipe.") | |
| fmt.Println(r9) | |
| fmt.Println(r9 == "olleH oW.dlr. iH epilehP.") | |
| // it should fails by now | |
| // r9 := invertPhrase("já vôvô") | |
| // fmt.Println(r9 == "áj ôvôv") | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package main | |
| import ( | |
| "fmt" | |
| "strings" | |
| "unicode" | |
| ) | |
| func invertPhrase(phrase string) string { | |
| result := []string{} | |
| lastWord := []rune{} | |
| for _, v := range phrase { | |
| if unicode.IsLetter(v) || unicode.IsNumber(v) { | |
| lastWord = append([]rune{v}, lastWord...) | |
| } else { | |
| result = append(result, fmt.Sprintf("%s%c", string(lastWord), v)) | |
| lastWord = []rune{} | |
| } | |
| } | |
| result = append(result, string(lastWord)) | |
| return strings.Join(result, "") | |
| } | |
| func main() { | |
| testCases := map[string]string{ | |
| "the rabbit jumps": "eht tibbar spmuj", | |
| "Hello World. Hi Phelipe.": "olleH dlroW. iH epilehP.", | |
| " Lorem ipsum": " meroL muspi", | |
| " Lorem ipsum ": " meroL muspi ", | |
| " ": " ", | |
| "5": "5", | |
| "5 10": "5 01", | |
| "#. !": "#. !", | |
| "Hello Wo.rld. Hi Phelipe.": "olleH oW.dlr. iH epilehP.", | |
| "já vôvô": "áj ôvôv", | |
| } | |
| for k, v := range testCases { | |
| if invertPhrase(k) == v { | |
| fmt.Println("success..") | |
| } else { | |
| fmt.Println("fails...") | |
| } | |
| } | |
| } | |
| // output: | |
| // success.. | |
| // success.. | |
| // success.. | |
| // success.. | |
| // success.. | |
| // success.. | |
| // success.. | |
| // success.. | |
| // success.. | |
| // success.. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package main | |
| import "fmt" | |
| func invert(str string) string { | |
| result := "" | |
| for i := len(str) - 1; i >= 0; i-- { | |
| result = result + string(str[i]) | |
| } | |
| return result | |
| } | |
| func main() { | |
| str := "the rabbit jumps" | |
| // it prints spmuj tibbar eht | |
| fmt.Println(invert(str)) | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import "fmt" | |
| func main() { | |
| str := "Lorem ipsum" | |
| // it prints string | |
| fmt.Printf("%T\n", str) | |
| // it prints uint8 | |
| fmt.Printf("%T\n", str[0]) | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment