Skip to content

Commit 205a1b9

Browse files
authored
Merge pull request #8 from Eclalang/7-update-funcs-sort-and-max-min
7 update funcs sort and max min
2 parents 8ce9fe5 + d6029be commit 205a1b9

File tree

5 files changed

+112
-78
lines changed

5 files changed

+112
-78
lines changed

README.md

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,17 @@
77

88
| Func Name | Prototype | Description | Comments |
99
|:---------:|:-------------------------------------------------------------------:|:---------------------------------------------------------------------------------------------:|:--------:|
10-
| Contain | func Contain[T comparable](array []T, value T) bool {} | Test if the value is in the array | N/A |
10+
| Contains | func Contain[T comparable](array []T, value T) bool {} | Test if the value is in the array | N/A |
1111
| Find | func Find[T comparable](array []T, value T) int {} | Returns the index of the value in the array <br/> (return -1 if the value isn't in the array) | N/A |
1212
| IsEqual | func IsEqual[T comparable](FirstArray, SecondArray []T) bool {} | Test two array and check if they are equals | N/A |
1313
| Max | func Max[T cmp.Ordered](array []T) (any, error) {} | Returns the maximum value in the array | N/A |
1414
| Min | func Min[T cmp.Ordered](array []T) (any, error) {} | Returns the minimum value in the array | N/A |
1515
| Remove | func Remove[T comparable](array []T, index int) []T {} | Removes the value of the array at the index | N/A |
1616
| Slice | func Slice[T comparable](array []T, start, end int) ([]T, error) {} | Slices the array from start to end | N/A |
17-
| SortAsc | func SortAsc[T cmp.Ordered](array []T) {} | Sorts the array in ascending order | N/A |
18-
| SortDesc | func SortDesc[T cmp.Ordered](array []T) {} | Sorts the array in descending order | N/A |
17+
| Sort | func Sort[T cmp.Ordered](array []T, order int) {} | Sorts the array in ascending or descending order | N/A |
18+
19+
## Constants :
20+
| Name | Type | Value |
21+
|:----:|:----:|:-----:|
22+
| ASC | int | 0 |
23+
| DESC | int | 1 |

array.go

Lines changed: 42 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,18 @@ package array
33
import (
44
"cmp"
55
"errors"
6+
"github.com/Eclalang/Ecla/interpreter/eclaType"
67
"reflect"
78
"sort"
89
)
910

10-
// Contain test if the value is in the array return True if inside and False if not inside
11-
func Contain[T comparable](array []T, value T) bool {
11+
var Variables = map[string]eclaType.Type{
12+
"ASC": eclaType.Int(0),
13+
"DESC": eclaType.Int(1),
14+
}
15+
16+
// Contains test if the value is in the array return True if inside and False if not inside
17+
func Contains[T comparable](array []T, value T) bool {
1218
size := len(array)
1319
for i := 0; i < size; i++ {
1420
if reflect.DeepEqual(array[i], value) {
@@ -49,10 +55,18 @@ func Max[T cmp.Ordered](array []T) (any, error) {
4955
return nil, errors.New("empty array")
5056
}
5157
m := array[0]
52-
for i := 1; i < size; i++ {
53-
if m < array[i] {
54-
m = array[i]
58+
if reflect.TypeOf(m).Kind() == reflect.String {
59+
for i := 1; i < size; i++ {
60+
if len(any(m).(string)) < len(any(array[i]).(string)) {
61+
m = array[i]
62+
} else if len(any(m).(string)) == len(any(array[i]).(string)) {
63+
m = max(m, array[i])
64+
}
5565
}
66+
return m, nil
67+
}
68+
for i := 1; i < size; i++ {
69+
m = max(m, array[i])
5670
}
5771
return m, nil
5872
}
@@ -64,10 +78,19 @@ func Min[T cmp.Ordered](array []T) (any, error) {
6478
return nil, errors.New("empty array")
6579
}
6680
m := array[0]
67-
for i := 1; i < size; i++ {
68-
if array[i] < m {
69-
m = array[i]
81+
if reflect.TypeOf(m).Kind() == reflect.String {
82+
for i := 1; i < size; i++ {
83+
if len(any(m).(string)) > len(any(array[i]).(string)) {
84+
m = array[i]
85+
} else if len(any(m).(string)) == len(any(array[i]).(string)) {
86+
m = min(m, array[i])
87+
}
7088
}
89+
return m, nil
90+
}
91+
92+
for i := 1; i < size; i++ {
93+
m = min(m, array[i])
7194
}
7295
return m, nil
7396
}
@@ -99,16 +122,15 @@ func Slice[T comparable](array []T, start, end int) ([]T, error) {
99122
return arrRet, nil
100123
}
101124

102-
// SortAsc sorts the array in ascending order
103-
func SortAsc[T cmp.Ordered](array []T) {
104-
sort.Slice(array, func(i, j int) bool {
105-
return array[i] < array[j]
106-
})
107-
}
108-
109-
// SortDesc sorts the array in descending order
110-
func SortDesc[T cmp.Ordered](array []T) {
111-
sort.Slice(array, func(i, j int) bool {
112-
return array[i] > array[j]
113-
})
125+
// Sort sorts the array in ascending or descending order
126+
func Sort[T cmp.Ordered](array []T, order int) {
127+
if order == 0 {
128+
sort.Slice(array, func(i, j int) bool {
129+
return array[i] < array[j]
130+
})
131+
} else {
132+
sort.Slice(array, func(i, j int) bool {
133+
return array[i] > array[j]
134+
})
135+
}
114136
}

array_test.go

Lines changed: 58 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -7,33 +7,33 @@ import (
77
)
88

99
// TestContain test the function Contain with array of type int, float, string and boolean
10-
func TestContain(t *testing.T) {
10+
func TestContains(t *testing.T) {
1111
arr := []any{1, 2, 3.3, 4.4, "five", "six", true, false}
1212

1313
// Test int
14-
check1 := Contain(arr, 1)
15-
check2 := Contain(arr, 2)
16-
check3 := Contain(arr, 3)
14+
check1 := Contains(arr, 1)
15+
check2 := Contains(arr, 2)
16+
check3 := Contains(arr, 3)
1717
if check1 == true && check2 == true && check3 == false {
1818
fmt.Println("Yes (1/4) | Array Find is set successfully for int")
1919
} else {
2020
t.Error("No (1/4) | Array Find isn't set successfully for int")
2121
}
2222

2323
// Test float
24-
check1 = Contain(arr, 3.3)
25-
check2 = Contain(arr, 4.4)
26-
check3 = Contain(arr, 5.5)
24+
check1 = Contains(arr, 3.3)
25+
check2 = Contains(arr, 4.4)
26+
check3 = Contains(arr, 5.5)
2727
if check1 == true && check2 == true && check3 == false {
2828
fmt.Println("Yes (2/4) | Array Find is set successfully for float64")
2929
} else {
3030
t.Error("No (2/4) | Array Find isn't set successfully for float64")
3131
}
3232

3333
// Test string
34-
check1 = Contain(arr, "five")
35-
check2 = Contain(arr, "six")
36-
check3 = Contain(arr, "seven")
34+
check1 = Contains(arr, "five")
35+
check2 = Contains(arr, "six")
36+
check3 = Contains(arr, "seven")
3737

3838
if check1 == true && check2 == true && check3 == false {
3939
fmt.Println("Yes (3/4) | Array Find is set successfully for string")
@@ -42,8 +42,8 @@ func TestContain(t *testing.T) {
4242
}
4343

4444
// Test boolean
45-
check1 = Contain(arr, true)
46-
check2 = Contain(arr, false)
45+
check1 = Contains(arr, true)
46+
check2 = Contains(arr, false)
4747
if check1 == true && check2 == true {
4848
fmt.Println("Yes (4/4) | Array Find is set successfully for boolean")
4949
} else {
@@ -127,7 +127,7 @@ func TestIsEqual(t *testing.T) {
127127
func TestMax(t *testing.T) {
128128
arrInt := []int{1, 20, 300, 44, 56}
129129
arrFloat := []float64{100.1, 20.2, 3.346598, 458.9, 5.5}
130-
arrString := []string{"one", "two", "three", "four", "five"}
130+
arrString := []string{"one", "two", "three", "fours", "five"}
131131
var arrEmpty []int
132132

133133
// Test int
@@ -148,7 +148,7 @@ func TestMax(t *testing.T) {
148148

149149
// Test string
150150
stringMax, _ := Max(arrString)
151-
if stringMax == "two" {
151+
if stringMax == "three" {
152152
fmt.Println("Yes (3/4) | Array Max is set successfully for string")
153153
} else {
154154
t.Error("No (3/4) | Array Max isn't set successfully for string")
@@ -167,7 +167,7 @@ func TestMax(t *testing.T) {
167167
func TestMin(t *testing.T) {
168168
arrInt := []int{1, 20, 300, 44, 56}
169169
arrFloat := []float64{100.1, 20.2, 3.346598, 458.9, 5.5}
170-
arrString := []string{"one", "two", "three", "four", "five"}
170+
arrString := []string{"zero", "one", "two", "three", "four", "five"}
171171
var arrEmpty []int
172172

173173
// Test int
@@ -188,7 +188,7 @@ func TestMin(t *testing.T) {
188188

189189
// Test string
190190
stringMin, _ := Min(arrString)
191-
if stringMin == "five" {
191+
if stringMin == "one" {
192192
fmt.Println("Yes (3/4) | Array Min is set successfully for string")
193193
} else {
194194
t.Error("No (3/4) | Array Min isn't set successfully for string")
@@ -336,70 +336,73 @@ func TestSlice(t *testing.T) {
336336
}
337337
}
338338

339-
// TestSortAsc test the function SortAsc with array of type int, float64 and string
340-
func TestSortAsc(t *testing.T) {
339+
// TestSort test the function Sort with array of type int, float64 and string
340+
func TestSort(t *testing.T) {
341341
arrInt := []int{3, 6, 9, 2, 5, 8, 0, 1, 4, 7}
342-
arrIntExpected := []int{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}
342+
arrIntExpectedAscending := []int{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}
343+
arrIntExpectedDescending := []int{9, 8, 7, 6, 5, 4, 3, 2, 1, 0}
343344
arrFloat := []float64{3.3, 6.6, 9.9, 2.2, 5.5, 8.8, 0.0, 1.1, 4.4, 7.7}
344-
arrFloatExpected := []float64{0.0, 1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 7.7, 8.8, 9.9}
345+
arrFloatExpectedAscending := []float64{0.0, 1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 7.7, 8.8, 9.9}
346+
arrFloatExpectedDescending := []float64{9.9, 8.8, 7.7, 6.6, 5.5, 4.4, 3.3, 2.2, 1.1, 0.0}
345347
arrString := []string{"a", "ccc", "bb", "eeeee", "dddd"}
346-
arrStringExpected := []string{"a", "bb", "ccc", "dddd", "eeeee"}
348+
arrStringExpectedAscending := []string{"a", "bb", "ccc", "dddd", "eeeee"}
349+
arrStringExpectedDescending := []string{"eeeee", "dddd", "ccc", "bb", "a"}
347350

348-
SortAsc(arrInt)
349-
SortAsc(arrFloat)
350-
SortAsc(arrString)
351+
// Test ascending order
352+
Sort(arrInt, 0)
353+
Sort(arrFloat, 0)
354+
Sort(arrString, 0)
351355

352356
// Test array int
353-
equal := IsEqual(arrInt, arrIntExpected)
357+
equal := IsEqual(arrInt, arrIntExpectedAscending)
354358
if equal {
355-
fmt.Println("Yes (1/3) | Array SortAsc is set successfully for int")
359+
fmt.Println("Yes (1/6) | Array Sort is set successfully for int in ascending order")
356360
} else {
357-
t.Error("No (1/3) | Array SortAsc isn't set successfully for int")
361+
t.Error("No (1/6) | Array SortAsc isn't set successfully for int in ascending order")
358362
}
359363

360364
// Test array float64
361-
equal = IsEqual(arrFloat, arrFloatExpected)
365+
equal = IsEqual(arrFloat, arrFloatExpectedAscending)
362366
if equal {
363-
fmt.Println("Yes (2/3) | Array SortAsc is set successfully for float64")
367+
fmt.Println("Yes (2/6) | Array SortAsc is set successfully for float64 in ascending order")
364368
} else {
365-
t.Error("No (2/3) | Array SortAsc isn't set successfully for float64")
369+
t.Error("No (2/6) | Array SortAsc isn't set successfully for float64 in ascending order")
366370
}
367371

368372
// Test array string
369-
equal = IsEqual(arrString, arrStringExpected)
373+
equal = IsEqual(arrString, arrStringExpectedAscending)
370374
if equal {
371-
fmt.Println("Yes (3/3) | Array SortAsc is set successfully for string")
375+
fmt.Println("Yes (3/6) | Array SortAsc is set successfully for string in ascending order")
372376
} else {
373-
t.Error("No (3/3) | Array SortAsc isn't set successfully for string")
377+
t.Error("No (3/6) | Array SortAsc isn't set successfully for string in ascending order")
374378
}
375-
}
376-
377-
// TestSortDesc test the function SortAsc with array of type int, float64 and string
378-
func TestSortDesc(t *testing.T) {
379-
arrInt := []int{3, 6, 9, 2, 5, 8, 0, 1, 4, 7}
380-
arrIntExpected := []int{9, 8, 7, 6, 5, 4, 3, 2, 1, 0}
381-
arrFloat := []float64{3.3, 6.6, 9.9, 2.2, 5.5, 8.8, 0.0, 1.1, 4.4, 7.7}
382-
arrFloatExpected := []float64{9.9, 8.8, 7.7, 6.6, 5.5, 4.4, 3.3, 2.2, 1.1, 0.0}
383-
arrString := []string{"a", "ccc", "bb", "eeeee", "dddd"}
384-
arrStringExpected := []string{"eeeee", "dddd", "ccc", "bb", "a"}
385379

386-
SortDesc(arrInt)
387-
SortDesc(arrFloat)
388-
SortDesc(arrString)
380+
// Test descending order
381+
Sort(arrInt, 1)
382+
Sort(arrFloat, 1)
383+
Sort(arrString, 1)
389384

390-
if IsEqual(arrInt, arrIntExpected) {
391-
fmt.Println("Yes (1/3) | Array SortDesc is set successfully for int")
385+
// Test array int
386+
equal = IsEqual(arrInt, arrIntExpectedDescending)
387+
if equal {
388+
fmt.Println("Yes (4/6) | Array Sort is set successfully for int in descending order")
392389
} else {
393-
t.Error("No (1/3) | Array SortDesc isn't set successfully for int")
390+
t.Error("No (4/6) | Array Sort isn't set successfully for int in descending order")
394391
}
395-
if IsEqual(arrFloat, arrFloatExpected) {
396-
fmt.Println("Yes (2/3) | Array SortDesc is set successfully for float64")
392+
393+
// Test array float64
394+
equal = IsEqual(arrFloat, arrFloatExpectedDescending)
395+
if equal {
396+
fmt.Println("Yes (5/6) | Array Sort is set successfully for float64 in descending order")
397397
} else {
398-
t.Error("No (2/3) | Array SortDesc isn't set successfully for float64")
398+
t.Error("No (5/6) | Array Sort isn't set successfully for float64 in descending order")
399399
}
400-
if IsEqual(arrString, arrStringExpected) {
401-
fmt.Println("Yes (3/3) | Array SortDesc is set successfully for string")
400+
401+
// Test array string
402+
equal = IsEqual(arrString, arrStringExpectedDescending)
403+
if equal {
404+
fmt.Println("Yes (6/6) | Array Sort is set successfully for string in descending order")
402405
} else {
403-
t.Error("No (3/3) | Array SortDesc isn't set successfully for string")
406+
t.Error("No (6/6) | Array Sort isn't set successfully for string in descending order")
404407
}
405408
}

go.mod

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
module github.com/Eclalang/array
22

33
go 1.21.1
4+
5+
require github.com/Eclalang/Ecla v1.0.0

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
github.com/Eclalang/Ecla v1.0.0 h1:JrbxU5m6hWfJ8D5/xsqtbhWljOOetDdV6JzEU82/4Fc=
2+
github.com/Eclalang/Ecla v1.0.0/go.mod h1:0Nq24TRfBRBY5oKK+U2hpdzQV0rlisPQZ5nhBLNGxAU=

0 commit comments

Comments
 (0)