diff options
| author | Paul Buetow <pbuetow@mimecast.com> | 2020-08-08 14:06:12 +0100 |
|---|---|---|
| committer | Paul Buetow <pbuetow@mimecast.com> | 2020-08-08 14:06:12 +0100 |
| commit | 06c8d4c68650bbc1fc158d90a44c4c17644c7889 (patch) | |
| tree | 3b2bd79680bc233dadd93e1a2be48e00ee60b66b /ds/arraylist.go | |
| parent | 44e930b71107310eae55060cf0aa2cac7089d239 (diff) | |
wrapup
Diffstat (limited to 'ds/arraylist.go')
| -rw-r--r-- | ds/arraylist.go | 35 |
1 files changed, 33 insertions, 2 deletions
diff --git a/ds/arraylist.go b/ds/arraylist.go index 66490f3..5964b88 100644 --- a/ds/arraylist.go +++ b/ds/arraylist.go @@ -2,10 +2,41 @@ package ds import ( "fmt" + "math/rand" "strings" ) -type ArrayList []Integer +type ArrayList []int + +func NewRandomArrayList(length, max int) ArrayList { + a := make(ArrayList, length) + for i := 0; i < length; i++ { + if max > 0 { + a[i] = rand.Intn(max) + continue + } + a[i] = rand.Int() + } + return a +} + +func NewAscendingArrayList(length int) ArrayList { + a := make(ArrayList, length) + for i := 0; i < length; i++ { + a[i] = i + } + return a +} + +func NewDescendingArrayList(length int) ArrayList { + a := make(ArrayList, length) + j := length - 1 + for i := 0; i < length; i++ { + a[i] = j + j-- + } + return a +} func (a ArrayList) FirstN(n int) string { var sb strings.Builder @@ -29,7 +60,7 @@ func (a ArrayList) FirstN(n int) string { func (a ArrayList) Sorted() bool { for i := len(a) - 1; i > 0; i-- { - if a[i].Lower(a[i-1]) { + if a[i] < a[i-1] { return false } } |
