Skip to content

Instantly share code, notes, and snippets.

@rbalman
Created August 13, 2020 14:45
Show Gist options
  • Save rbalman/a97e8080cad5e17122595061242a1ab3 to your computer and use it in GitHub Desktop.
Save rbalman/a97e8080cad5e17122595061242a1ab3 to your computer and use it in GitHub Desktop.
Sorting Challenge

UseCase

We have a list containing 25 numbers, that we need to sort and figure out the greatest three numbers. However, the catch is that you can only sort 5 numbers in a single operation. So, basically imagine a function that will sort your numbers but can only take in 5 at a time. Using this function we need to be able to sort the 25 numbers to get top 3. Also, make a note of how many times the sorting function is called.

package main
import (
"fmt"
"math/rand"
"errors"
"sort"
)
// Generate Random number within this range
const RandomRange = 1500
// Total list count to compare within
const ListsCount = 15
// Size of the each list
const ListSize = 3
func getRandomList(len int) ([]int, error) {
var l []int
if len <= 0{
return nil, errors.New("List length Should be greater than 0")
}
var i int
for i=0; i<len; i++ {
l = append(l, rand.Intn(RandomRange))
}
return l, nil
}
type RandomLists [][]int
func main() {
var i int
var ls RandomLists
if ListSize < 3 {
fmt.Println("ListSize cannot be less than 3 for now")
return
}
// Generate Random List Data
for i = 0; i < ListsCount; i++ {
l, err := getRandomList(ListSize)
if err != nil {
fmt.Println("ERROR: ", err)
return
}
ls = append(ls, l)
}
fmt.Println("Random Lists: ",ls)
//Sort the generated lists
for _, l := range ls {
sort.Ints(l)
}
fmt.Println("Sorted Lists: ",ls)
var A, B,C int //lets assume A, B and C are three greatest number in descending order
/* FIND LARGEST HERE
- Create a new temp list of largest numbers of each list
- sort this temp list
- Assign:
- A=largest in temp list
- B=2nd largest in temp list
- C=3rd largest in temp list
*/
var tmp []int
for _, l := range ls {
tmp = append(tmp, l[ListSize - 1])
}
sort.Ints(tmp)
A = tmp[ListsCount - 1]
B = tmp[ListsCount - 2]
C = tmp[ListsCount - 3]
/* FIND 2nd LARGEST HERE
- Create a new temp list of 2nd largest numbers of each list
- sort this temp list
- Assign:
- B=assign largest value in temp list only if the existing value is samller
- C=assign 2nd largest value in temp list only if the existing value is samller
*/
tmp = nil
for _, l := range ls {
tmp = append(tmp, l[ListSize - 2])
}
sort.Ints(tmp)
if tmp[ListsCount - 1] > B {
B = tmp[ListsCount - 1]
}
if tmp[ListsCount - 2] > C {
C = tmp[ListsCount - 2]
}
/* FIND 3rd LARGEST HERE
- Create a new temp list of 3rd largest numbers of each list
- sort this temp list
- Assign:
- C=assign largest number in temp list only if the existing value is samller
*/
tmp = nil
for _, l := range ls {
tmp = append(tmp, l[ListSize - 3])
}
sort.Ints(tmp)
if tmp[ListsCount - 1] > C {
C = tmp[ListsCount - 2]
}
fmt.Printf("Largest Number in Descending Order: %d, %d, %d\n",A, B, C)
}
@barunthapa
Copy link

Balman, couldn't figure out where that original list of 25 numbers is, and then the sorting function to sort the 5 at a time.
here is my snippet for sorting function

func sorter(list []int, count *int) []int {
	//if len(list) > 5 {
	//	return []int{}
	//}
	//fmt.Println("inside sort : ", list)
	*count++
	sort.Ints(list)
	//fmt.Println("inside sort : ", list)
	return list
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment