diff options
| author | Mariano Spadaccini <spadacciniweb@gmail.com> | 2025-10-02 16:12:28 +0200 |
|---|---|---|
| committer | Mariano Spadaccini <spadacciniweb@gmail.com> | 2025-10-02 16:12:28 +0200 |
| commit | 8b5e21e973d05854e4fec739dda8d2d7ce79df61 (patch) | |
| tree | 1c813069e7b3b39549adf1a1bbb64dd471cbab48 | |
| parent | ba830b6ade4f507e4f121a8b6221b496cbe1dc76 (diff) | |
| download | perlweeklychallenge-club-8b5e21e973d05854e4fec739dda8d2d7ce79df61.tar.gz perlweeklychallenge-club-8b5e21e973d05854e4fec739dda8d2d7ce79df61.tar.bz2 perlweeklychallenge-club-8b5e21e973d05854e4fec739dda8d2d7ce79df61.zip | |
Add ch-2 in Go
| -rw-r--r-- | challenge-341/spadacciniweb/go/ch-2.go | 93 |
1 files changed, 93 insertions, 0 deletions
diff --git a/challenge-341/spadacciniweb/go/ch-2.go b/challenge-341/spadacciniweb/go/ch-2.go new file mode 100644 index 0000000000..b34c62ec20 --- /dev/null +++ b/challenge-341/spadacciniweb/go/ch-2.go @@ -0,0 +1,93 @@ +/* +Task 2: Reverse Prefix +Submitted by: Mohammad Sajid Anwar + +You are given a string, $str and a character in the given string, $char. +Write a script to reverse the prefix upto the first occurrence of the given $char in the given string $str and return the new string. + +Example 1 +Input: $str = "programming", $char = "g" +Output: "gorpmming" + +Reverse of prefix "prog" is "gorp". + +Example 2 +Input: $str = "hello", $char = "h" +Output: "hello" + +Example 3 +Input: $str = "abcdefghij", $char = "h" +Output: "hgfedcbaj" + +Example 4 +Input: $str = "reverse", $char = "s" +Output: "srevere" + +Example 5 +Input: $str = "perl", $char = "r" +Output: "repl" +*/ + +package main + +import ( + "fmt" +) + +func reverse_array(arr []rune) []rune { + left := 0 + right := len(arr) - 1 + for left < right { + arr[left], arr[right] = arr[right], arr[left] + left++ + right-- + } + return arr +} + +func reverse_prefix(str string, chars string) { + var str_new []rune + var c rune + + for _, char := range chars { + c = char + break + } + + for _, s := range str { + str_new = append(str_new, s) + if s == c { + reverse_array(str_new) + break + } + } + + for _, s := range str[len(str_new):] { + str_new = append(str_new, s) + } + + fmt.Printf("'%s' '%s' -> '%s'\n", string(str), string(chars), string(str_new)) +} + + +func main() { + str := "programming" + char := "g" + reverse_prefix( str, char ) + + str = "hello" + char = "h" + reverse_prefix( str, char ) + + str = "abcdefghij" + char = "h" + reverse_prefix( str, char ) + + str = "reverse" + char = "s" + reverse_prefix( str, char ) + + str = "perl" + char = "r" + reverse_prefix( str, char ) +} |
