# Practice Problems in LOLGraphics/Printable version

The current, editable version of this book is available in Wikibooks, the open-content textbooks collection, at
https://en.wikibooks.org/wiki/Practice_Problems_in_LOLGraphics

# Preface

This book is intended for readers who have finished reading Programming in LOLGraphics 3.4, and would like to practice their skills. In every chapter, the reader will be faced with a problem that needs to be solved in LOLGraphics, and then he will see the solution with detailed explanations on how the code works an which parts of it can be written differently. The book assumes that you are already familiar with how the language works. If you are not, please read “Programming in LOLGraphics 3.4”.
The book will focus on the journey of Namer the cat to the Kingdom of Cats. At every state of the journey he will be presented with a challenge which he has to solve using the LOLGraphics 3.4 programming language.
IMPORTANT: Just because your solution for one of the problems is different from the one presented in the book doesn’t mean that it’s wrong. There are multiple ways to solve every problem in the book which is why after every solution there’s also a list of alternative ways.

# Problem I - Calculator

Namer is a street cat from Beer Sheva, Israel. Like all street cats, it’s his dream to reach the Kingdom of Cats - a mansion with a beautiful garden on a floating island with everything a cat can ever wish for. However, not every cat is eligible to live in the Kingdom of Cats - cats have to first pass a test in LOLGraphics to be allowed to even enter the Kingdom. A cat that enters the Kingdom will feast with the King of Cats and then will pass another test in order to join the Order of the Tiger. Only members of the Order are allowed to live in the Kingdom of Cats.
Each year, a hot air balloon only visible to cats departs from a random city in the world. This year it departs from Istanbul. Namer left Beer Sheva towards the sea where he met a cat with a boat. The cat said that he doesn’t know how to program, and that he will take Namer free of charge if he programs a calculator in LOLGraphics for him. He also gave a page which reads as following:
   If you program a calculator in LOLGraphics for me, I will take you to Istanbul for free. My requirements for the LOLGraphics calculator are as following:
   First, the calculator should ask the user to enter two numbers, and then a mathematical operation (addition, subtraction, multination, division), and then it will print the result. For division, since LOLGraphics doesn’t support fractions, I want to see the integer result and the reminder. For 99/3 it will print 33 and 0, for 15/7 it will print 2 and 1.

# Solution to Problem I
## Code

IMPORTANT: The subprograms minus and subtract are not the same! Minus is for subtraction operation, and subtract is used for division because division is broken in LOLGraphics.

## Explanation

This code can be easily divided into 3 parts:
* The first part of the code asks the user to enter 2 numbers and stores them in 2 4-byte variables called x and y. Note that LOLGraphics doesn’t have scopes - once you define a variable it exists forever until the program ends. That’s why there’s no problem in defining variables in the subprograms where they are first used.
* The second part of the code asks the user to enter the mathematical operation that he wishes to do. Because LOLGraphics doesn’t let you ask for string inputs - only number inputs, each one of the 4 basic arithmetic operations is given a number from 1-4.
* The third part of the code checks what operation the user entered using a switch/case structure and calculates the result of the arithmetic operation. Addition, subtraction, multiplication, and division are straight forward so I will focus on the division routine. Because division seems to be broken in LOLGraphics, I had to implement it myself. Ideally, the code would just keep subtracting Y from X in a loop while X>=Y but this simply isn’t possible. The code does it while X>0, but that means there’s a chance it will subtract to below 0, that’s why it after that has to check that X is not negative, and if it is correct the answer by subtracting 1 from Z, and Y to Z. After that, Z will store the integer part of the solution, and X the reminder, and both will be printed.

## Alternative ways

There are multiple things that can be done differently. For one, if you run this code you will see X=5. You can theoretically add spaces that it will be X = 5, by remember that since the LOLGraphics interpreter wastes spaces from the beginning and end of every line, so to print the second space you will need to use the command PLZ ADD A SPACE.
Also, it’s possible to print to the user what operation is calculating. Since the code is in any case running with conditions, and each operation has a subprogram, the best way would be to add in each such subprogram a print command. PLZ PRINT TEXT + for the addition subprogram, etc.
Also it’s possible to implement the division routine differently. This code will subtract Y from X while X>0. This means that it’s possible that it will stop at X=-2 for example and that the result will be of so there’s a subprogram that checks and corrects if necessary. You can also subtract Y from X while X>Y but take into account that if X can be evenly divided by Y, then your answer will be lower by 1 then the correct answer, so you will still need to run a correction subprogram.

# Problem II - Greek Numerals

Namer has successfully written a calculator in LOLGraphics and got a free ride to Istanbul. Near the port, he met a cat from the Order of the Tiger who said he will tell the way to the launch location if Namer can write a program in LOLGraphics that converts Arabic Numerals to Greek Numerals. He also gave a page that reads as following:
   In order to prove that you are worthy to see the hot air balloon of His Majesty King Battlecat III, you need to first write a code that converts Arabic Numerals to Greek Numerals. Here is a table of Greek Numerals (there are multiple variations, we will be using the modern one for typability):
   | Arabic | Greek | Greek | | | Arabic |
   | 1 | Αʹ | 10 | Ιʹ | 100 | Ρʹ | 1000 | ,Α |
   | 2 | Βʹ | 20 | Κʹ | 200 | Σʹ | 2000 | ,Β |
   | 3 | Γʹ | 30 | Λʹ | 300 | Τʹ | 3000 | ,Γ |
   | 4 | Δʹ | 40 | Μʹ | 400 | Υʹ | 4000 | ,Δ |
   | 5 | Εʹ | 50 | Νʹ | 500 | Φʹ | 5000 | ,Ε |
   | 6 | Ϛʹ | 60 | Ξʹ | 600 | Χʹ | 6000 | ,Ϛ |
   | 7 | Ζʹ | 70 | Οʹ | 700 | Ψʹ | 7000 | ,Ζ |
   | 8 | Ηʹ | 80 | Πʹ | 800 | Ωʹ | 8000 | ,Η |
   | 9 | Θʹ | 90 | Ϟʹ | 900 | Ϡʹ | 9000 | ,Θ |
   So the number 7777 will be written ,ΖΨʹΟʹΖʹ. Assume the number is smaller than 9999 but bigger than 1.

# Solution to Problem II
## Explanation

This code is pretty easy to understand. First it check is the number more than 8999 (which is the same as more than or equal to 9000 since LOLGraphics only supports integer numbers). If it is, it types “,Θ” to the screen since this is the Greek numeral for 9000 then subtracts 9000 from the number. Then it repeats the process for 8000, 7000, ..., 2000, 1000, 900, 800, ..., 200, 100, 90, 80, ..., 20, 10, 9, 8, ..., 2, 1. Notice that when it gets to 1, the code checks is the number greater than 0.
One more important thing to put attention on is the delay between two commands. The default is 100 milliseconds (0.1 sec) but since there are many checks, it was reduced to 1 millisecond (0.001 sec).

## Alternative ways

There is another more complex way to write the same code. First divide the number by 1000, then check if it’s a digit 1-9 (not 0), print the relevant character. Then divide the number by 100, but notice that then you will have to subtract 10 from the number until you get a number smaller than 10 which makes this way extremely inefficient. Notice though that since LOLGrapics only supports integer numbers the result of 9999/1000 will be 9 not 9.999 or 10 like you would expect. Readers familiar with programming will recall that other programming languages also take only the integer part when dividing integer variables.

# Problem III - Byzantine Calendar

Namer has successfully written a program in LOLGraphics that converts numbers to Greek Numerals, and the cat from the Order of the Tiger has led him to the location of the hot air balloon that belongs to the Kingdom of Cats. There were other cats, and each got a computer and a page. The page read:
   Congratulations for reaching this far! Only one of you can get to the Kingdom of Cats, however, and in order to determine which one, each of you will try to write a program in LOLGraphics and the one who finishes first will proceed. Because we are standing on the site of former Constantinople, the problem is to write a code that converts Gregorian dates to Byzantine dates. The program will ask the user to enter the numbers of the current year, month, and say, and the program will print the year, name of month, and day according to the Byzantine Calendar.
   The Byzantine Calender is 13 days behind the Gregorian Calender so Byzantine January 1 is Gregorian January 14. Also the Byzantine New Year is on September 1 (Gregorian September 14) not January. Another thing to put attention on is the years. Gregorian September 1, 2027 is Byzantine August 19, 7525. Gregorian September 14, 2027 is Byzantine September 1, 7526.

# Authors & Contributors

This list is updated automatically by AuthorsAndContributorsBot. Please do not update it manually.
* Gifnk dlm 2020 (discuss · contribs · count · logs · block log · rfps · rights [change])
* L10nM4st3r (discuss · contribs · count · logs · block log · rfp · rights [change])
* Slava Ukraini Heroyam Slava 123 (discuss · contribs · count · logs · block log · rfp · rights [change])