6 kyu

Number Shortening Filter

455 of 986GiacomoSorbi

Description:

Here is a new kata that Codewars asked me to do related to interviewing and working in a production setting.

You might be familar with and have used Angular.js. Among other things, it lets you create your own filters that work as functions. You can then put these in a page to perform specific data changes, such as shortening a number to display a more concise notation.

In this kata, we will create a function which returns another function (or process, in Ruby) that shortens very long numbers. Given an initial array of values replace the Xth power of a given base. If the input of the returned function is not a numerical string, it should return the input itself as a string.

Here's an example, which is worth a thousand words:

filter1 = shortenNumber(['','k','m'],1000);
filter1('234324') == '234k';
filter1('98234324') == '98m';
filter1([1,2,3]) == '1,2,3';
filter2 = shortenNumber(['B','KB','MB','GB'],1024);
filter2('32') == '32B';
filter2('2100') == '2KB';
filter2('pippi') == 'pippi';
filter1 = shorten_number(['','k','m'],1000)
filter1('234324') == '234k'
filter1('98234324') == '98m'
filter1([1,2,3]) == '[1,2,3]'
filter2 = shorten_number(['B','KB','MB','GB'],1024)
filter2('32') == '32B'
filter2('2100') == '2KB';
filter2('pippi') == 'pippi'
var filter1 = shortenMethods.ShortenNumberCreator(new string[] { "", "k", "m" }, 1000);
filter1("234324") == "234k";
filter1("98234324") == "98m";
filter1(new int[] { 1,2,3 }) == "1,2,3";
var filter2 = shortenMethods.ShortenNumberCreator(new string[] { "B", "KB", "MB", "GB" }, 1024);
filter2("32") == "32B";
filter2("2100") == "2KB";
filter2("pippi") == "pippi";
filter1 = shorten_number(['','k','m'],1000)
filter1.call('234324') == '234k'
filter1.call('98234324') == '98m'
filter1({}) == '{}'
filter2 = shortenNumber(['B','KB','MB','GB'],1024)
filter2.call('32') == '32B';
filter2.call('2100') == '2KB';
filter2.call('pippi') == 'pippi'

If you like to test yourself on kata related to actual work and interviews, consider trying this kata where you will build a breadcrumb generator

Angular
Algorithms

Stats:

CreatedFeb 5, 2016
PublishedFeb 5, 2016
Warriors Trained2537
Total Skips107
Total Code Submissions5607
Total Times Completed986
JavaScript Completions436
Python Completions455
Ruby Completions65
C# Completions67
Total Stars94
% of votes with a positive feedback rating94% of 171
Total "Very Satisfied" Votes152
Total "Somewhat Satisfied" Votes16
Total "Not Satisfied" Votes3
Ad
Contributors
  • GiacomoSorbi Avatar
  • alchemy Avatar
  • joh_pot Avatar
  • user5036852 Avatar
  • bidouille Avatar
  • hobovsky Avatar
  • Just4FunCoder Avatar
  • saudiGuy Avatar
Ad