This document is the preface to a book titled "String Based Programming Puzzles Solved" which contains solutions to 28 programming puzzles involving string manipulation. The preface provides an overview of the types of puzzles covered in the book, which range from simple pattern generation to more complex problems involving encryption, dictionaries, and boards/mazes. It is intended as a resource for students preparing for programming competitions. The puzzles are solved in C programming language and explanations are provided for the logic and approaches used.
1. 28
String Based Programming Puzzles Solved:
Join us to win Informatics Olympiad to ICPC
N.B. Venkateswarlu
AITAM, Tekkali
2. Copyrightsยฉ2016
Exclusive Rights are with Author.
No part of this publication may be reproduced, stored in a retrieval system or transmitted in any
form or by means of electronic, mechanical, photocopying, recording, scanning or otherwise
except with the permission of author.
5. Preface
Majority of programming competitions, interviews, examinations will be evidently having
substantial number of questions that involves strings. This is true with Informatics Olympiad,
ICPC, and other challenging competitions.
This book comprises 28 (256) selected questions that are asked in various worldwide
programming contests and demands string manipulations. We have supplied solutions along with
explanations including programโs working snap shots. Some of the selected problems uses
adhoc logic while others uses popular data structures. We have organized the book such that
beginning examples are little demanding compared to last examples such that a novice student
can start improving his logical and programming abilities in incremental manner by solving
illustrated problems. However, we donโt claim that our solutions are always optimal. Also, in
reality we donโt want to claim that one problem is difficult and the other is easy as the adhoc
logic what we have used may be inferior to others. However, we have thoroughly tested every
problem with various test cases. We are of the opinion that the student can explore the possibility
of identifying better solution than ours. Some of the problems are having some questions posed
by us. We advise the reader to answer them to build their programming abilities.
We have included enough explanation to make the readers understand the adhoc logic which we
have employed in solving some of the problems. We have also included figures wherever
needed to explain the logic which we have employed in solving the problems. We have given
complete details of the method which we have employed to solve a problem such that readers
can solve similar problems in an easy manner.
The examples given in this book are borrowed from various programming competitions of
various levels high school to college. All the examples discussed here can be grouped as:
๏ท Simple String based puzzles solved
๏ท Pattern Generation & Animation based puzzles solved
๏ท Little More involved string based puzzles solved
๏ท Palindrome Related Puzzles Solved
๏ท Permutation based string puzzles solved
๏ท Arithmeticโs based string puzzles solved
๏ท Card and Deck based puzzles
๏ท Board and block based string puzzles solved
๏ท Encryption based puzzles solved
๏ท Dictionary & Editor oriented puzzles solved
๏ท Miscellaneous & Complex string based puzzles solved
Beginning examples includes generation of various character based patterns. These examples are
aimed at enriching reader towards loop (iterative) building abilities. That is, these examples
progressively enlightens reader about nested for or while loops. We strongly believe that these
examples can be very much useful in introductory level programming oriented courses of any
programming language.
This book embodies three to four dozens of puzzles related to security and encryption. We
believe these novice examples are very much useful to develop interest to Computer Science
6. students on data security. This book also contains many examples related data compression and
data coding.
This book contains many examples related to board and maze. By solving these examples, reader
gets equipped with use of stack, back tracking, etc. Moreover, this book accommodated many
examples that demands practical use trees and their traversals.
Many illustrious examples are solved to let Computer Science students to have practical
exposure and introduction to Computer Science theme areas such as DNA matching, tries,
dictionaries, plagiarism, web mining, regular expressions, compiler construction, link extraction,
editor design, random numbers, virus detection, Geomatics, Geometry, Graphics, Genetic
algorithms, number systems, cross words, etc.
All the solutions are given in C programming language. We presume that the reader has exposure
to C programming language elements and enough exposure to writing programs. For those
people who feel that they only know the language and have not yet developed their logic skills,
first we advise them to read the reputed C programming books. Also, illustrates solutions are
written such that they can be understood by the readers who does not have sound foundation in C
language but are exposed to other programming languages. All the programs are tested and
developed under Bloodshed (www.bloodshed.net) Dev C++ integrated development
environment (IDE) which uses GCC compiler tool chain.
We believe this book is going to be very useful for those students who are preparing for IOI,
ICPC, Challenge24, Microsoft Cup, Aspirations of Infosys, and others. Also, we are of the
opinion that this will be very useful for campus recruitment tests (CRT) conducted by Google,
Face Book, Amazon, Microsoft, Wipro, Yahoo, etc.
--Author
7.
8. Acknowledgments
I am very much indebted to studentโs of current generation, especially students of AITAM.
Also, I am thankful to management of AITAM for giving free time for scribbling this horrendous
sized book. I do would like to acknowledge the time spent by Dr. Garimella V Saradamba for
proof reading the book.
--Author
9.
10. Contents
1.Simple String base puzzles solved
1. Dedupe 1
2. Most Popular letter 3
3. Krunching Strings 4
4. Exchange the words before and after AND 6
5. The String Difference 8
6. Isograms 10
7. Countdown 13
8. Shy Tortoise 15
9. Word Swap 18
10. Base Alphabets 20
11. Alphabetically Speaking 23
12. Common Letters 24
13. Weighing Strings 26
14. Balanced Words 28
15. Name That Number 31
16. Telephone Number Helper 33
17. Word Shifter 36
18. Obedience School 37
19. Gone Phishin 40
20. Fire! Fire! Fire! 42
21. Letter Game 45
22. Birds and Shooters 48
23. M&M Factory 50
24. Squishing the string 53
25. Dashes Removal 55
26. Family Concepts 56
27. Russel Soundex Index 58
28. FOO FIGHTER 62
29. Multiprocessing output string validity 63
30. A String Decompression 65
31. Text Compression 68
2.PatternGeneration& Animation basedpuzzles solved
32. Drawing A 70
33. Triangle with a string 72
34. Word in a triangle 73
35. Word in a triangle 74
36. Word As a Triangle 76
37. Tent House 79
38. Egg Timer 80
39. Dressing Up 81
40. Trident 83
41. Word Diamonds 85
42. Consecutive Diamonds 88
11. 43. Triangles 89
44. Tower Building 91
45. Diamond inside a box 93
46. The Moving X 95
47. STOP Figure 98
48. Arrows 101
49. Square Designs 108
50. Target Practice 111
51. Bar Chart 113
52. Patterns 114
53. Vertical Histogram 116
54. Divided Fractals 118
55. Hilbert Hack 126
56. 0123456789 133
57. Bit-Block Characters 137
58. Itโs a plot 139
59. Snake 142
3. Little More involved string basedpuzzles solved
60. Code finder 147
61. Acronyms 149
62. Word Construction 151
63. WORD Frequency 153
64. Word Stats 157
65. Word Morphing 161
66. Largest Repeating Prefix 165
67. String Subsets 167
68. Plagiarism 169
69. Letter Grade Assignment with Leniency 172
70. Journey to Batlantis! 175
71. Fishy Business 178
72. Shuttle Puzzle 180
73. Word Problems 185
74. Contact 188
75. Too Many Keys 195
76. Wheebop! 199
77. Fruit Loops 202
78. These Arenโt The Droids Weโre Looking For 203
79. Check-ing Hai-ku 206
80. Word Chain 209
81. Letter Selection 212
82. All the sub-strings of a string 217
83. Character Merged Strings 220
84. Smallest Substring in alphabetical order 223
85. Broken Necklace 228
86. Visual Acuity 233
87. Back Seat Driver 236
12. 88. Weighting Strings 239
89. Teams 241
90. String Transformations 243
91. Reversing Words 244
92. BigBucksIn 246
93. Sentences from parts 248
94. Tournament Winner 250
95. Fifol's Orders 253
96. Who's calling Pete? 256
97. Virus Detection 259
98. Hangman Judge 261
99. Score Update 265
100. Win or Lose 268
101. The Magic of Words 272
102. Greedy Gift Givers 275
103. n-strings 277
104. Monotony 281
105. Garbage In, Garbage Out 283
106. Game, Set, Match 285
4. Palindrome RelatedPuzzles Solved
107. Palindromes 289
108. Longest Palindrome 291
109. Palins 293
110. Palindrome Compression 296
111. A String Manipulation Problem by any Other Name... 298
112. Palmost 300
113. Aplindrome (aplindro) 304
5. Permutations based puzzles solved
114. Partial Primes 307
115. Commutation 309
116. Restrung 312
117. Permute Commute 317
118. ID Codes 319
119. Fifteen 322
6. Arithemetics basedpuzzles solved
120. Letter Arithmetic 324
121. When in Rome 325
122. Censor 329
123. Adding Hex Numbers 331
124. Spoken Numerals 335
125. English to Integer 338
126. Spoken Addition 341
127. OOPS! 345
128. Majoring in Scales 349
129. Kibbles 'n' Bits 'n' Bits 'n' Bits 355
7. Card and Deck basedpuzzles
13. 130. The Psychic Poker Player 360
131. Krypton Factor 374
132. Sequence Checking 379
133. Accordian patience 381
134. Miss You! 385
135. Pound in the Shuffle 390
8. Boardand block based puzzles solved
136. So, Who Won? 393
137. Where's Ali? 395
138. SOS 397
139. Defense Line 400
140. A Planimeter Problem 407
141. Find X 412
142. Reversi 415
143. Window Area 418
144. Frame Up 428
145. Biggest Barn 435
146. Storage Center 440
147. Meet in the Middle 444
148. Toggle 448
149. Allowance or Aquarium? 453
150. Nine-Piece Puzzle 461
151. Starry Night 469
152. Telling Continents Apart 476
153. Squareland 479
154. Fish Tank 487
155. Inspector Doohickey 494
156. Cross Words 501
157. Population Explosion 507
158. Marvelous Mazes 513
159. Masterminds 515
160. On The Road Again 518
161. Alarm Chain 522
162. Go For It: Connect 4 525
163. Yomoku 529
164. Whereโs Waldrof? 531
165. Word Walk 536
166. Walkabout 539
167. Life 546
168. Scrambled Flavors 550
169. Ice Cream Fad (fad) 554
170. Ice Cream Grids (rotate) 558
171. Smallest Tile 561
172. TRIANGLES 564
173. Chain Reaction 570
174. Skewways crosswords 574
14. 175. Othello Moving 579
176. Scrambling Words 586
177. Rock Paper Scissors โฆ Lizard Spock?? 591
178. Islands Smaller Than Rhodes 597
179. Transformations 601
180. Algrbraic Socks 607
9. Encryption basedpuzzle problems solved
181. Oh only have Oh's for a 615
182. Vicksburg Cipher 616
183. Zombie Spellcheck 621
184. Zzzyyx or Iโm Out 626
185. Decoding 632
186. What's Cryptanalysis? 634
187. CSBAAOERX 637
188. What is on the Grille? 640
189. Boole Ciphers 645
190. Help me read Paulโs messages 649
191. Encoding Grid 653
192. Decoder 657
193. Dynamic Dictionary Coding 659
194. Encoder/Decoder for text strings 662
195. The Mystical Cereal Box Decoder 668
196. Code Decryption 671
197. Caesar's Challenge 677
198. Super Encryptor 681
199. Ring Tone Composing 684
200. Pirate Talk 686
201. Let's Talk Zombie 688
202. Garbled Message 693
203.Tongues 695
204. Secretly Lost 698
205. Nihongo no AI (Japanese Language AI) 700
206. Decoder 702
207. Autokey Dyvfitrhh 703
208. Encoder and Decoder 706
209. Key to Success 710
210. Password Cracker 714
211. Good Passwords 716
212. TALES FROM DECRYPTION 719
213. BEJMUL (JUMBLE) 728
214. Eakspay igpay atinlay? 733
215. Decoding ASCII 736
216. Professor Morse's Code 739
217. Wippi Leaks 743
218. Prime Cryptarithm 747
10. Dictionary & Editor oriented puzzles solved
15. 219. Justifying Text 749
220. Insert/Delete 752
221. The Year of the Missing Semicolons 754
222. Pretty-Print Expressions 757
223. Pretty-Print Expressions with Error Checking 759
224. Quotations Extraction 763
225. A Quotation within a Quotation ... within a Quotation 766
226. Dictionary 772
227. Runny Babbit 777
228. Columnar Tabulation. 784
229. Searching articles 786
230. Formatting with hyphenation 790
231. AmeriCanadian 793
232. Directory Wild Cards 795
233. Whoops 798
234. Dictionary 801
235. Building Blocks 803
236. The Tokenator 807
237. Word Counting 810
238. Positive Sentences 812
239. Vhat's fur Dinner? 813
240. Vocal Punctuation 819
241. Removal 821
242. Primitive Typewriters 822
243. Translation 825
244. Simple Compliment Convertor 829
245. The HBS Language 831
246. Year 2000 833
247. Poetry 840
248. Identify yourself 848
249. Rare Order 851
11. Miscellaneous & Complex String basedpuzzles solved
250. The Errant Physicist 853
251. Boxes 865
252. Balancing chemical equations 869
253. Revelation 888
254. The Spaghatta Naddle Synthesizer 894
255. Dr. Morse's Dragon Parade 898
256. The Array Reloaded 900
12. Bonus Problems
257. Illegal Hawaiian Virus Collection 903
258. Hope Chest Gone Bad 908
259. Genetic Algorithms 911
260. Genetic Sequencing 913
261. Secret Judging League 916
262. Waiting for Change 920
16. 263. Crushing Confections 924
264. Missionaries and Canniballs 927
265. The DNA string. 930
266. Tree Recovery 931
267. Nasty virus 937
268. Numeric Dictionary order 939