Compresion Algorithm
A compression algorithm is a computational technique used to reduce the size of data, allowing it to be stored or transmitted more efficiently. These algorithms play a crucial role in the digital world, as they facilitate the storage and transmission of large files, such as images, audio files, and video files, by compressing them into a smaller, more manageable size. Compression algorithms can be broadly classified into two categories: lossless and lossy. Lossless compression algorithms maintain the original quality of the data, while lossy algorithms result in some loss of data quality due to the compression process. Both types of algorithms employ various techniques, including encoding redundancy elimination, statistical coding, and mathematical transformations, to achieve their intended level of compression.
Lossless compression algorithms, such as Huffman coding or Lempel-Ziv-Welch (LZW) compression, work by identifying and eliminating redundancies within the data, allowing the original data to be precisely reconstructed upon decompression. These algorithms are commonly employed in file formats that require exact replicas of the original data, such as text files, executable programs, and certain image formats like PNG or GIF. On the other hand, lossy compression algorithms, such as JPEG for images or MP3 for audio files, intentionally discard some of the data in order to achieve a higher compression ratio. This results in a smaller file size at the expense of some reduction in quality, which is usually acceptable for human perception in multimedia applications. Both lossless and lossy compression algorithms continue to evolve, with newer techniques being developed to further optimize compression performance and minimize quality loss.
package compression
/**
* Calculation of Run Length Encoding using Tail Recursion
* Created by Andy on 05/05/2016.
*/
tailrec fun runLengthEncoding(text:String,prev:String=""):String {
if (text.length == 0){
return prev
}
val initialChar = text.get(0)
val count = text.takeWhile{ it==initialChar }.count()
return runLengthEncoding(text.substring(count),prev + "$count$initialChar" )
}