Solution: Compressing ASCII art
- [Instructor] This is the final challenge of the course and not coincidentally, it's also the challenge that has the greatest number of possible solutions. You could do just about anything you wanted to do here and it would probably work. So let's go over three basic types of data compression you might have done here and if you didn't do any of these or a variation on them, awesome. The great thing about data compression is that as long as the data shrinks and then expands back to more or less the original, no one really cares how you got there, but I care. So if you do find something cool, let me know. So the first thing I did was take that JSON blob from encode string and then just write it to a file. And then with decode file, we can open that file name again, read the JSON blob, and then run it through decode string. So if I run this, so the file compression is not the greatest here, went down to 2,441 bytes, but technically we did reduce the file size. So hooray, file compression. The thing you might've noticed is that this JSON blob, and I have an example of it here, it has a lot of characters in it. So if you look at this closed parenthesis comma space open parenthesis quote, that's five characters for what's essentially just a delimiter between a number and the next character. So what if we use delimiters like this, like a pipe or a tilda to sort of shrink up this JSON blob and make it a little bit smaller? So let me get rid of this solution and then I have an example of that right here. So here's something else you might have done, and if we test this, it shrinks it quite a bit to 1007 bytes. So this is pretty good, but can we go even smaller? So let me just get rid of this. And then we have a new solution down here, and that is the byte solution. So earlier in this course we discussed working with bytes. So you can store any integer up to 255 in a single byte of data. So rather than storing the number 255 as a string with three characters, which takes up three bites, you can use one single byte. You store it as an integer rather than a string. And each of these characters in the ASCI art are also one byte or one character or one byte. So if we make sure that we write our file precisely, one character, one number, one character, one number, one character, one number, and then we read it in precisely, the first byte's, the character, the next byte's, the number, et cetera, we can encode and decode this image extremely efficiently. There are no delimiters, we're just depending on the pattern being exactly correct every single time, which hey, that's what computers are really good at. So in this version of the solution here, I've used a few language features that I didn't cover in the course, but if you are interested in writing byte files, file creation, and data compression, I recommend that you play around with these solutions and use them as starting points for future learning. And if you're not excited about the code, that's fine. Check out these results. It gets the file down to 466 bytes. So that's nearly a sixfold reduction in size over the original 2749. Sometimes these concepts of memory and bytes and encodings can seem really, I don't know, useless and academic, but I want to stress that they do have very real and important impacts day-to-day as a programmer, a reduction in file size like this can be real money if you have enough files.
随堂练习,边学边练
下载课堂讲义。学练结合,紧跟进度,轻松巩固知识。