|
| 1 | +--- |
| 2 | +title: Billy's Books |
| 3 | +desc: Determine the order in which Billy stacked his books. |
| 4 | +class: COMP1511 |
| 5 | +difficulty: Medium |
| 6 | +--- |
| 7 | + |
| 8 | +For this exercise, you’ll be working with the following scenario. |
| 9 | + |
| 10 | +Everyday Billy places a book on his floor, with each book being stacked on top of the previous book. On every third day, in addition to stacking a book to the top, Billy also places another book on top of his bookstack (after placing the new book). If there are an odd number of books, then Billy will move the book in the middle of his bookstack to the top. If there an even number of books, then Billy will take the book at the bottom of his bookstack, and move that to the top. He continues this for all N books. |
| 11 | + |
| 12 | +Write a program `billys_books.c` which scans two lines of input: |
| 13 | + |
| 14 | +1. A single integer `n`, which corresponds to the number of books Billy has. |
| 15 | +2. A sequence of exactly `n` characters which corresponds to the order of Billy's final bookstack from bottom to top. |
| 16 | + |
| 17 | +Your program should then output the order in which Billy placed his books. |
| 18 | + |
| 19 | +For example, if billy had placed 11 books in the order ABCDEFGHIJK, then the bookstack would look like this |
| 20 | + |
| 21 | +- Day 1: A |
| 22 | +- Day 2: AB |
| 23 | +- Day 3: ACB (middle book moved to he top) |
| 24 | +- Day 4: ACBD |
| 25 | +- Day 5: ACDBE |
| 26 | +- Day 6: CBDEFA (bottom book moved to the top) |
| 27 | +- Day 7: CBDEFAG |
| 28 | +- Day 8: CBDEFAGH |
| 29 | +- Day 9: CBDEAGHIF (middle book moved to the top) |
| 30 | +- Day 10: CBDEAGHIFJ |
| 31 | +- Day 11: CBDEAGHIFJK |
| 32 | + |
| 33 | +Hence, given `11\nCBDEAGHIFJK` as input, it should correspond to the order in which Billy placed his books: ABCDEFGHIJK. |
| 34 | + |
| 35 | +The output from your program should look **exactly** like this: |
| 36 | + |
| 37 | +```bash:~/1511-revision/billys_books |
| 38 | +$ dcc billys_books.c -o billys_books |
| 39 | +$ ./billys_books |
| 40 | +11 |
| 41 | +CBDEAGHIFJK |
| 42 | +ABCDEFGHIJK |
| 43 | +``` |
| 44 | + |
| 45 | +## Assumptions/Restrictions/Clarifications |
| 46 | + |
| 47 | +- You may assume the first line of input will be a valid positive integer |
| 48 | +- The second line of input will contain exactly `n` characters, followed by a newline character |
| 49 | +- You cannot assume the books are unique (ie. the stack AABBCCDD is valid) |
| 50 | +- You cannot assume a particular book can only be moved once (especially for large input size) |
| 51 | + |
| 52 | +## CSE Autotest |
| 53 | + |
| 54 | +When you think your program is working, you can use CSE autotest to test your solution. |
| 55 | + |
| 56 | +```bash:~/1511-revision/billys_books:~ |
| 57 | +$ 1511 autotest billys_books |
| 58 | +``` |
| 59 | + |
| 60 | +## Solution |
| 61 | + |
| 62 | +You can view the solution code to this problem [here](https://github.com/csesoc/comp1511-revision-t1-2022/blob/master/solutions/billys-books/solution.c)). |
0 commit comments