See all 4 tutorials
The last tutorial left off with a challenge problem. This tutorial contains the answer to that, so if you want to do it, go ahead and try first. With that being said, let's get into the solution to the problem, that problem being to turn every other letter in a string of lowercase letters uppercase.
First, let's identify some tricks we can use. From the last tutorial, we had the
,[ code ,] trick, which put every letter of input through the code. However, we have to do this for every other letter... how? The fix is simple:
,[ code , [-] ,]. With this, after the first letter (not skipped), every other letter is skipped. It's true that every
, overwrites the value in the cell, but if there is no input, some BrainF**k interpreters don't set the cell to 0, so we do it with the
[-] trick just in case.
What do we do for our code, though? For that, we need to look at our ASCII table. Looking at it, you can see that every letter A-Z is in order, and every letter a-z is in order. The difference between a (97) and A (65) is 32, the difference between b and B is 32, and so on. So the only thing we need to do is subtract 32. We know that 32 is 4 * 8, so we can use this code to subtract 32 from a number (which is what we want to do, since we start from lowercase):
> ++++ [ < -------- > - ] <. To re-iterate, this essentially starts a loop that subtracts 8 from the cell 4 times over by keeping track of how many times the loop is run in the cell after the one we are operating on.
Putting it all together, we get
,[>++++[<-------->-]<.,.[-],] Don't forget the period after we subtract 32, to print the letter we have! You also have to put a period after you get the input that you're zeroing out, since you're not performing the code on it.
Example problem 2: more tricks!
This problem can use some tricks that I didn't write in the previous tutorial, but one which many problems can utilize. Here's the problem: given a number (to make it easier, let's say we take the ASCII number of the input, so input
A translates to the number 65), output that many exclamation points and that many equal signs, in any order.
If you'd like to attempt the problem, feel free to do so! This is a really open-ended problem, and can be solved without any new tricks, so I'd really encourage you to try.
Solution 1: no tricks
Solving this with no tricks sounds harder than it is. In reality, we can use the looping trick from earlier, but using the input as our counter. To output the exclamation points (ascii: 33) and equal signs (ascii: 61) we can do one after the other. As such, the code will look like this
, take input into cell 1 [ loop while input is nonzero >> we will use cell 2 to store the exclamation point(comma) so we skip to cell 3 to start another loop (3 * 11) +++[<+++++++++++>-]< this puts 33 into cell 2 and lands on cell 2 . print the exclamation point [-] zero out the cell so we can use it for the equal sign (alternitavely(comma) to make the code shorter(comma) you can just add 28 to 33 to get 51) > go to cell 3 and do the same thing for 61 (6 * 10 (plus) 1) ++++++[<++++++++++>-]<+ put 61 into cell 2 . print the equal sign [-] zero it out to get it ready for the next loop < go back to the counter at the beginning (the input) - subtract one ]
Solution 2: with tricks!
This solution introduces 2 new tricks. The first is to copy a cell into the next two cells: assuming you start on that cell, run
[>+>+<<-]. The next trick is less a code snippet and more something that some of you may have already thought of: we can put the values 33 and 61 into cells at the beginning and go to them to print them, instead of putting them in every time we want to use them. While this doesn't really impact this problem so much, there are other instances where you want to repeat a character in several different contexts, and this trick helps a lot. With that being said, here is the solution 2 code:
>+++[<+++++++++++>-] put 33 into the first cell(period) This lands on cell 2 >++++++[<++++++++++>-]<+> put 61 into the second cell(period) This lands on cell 3 , take input [>+>+<<-] copy input into next two cells (cells 4 and 5) > go to cell 4 [ begin a loop <<<. go back to cell 1 and print the exclamation point >>>- reduce the counter ] > go to cell 5 [ begin a loopo <<<. go back to cell 2 and print the equal sign >>>- reduce the counter ]
With this solution, you have
!!!!...====... instead of
!=!=!=!=..., like in solution 1.
With that, this tutorial comes to a close. Challenge problem: for each character in the input, write that character the amount of times equal to that character's ascii value. For example, the input
AB would result in 65
As followed by 66