Python 3 Dictionaries
The name of the script will be sortArtrays.py and will accept two command line arguments, the amount of the available memory and the name of the text file – the sortArtrays.py script leaves the original input file intact.
There is a surprise here: instead of using an array, the Python 3 implementation will use a Dictionary because dictionaries are more versatile in Python 3 – additionally, when you are using a dictionary you do not occupy any unnecessary memory places. The most important Python 3 code of sortArtrays.py is the next: if (value >= space) and (value < space+size):
key = value data[key] = 1 If a value is within the desired range, then a new pair is inserted in the dictionary variable named data, which does the desired job. Executing sortArtrays.py will generate the next kind of output: $ ./sortArrays.py 2 test 28 Passes: 4 23458
The next section will implement the same algorithm in Go.
Go Arrays
This section will use Go to implement the previous scenario where a fixed amount of RAM is available. The name of the Go utility will be sortArrays.go, will accept two parameters, the size of the available memory and the name of the text file to process that will only contain positive integers, which are also known as natural numbers.
The first idea was to use a Go array – as Go arrays have a fixed size, they look perfect for simulating a fixed amount of RAM. The bad thing is that when you pass an array to a function, you actually pass a copy of the array, which means that any changes you make to an array inside a function will be lost after the function finishes – however, as sortArrays. go prints data on screen, this would not have been a problem. The real problem with Go arrays is that you cannot easily define their size at runtime, which is the main reason that sortArrays.go will use a slice.
As each element of the slice will be used for a given number, there is no need to write this number again.