Implement a simple but effective compression scheme for short, simple strings
Make sure you have a Github account, and setup your public and private SSH keys for password-less authentication from your local machine to Github.
- Fork this Github project to your your own account. Then, clone your fork to your local machine.
git clone git@github.com:[your-github-username]/hw-data_packer.git- Set your local ruby version for the project folder using
rbenv
e.g., for Ruby 3.4.2:
rbenv local 3.4.2
This creates a .ruby-version file in the root of your project directory.
- Run
bundle installto install the package dependencies listed inGemfile. After bundling, you should see aGemfile.lockfile appear in your project directory.
In this assignment, you must write the code needed within two methods found in the file short_string_packer.rb:
- The
pack(str)method must take a string calledstrand return an Integer - The
unpack(packed)method must take an Integer calledpacked, decompress it and return the string that was originally compressed
Types:
str: A short String that only contains lowercase characters 'a' - 'z' (no spaces, digits, uppercase, symbols, etc.)packed: An integer of type Integer
Methods:
pack(str): Realize that the 26 letters 'a'-'z' don't need to use all the bits available in a UTF-8 character. You can use this to compress a string by packing the bits of each character together:- take each letter of
str, convert it to a number from 1-26 - take the bits of each number have created and shift them appropriately, and use a binary operation to pack them together into a single Integer called
packed
- take each letter of
unpack(packed): Reverse the process above by accepting a packed Integer and returning the reproduced String
- Do not use any external libraries (gems)
- Do not write code anywhere outside of the two methods of
short_string_packer.rb - Do not use any for/while loops. ONLY use functional iterators:
.map,.each,.reduceetc. - Run
rubocopon your code to ensure that it is formatted correctly
Test your solution by running the test specification file:
ruby packer_spec.rbYour final solution should see all tests passing:
$ ruby packer_spec.rb
Run options:
# Running:
.......
Finished in 0.001401s
7 runs, 7 assertions, 0 failures, 0 errors, 0 skipsOn Canvas, submit a URL to your git repository.