(This page has no text content)
i Beginner’s Guide to Code Algorithms
iii Beginner’s Guide to Code Algorithms Experiments to Enhance Productivity and Solve Problems by Deepankar Maitra
iv First edition published 2022 by CRC Press 6000 Broken Sound Parkway NW, Suite 300, Boca Raton, FL 33487- 2742 and by CRC Press 2 Park Square, Milton Park, Abingdon, Oxon, OX14 4RN CRC Press is an imprint of Taylor & Francis Group, LLC © 2022 Deepankar Maitra Reasonable efforts have been made to publish reliable data and information, but the author and publisher cannot assume responsibility for the validity of all materials or the consequences of their use. The authors and publishers have attempted to trace the copyright holders of all material reproduced in this publication and apologize to copyright holders if permission to publish in this form has not been obtained. If any copyright material has not been acknowledged please write and let us know so we may rectify in any future reprint. Except as permitted under U.S. Copyright Law, no part of this book may be reprinted, reproduced, transmitted, or utilized in any form by any electronic, mechanical, or other means, now known or hereafter invented, including photocopying, microfilming, and recording, or in any information storage or retrieval system, without written permission from the publishers. For permission to photocopy or use material electronically from this work, access www.copyright.com or contact the Copyright Clearance Center, Inc. (CCC), 222 Rosewood Drive, Danvers, MA 01923, 978- 750- 8400. For works that are not available on CCC please contact mpkbookspermissions@tandf.co.uk Trademark notice: Product or corporate names may be trademarks or registered trademarks and are used only for identification and explanation without intent to infringe. Library of Congress Cataloging‑in‑Publication Data Names: Maitra, Deepankar, author. Title: Beginner’s guide to code algorithms : experiments to enhance productivity and solve problems / Deepankar Maitra. Description: First edition. | Boca Raton : CRC Press, [2022] | Includes bibliographical references and index. | Summary: “This book takes you on a problem-solving journey to expand your mind and increase your willingness to experiment with code”– Provided by publisher. Identifiers: LCCN 2021044911 (print) | LCCN 2021044912 (ebook) | ISBN 9781032080642 (hbk) | ISBN 9781032102382 (pbk) | ISBN 9781003214335 (ebk) Subjects: LCSH: Computer algorithms–Amateurs’ manuals. | Application software–Development–Amateurs’ manuals. | Computer programming–Amateurs’ manuals. Classification: LCC QA76.9.A43 M34 2022 (print) | LCC QA76.9.A43 (ebook) | DDC 005.1–dc23 LC record available at https://lccn.loc.gov/2021044911 LC ebook record available at https://lccn.loc.gov/2021044912 ISBN: 978- 1- 032- 08064- 2 (hbk) ISBN: 978- 1- 032- 10238- 2 (pbk) ISBN: 978- 1- 003- 21433- 5 (ebk) DOI: 10.1201/ 9781003214335 Typeset in Times by Newgen Publishing UK Additional eResources for this book are available for download at: https://www.routledge.com/Beginners-Guide-to-Code-Algorithms-Experiments-to-Enhance- Productivity/Maitra/p/book/9781032102382
v For my parents Mr. Priyanath and Mrs. Geeta Maitra Dr. Amiya Prosad and Mrs. Durga Majumdar
vii vii Contents Preface .......................................................................................................................xi Acknowledgments ...................................................................................................xiii Author Biography ....................................................................................................xv Introduction .................................................................................................................1 Chapter 1 What Is an Algorithm? .........................................................................5 Chapter 2 Build Your Own Game with a Simple Algorithm— Tic Tac Toe ........13 2.1 How to Get Started ...................................................................13 2.2 How to Get Warmed Up ...........................................................16 2.3 How to Build the Screen (UI, User Interface) .........................19 2.4 Building a “Game” from a “Widget” .......................................22 2.5 Understanding the Need for an Algorithm ...............................28 2.6 How to Test Drive Your New Creation .....................................32 2.7 Conclusion ...............................................................................34 Chapter 3 Explore Your Deductive Logic— Solve a Sudoku Forever .................35 3.1 The Basic Code for solving a Sudoku ......................................38 3.2 Iterative build of CantBeList ....................................................41 3.3 Algorithms that a computer can solve better than humans ......42 3.4 3 by 3 grid interaction ..............................................................45 3.5 Love-Locked Pair .....................................................................49 3.6 The Golden Triangle ................................................................53 3.7 The Polyomino .........................................................................56 3.8 Matching Twins ........................................................................60 3.9 Conclusion ...............................................................................67 Chapter 4 Introduction to Multiplatform Integration— Build Your Own Remote Control .........................................................................69 4.1 Connecting the smartphone to the laptop – virtually ...............70 4.2 Settings in Powerpoint .............................................................71 4.3 Standard Functions ...................................................................73 4.4 Reading the Googledocs input .................................................73 4.5 Taking over Keyboard Controls ...............................................78 4.6 Conclusion ...............................................................................80
viii Contents viii Chapter 5 The Organizer— Build Your Own Virtual Filing Cabinet ...................81 5.1 The Algorithm ..........................................................................81 5.2 Invoking the code .....................................................................87 5.3 Conclusion ...............................................................................87 Chapter 6 Merging Sheets— Combine Multiple Workbooks of the Same Format into One Workbook Automatically ..............................89 6.1 Algorithm for merging sheets ..................................................91 6.2 User Interface ...........................................................................91 6.3 Error Correction .......................................................................95 6.4 Heavy Lifting ...........................................................................95 6.5 The Pickfile subroutine ............................................................98 6.6 Conclusion ...............................................................................98 Chapter 7 Introduction to Graphs— Create Your Own Interface Diagram Instantly .............................................................................101 7.1 The Algorithm ........................................................................101 7.2 Conclusion .............................................................................116 Chapter 8 Shaping up— Analyze a Picture and Document Its Components in Text ..........................................................................119 8.1 The Algorithm ........................................................................119 8.2 List of Shapes .........................................................................121 8.3 Conclusion .............................................................................127 Chapter 9 Real- Time Currency Conversion— An Introduction to Simple Web Scraping Techniques ................................................................129 9.1 Algorithm ...............................................................................129 9.2 Understanding the HTML ......................................................130 9.3 Output field ............................................................................131 9.4 The Algorithm ........................................................................132 9.5 Conclusion .............................................................................134 Chapter 10 The Genius of Collaboration— Build a Burglar Alarm Using a Free Webcam Application ..............................................................135 10.1 Ideas .......................................................................................136 10.2 Burglar Alarm ........................................................................137 10.3 Counting People .....................................................................137 10.4 Remote Photo .........................................................................138 10.5 Time based greeting ...............................................................138 10.6 Conclusion .............................................................................140
ixContents ix Chapter 11 Advanced Graphics— Complex Visualizations and More ................141 11.1 Features of the graph ..............................................................144 11.2 The data ..................................................................................144 11.3 The User Interface ..................................................................145 11.4 The Bar Chart .........................................................................145 11.5 The Map Chart .......................................................................146 11.6 The Algorithm for the Chart ..................................................146 11.7 Building the map ....................................................................152 11.8 Conclusion .............................................................................158 The Final Word ....................................................................................................159 Figures ...................................................................................................................161 Bibliography .........................................................................................................163 A Little Bit of Computer Science ........................................................................165 Index ......................................................................................................................171
xi xi Preface People talk about AI, VR, AR, etc. these days. Each term has a deep scientific meaning and sociopolitical connotation for the user of these words and the people they are dir- ected to. It is simply amazing to see how loosely these terms are used and interpreted today. For example, the A in the AI stands for artificial. While people generally under- stand that AI is artificial intelligence, I have come across a variety of ways that people perceive “artificial”. Is it the capability of a machine to figure out a problem? Or is it an ability for a device to do a task? The fact is neither— AI is a computer science term intended to mean the ability of machines to develop “intelligence” much the same way as humans— the capability to “learn”. In this book, I want to introduce a new concept— augmented common sense (ACS). This is in effect the opposite of AI in some sense— it is the ability of humans to develop common sense with the help of machines! Simple concept. Powerful impli- cations for your specific universe— I deliberately chose the word “universe” because I did not want to limit the vast number of situations the average human goes through. Work. Leisure. Entertainment. Exercise. Nurture. Innovate. Contribute. These are all situations that require humans to apply their common sense. The materials presented here will help you to strengthen your logic and develop your ability to write simple programs to automate your tasks. While there are innu- merable ways that a human endeavor can be improved, I will be discussing how this can be achieved through the use of spreadsheets. You will need: • A computer • Microsoft Office (any version) • Your basic skill in working with a spreadsheet • Your desire to learn programming The last one is scary for some and easy for others. This book is intended for those who want to be inspired to develop their ACS through programming and discover the beauty of logic— the happiness of solving a riddle. After reading this book, I hope to have elevated your appreciation of program- ming. But that is just a side effect. My main objective is to entertain. To help you see the beauty of common sense and to appreciate how simple computing can make mundane tasks interesting. I am going to begin with a discussion on algorithms and quickly transition into some real- life examples that are both entertaining and useful. Starting with a program on how to play Tic Tac Toe with the computer, I am going to describe a few other interesting problems hoping to inspire you to try your own. The last chapter is a more complex discussion on writing a program to draw a COVID graph of USA— devel- oped from the common question that we found ourselves hurled into in 2020, during the tough pandemic days.
xii Preface xii Albert Einstein had two famous quotes. Two things are infinite: the universe and human stupidity; and I’m not sure about the universe. – Albert Einstein I am enough of an artist to draw freely upon my imagination. Imagination is more important than knowledge. Knowledge is limited. Imagination encircles the world. – Albert Einstein This book is to help you discover the world of smart applications through the power of your limitless imagination. Let the fun begin!
xiii xiii Acknowledgments I am grateful to Dr. Anil Bhowmick for his continued encouragement and support. I thank Dr. Ananda Datta, Venki Krishnamoorthy, and Satish Badgi for their con- structive comments and suggestions. I want to recognize my daughters— Aparajita for the illustration in Chapter 10 and Amrapali for reviewing the initial chapters. I appreciate my son- in- law Lahiru for the infinite snacks and granddaughter Anika for her inspiring smiles. For editing my manuscript and keeping my dream alive— I thank my wife Saswati.
xv xv Author Biography Deepankar Maitra is a leader in business software solution architecture, focused on Human Capital Management. For the past 30 years, he has managed the implemen- tation of enterprise applications in multinational corporations, providing digital solu- tions to a userbase of over 2,000,000. A valedictorian and a National Talent Scholar, Deepankar is devoted to helping the new generation of consultants demystify technology and simplify complex business scenarios. His philosophy of creating impact through knowledge- sharing has evolved into teaching courses, presenting at international conferences, and developing guides for technology professionals. Deepankar’s consulting journey started in New Zealand, where he worked with leading business software vendors to enhance their products. His algorithmic solu- tions were implemented by several industries such as airlines, improving low lead time aircraft maintenance, repair, and overhaul processes. A graduate of the premier Indian Institute of Technology, Deepankar has a passion for solving the world’s most complex riddles and believes in practicing brainteasers for mental fitness. Through this book, he wants to inspire readers to tap into a world of possibilities using code. Deepankar currently lives in Houston, TX and works as an executive in a leading international consulting firm. newgenprepdf
1 1DOI: 10.1201/9781003214335-1 Introduction The earth rotates around its axis every 23 hours, 56 minutes, and 4 seconds. We call this a day and then, before we know it, the calendar automatically changes its date to the next day. A day is never enough to get everything done! If we pause for a moment and think about the tasks we could not complete, quite often it is because they are repetitive and high in volume. Repetitive tasks throw a damper to our spirits— espe- cially if they are numerous. A task that needs to be done again and again is no longer interesting, and mentally we are ready to move on to something different, even if it is simply to relieve the drudgery. Take for example an analyst who needs to compile a financial report with 600 lines of amounts in a single currency. Yes, the currency conversion table is available in a spreadsheet, but the rate changes every day. The analyst needs to do this boring task of downloading the rate from a website and fitting it into a table, adjusting “vlookups” and confirming that the amounts are all converted correctly— manual tasks have chances of error and the only way to assure their accuracy is through spending even more time checking them. Take another example where a survey is launched and the budget inputs for dif- ferent departments all come in separate forms— there is no quick way to merge them into one spreadsheet although the formats are the same. We have a robot. We can use the robot to do a great deal of the repetitive work. If only we knew how to train it! Yes, I am talking about the laptop we own. It has come a long way from the first computer ENIAC. Invented in 1943, it had 18,000 vacuum tubes and occupied 1,800 square feet of space. The modern- day laptop takes the speed of ENIAC from about 400 instructions per second to several billion— that is quite impressive in terms of speed. But what good is the speed if we cannot use it to solve our daily problems and reduce the time we spend on our boring tasks? We have our speedy computer but how do we harness its power? How do we explain to our speedy computer— the untrained robot— the problems we want help with? This is the theme of the book. How to train your robot by a combination of common sense, algorithmic skills, and a bit of coding. And have some fun along the way! You will find immense joy in creating your ideas and helping others with the code you develop! This book is not about teaching you the syntax of coding— for that, there are man- uals available online that you can refer to. (I have provided some links at the end of this book.) Unlike a traditional computer programming book, I have covered the essential programming techniques in simple and fun ways for you to experiment and learn. The techniques are by no means exhaustive, but sufficient for you to get inspired, learn coding, and enjoy the process! They are explained in an increasing order of complexity to help you develop your skill of coding an algorithm in progres- sive steps.
2 Introduction 2 Throughout the book, the emphasis is for you to find a practical solution by using simple concepts and reusing code that you will find in this book. You will find here working code for several interesting scenarios and you can use these as building blocks for your own personalized solutions. Chapter 1 introduces the concept of algorithms and presents this learning as the heart of all computing endeavors. It discusses the origins of the word and explains it in simple terms. Chapter 2 takes you through a step- by- step process to create a game that almost everyone has played in their childhood— thus establishing the main principles of writing a program with a simple introduction to user interfaces. This is the second longest chapter that lays the foundation for teaching a few more advanced skills that are presented in the following chapters. The biggest barrier to program development is the fear of failure— this fear paralyzes most individuals who are not in the com- puting profession. The techniques described in this chapter will help get over this fear by teaching you how to “debug” your program— no matter how small— and under- stand which part is not working, so that it can be quickly corrected and lead you to a successful run. Chapter 3 describes how to automate a very popular puzzle— Sudoku. This chapter teaches a few new skills such as discovering patterns based on tuples and exploring deductive logic to solve a complex riddle. Sudoku is fun. But solving Sudoku by developing algorithms for each technique is ten times more fun! Chapter 4 discusses the concept of multiplatform integration by introducing a problem of controlling a powerpoint presentation from a smartphone and then solving it through simple programming techniques. It also demonstrates that innovation can be achieved in simple things if you put your mind to it. Chapter 5 teaches how to handle files through a very innovative exercise— creating a filing cabinet for your computer files while introducing the concept of recursion. Chapter 6 discusses the workbook merge problem— how to combine multiple sheets into one without having to open each one and manually cut and paste. While the code is simple, it saves a lot of time and repetitive effort. Chapter 7 introduces the topic of graphs by teaching you how to convert three col- umns of text to a graphical architecture diagram automatically, through the use of the spreadsheet. This program is a very useful tool for system engineers to visualize how data are exchanged between different components of a complex system. Chapter 8 builds on the graphical concepts of Chapter 6 by solving the reverse problem— how to convert a graph to text. Chapter 9 teaches a fundamental technique of web scraping— a skill that is essen- tial for automating all kinds of tasks that need pulling data from the web. It teaches the complex part of pulling these data where data retrieval requires a “prompt” (e.g., a form) and reveals how to automate this prompt. Chapter 10 explores the character of Professor Butts and Rube Goldberg ma- chines. Through a simple example of a webcam software, it helps you build a burglar alarm, a customer counter, a remotely activated camera, and a greeting machine. These examples should serve as an inspiration for you to experiment with simple programming concepts that you can connect and build, unraveling a legoland of opportunities.
3Introduction 3 Chapter 11 pulls in all the skills learnt in the previous chapters to create a visual- ization that is complex, relevant, and complete. The book is not meant to be a formal theoretical dissertation. Each chapter is an experiment— a story of success where a human being was able to build something awesome by using the power of Excel on your laptop and a little bit of coding. The original codes in this book are written by me on a Windows computer, but they would work on Macs as well with very few, if any, compatibility issues. The code provided in this book is written in a simple way that does not confuse the reader with complex computer science rigors of efficient coding. There are no materials that are devoted to explain good coding practices, best ways to name vari- ables, quickest way to do a sort or the explanation of memory management. These are important— but the emphasis here is on how to solve a problem with a quick and intuitive way to code the algorithm. There are many books available for the “science”, but very few on the “art” of solving. You may ultimately want to be a developer and even a computer scientist but right now all you need is to understand the technique of building an algorithm. The code used in this book is available for you to download on the publisher’s site, along with a few videos on some chapters to get you started. To maximize your learning, you have to practice the code, test it out yourself, and build it further to address your specific needs. Like Logic & Math? This book is for you! Programmer ? Love Creativity ? Hate repetitive work? Yes Yes No Yes FIGURE I.1 Algorithm of this book.
Comments 0
Loading comments...
Reply to Comment
Edit Comment