Skip to content

Commit 52642d5

Browse files
committed
covert slice of ints into zig_zag
1 parent bc23627 commit 52642d5

File tree

5 files changed

+100
-1
lines changed

5 files changed

+100
-1
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ WIP, the descriptions of the below `unsolved yet` problems can be found in the [
107107
- [x] [Reverse an array without affecting special characters](https://github.com/danrusei/algorithms_with_Go/tree/main/strings/reverse_alpha)
108108
- [] All Possible Palindromic Partitions
109109
- [] Count triplets with sum smaller than a given value
110-
- [] Convert array into Zig-Zag fashion
110+
- [x] [Convert array into Zig-Zag fashion](https://github.com/danrusei/algorithms_with_Go/tree/main/strings/zig_zag)
111111
- [] Generate all possible sorted arrays from alternate elements of two given sorted arrays
112112
- [] Pythagorean Triplet in an array
113113
- [x] [Length of the largest subarray with contiguous elements](https://github.com/danrusei/algorithms_with_Go/tree/main/strings/largest_subarray)

strings/zig_zag/README.md

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# Convert array into Zig-Zag fashion
2+
3+
Source: [GeeksforGeeks](https://www.geeksforgeeks.org/convert-array-into-zig-zag-fashion/amp/)
4+
5+
Given an array of DISTINCT elements, rearrange the elements of array in zig-zag fashion in O(n) time. The converted array should be in form a < b > c < d > e < f.
6+
7+
## Example
8+
9+
```bash
10+
Input: arr[] = {4, 3, 7, 8, 6, 2, 1}
11+
Output: arr[] = {3, 7, 4, 8, 2, 6, 1}
12+
13+
Input: arr[] = {1, 4, 3, 2}
14+
Output: arr[] = {1, 4, 2, 3}
15+
```
16+
17+
## Algorithm
18+
19+
* create a flag to denote the "<" or ">"
20+
* if two consecutive numbers do not have the expected relationship, then swap the elements
21+
22+
## Result
23+
24+
```bash
25+
$ go test -v
26+
=== RUN TestReverse
27+
=== RUN TestReverse/first_10
28+
=== RUN TestReverse/7_values
29+
=== RUN TestReverse/4_values
30+
--- PASS: TestReverse (0.00s)
31+
--- PASS: TestReverse/first_10 (0.00s)
32+
--- PASS: TestReverse/7_values (0.00s)
33+
--- PASS: TestReverse/4_values (0.00s)
34+
PASS
35+
```

strings/zig_zag/test_cases.go

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package zigzag
2+
3+
var testcases = []struct {
4+
name string
5+
input []int
6+
output []int
7+
}{
8+
{
9+
name: "first_10",
10+
input: []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10},
11+
output: []int{1, 3, 2, 5, 4, 7, 6, 9, 8, 10},
12+
},
13+
{
14+
name: "7_values",
15+
input: []int{4, 3, 7, 8, 6, 2, 1},
16+
output: []int{3, 7, 4, 8, 2, 6, 1},
17+
},
18+
{
19+
name: "4_values",
20+
input: []int{1, 4, 3, 2},
21+
output: []int{1, 4, 2, 3},
22+
},
23+
}

strings/zig_zag/zigzag.go

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package zigzag
2+
3+
func zigzag(input []int) {
4+
5+
// if `less` is true indicate "<", else ">"
6+
// initially is true as we start with "<"
7+
// a < b > c < d > e < f
8+
less := true
9+
10+
for i := 0; i <= len(input)-2; i++ {
11+
if less {
12+
if input[i] > input[i+1] {
13+
input[i], input[i+1] = input[i+1], input[i]
14+
}
15+
} else {
16+
if input[i] < input[i+1] {
17+
input[i], input[i+1] = input[i+1], input[i]
18+
}
19+
}
20+
21+
// change flag to false, as we should operate on oposite signo
22+
less = !less
23+
}
24+
}

strings/zig_zag/zigzag_test.go

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package zigzag
2+
3+
import (
4+
"reflect"
5+
"testing"
6+
)
7+
8+
func TestReverse(t *testing.T) {
9+
for _, tc := range testcases {
10+
t.Run(tc.name, func(t *testing.T) {
11+
zigzag(tc.input)
12+
if !reflect.DeepEqual(tc.input, tc.output) {
13+
t.Errorf("Expected: %d, got: %d", tc.output, tc.input)
14+
}
15+
})
16+
}
17+
}

0 commit comments

Comments
 (0)