File stats exercise in Golang
3 years ago in HTML
<html>
<head><meta http-equiv=Content-Type content="text/html; charset=UTF-8">
</head>
<body>
<div>
<h1>Stage 1</h1>
<div>Write a library that performs statistics and aggregations for file metadata provided to it.</div>
<h2>Required Functions</h2>
<div>The library MUST implement the following functions:</div><br>
<div><code style="background-color: #dddcdc;">AddFile(metadata FileMetadata) error</code></div>
<div>The function receives a structure containing the metadata of one file. This file should be taken into account when calculating statistics. The function can return an error if the input is invalid or processing of the file fails.</div><br>
<div><code style="background-color: #dddcdc;">GetStats() FileStats</code></div>
<div>This function returns statistics for all files added until that point. The following statistics should be returned:</div>
<ul>
<li>Number of files received</li>
<li>Largest file received (including name and size)</li>
<li>Average file size</li>
<li>Most frequent file extension (including number of occurences)</li>
<li>Percentage of text files of all files received</li>
<li>List of latest 10 file paths received</li>
</ul>
<h2>Required Types/Structures</h2>
<div>The following structures should be used by the library:</div>
<div>
<div style="background: #ffffff; overflow:auto;width:auto;border:solid gray;border-width:.1em .1em .1em .8em;padding:.2em .6em;"><pre style="margin: 0; line-height: 125%"><span style="color: #008800; font-weight: bold">type</span> FileMetadata <span style="color: #008800; font-weight: bold">struct</span> {
Path <span style="color: #333399; font-weight: bold">string</span> <span style="background-color: #fff0f0">`json:&quot;path&quot;`</span> <span style="color: #888888">// the file&#39;s absolute path</span>
Size <span style="color: #333399; font-weight: bold">int64</span> <span style="background-color: #fff0f0">`json:&quot;size&quot;`</span> <span style="color: #888888">// the file size in bytes</span>
IsBinary <span style="color: #333399; font-weight: bold">bool</span> <span style="background-color: #fff0f0">`json:&quot;is_binary&quot;`</span> <span style="color: #888888">// whether the file is a binary file or a simple text file</span>
}
<span style="color: #008800; font-weight: bold">type</span> FileStats <span style="color: #008800; font-weight: bold">struct</span> {
NumFiles <span style="color: #333399; font-weight: bold">int64</span> <span style="background-color: #fff0f0">`json:&quot;num_files&quot;`</span>
LargestFile LargestFileInfo <span style="background-color: #fff0f0">`json:&quot;largest_file&quot;`</span>
AverageFileSize <span style="color: #333399; font-weight: bold">float64</span> <span style="background-color: #fff0f0">`json:&quot;avg_file_size&quot;`</span>
MostFrequentExt ExtInfo <span style="background-color: #fff0f0">`json:&quot;most_frequent_ext&quot;`</span>
TextPercentage <span style="color: #333399; font-weight: bold">float32</span> <span style="background-color: #fff0f0">`json:&quot;text_percentage&quot;`</span>
MostRecentPaths []<span style="color: #333399; font-weight: bold">string</span> <span style="background-color: #fff0f0">`json:&quot;most_recent_paths&quot;`</span>
}
<span style="color: #008800; font-weight: bold">type</span> LargestFileInfo <span style="color: #008800; font-weight: bold">struct</span> {
Path <span style="color: #333399; font-weight: bold">string</span> <span style="background-color: #fff0f0">`json:&quot;path&quot;`</span>
Size <span style="color: #333399; font-weight: bold">int64</span> <span style="background-color: #fff0f0">`json:&quot;size&quot;`</span>
}
<span style="color: #008800; font-weight: bold">type</span> ExtInfo <span style="color: #008800; font-weight: bold">struct</span> {
Extension <span style="color: #333399; font-weight: bold">string</span> <span style="background-color: #fff0f0">`json:&quot;extension&quot;`</span>
NumOccurrences <span style="color: #333399; font-weight: bold">int64</span> <span style="background-color: #fff0f0">`json:&quot;num_occurrences&quot;`</span>
}
</pre></div>
</div>
<h2>Guidelines</h2>
<div>The library MAY define a structure/type/object on which the two required functions are to be called.</div>
<div>There is no guarantee that calls to AddFile() will happen sequentially.</div>
<div>Create a unit test for the library</div>
<h1>Stage 2</h1>
<div>Create a command line utility to uses the library. The utility reads a list of file metadata from standard input,</div>
<div>where each line is a JSON representation of the metadata. The utility calls AddFile() for each line, and finally</div>
<div>prints the output of GetStats() in JSON format when input ends.</div>
<h2>Bonus:</h2>
<div>create a<span class="cls_003"> <A HREF="https://docs.docker.com/engine/reference/builder/">Dockerfile</A> for the executable.</div>
</div>
</body>
</html>