Beginner's Guide to Code Algorithms
eBook - ePub

Beginner's Guide to Code Algorithms

Experiments to Enhance Productivity and Solve Problems

  1. 168 pages
  2. English
  3. ePUB (mobile friendly)
  4. Available on iOS & Android
eBook - ePub

Beginner's Guide to Code Algorithms

Experiments to Enhance Productivity and Solve Problems

About this book

Do you have creative ideas that you wish you could transform into code?

Do you want to boost your problem solving and logic skills?

Do you want to enhance your career by adopting an algorithmic mindset?

In our increasingly digital world, coding is an essential skill. Communicating an algorithm to a machine to perform a set of tasks is vital. Beginner's Guide to Code Algorithms: Experiments to Enhance Productivity and Solve Problems written by Deepankar Maitra teaches you how to think like a programmer. The author unravels the secret behind writing code – building a good algorithm. Algorithmic thinking leads to asking the right question and enables a shift from issue resolution to value creation. Having this mindset will make you more marketable to employers.

This book takes you on a problem-solving journey to expand your mind and increase your willingness to experiment with code.

You will:

  • Learn the art of building an algorithm through hands-on exercises
  • Understand how to develop code for inspiring productivity concepts
  • Build a mentality of developing algorithms to solve problems
  • Develop, test, review, and improve code through guided experimentation

This book is designed to develop a culture of logical thinking through intellectual stimulation. It will benefit students and teachers of programming, business professionals, as well as experienced users of Microsoft Excel who wish to become proficient with macros.

Trusted by 375,005 students

Access to over 1.5 million titles for a fair monthly price.

Study more efficiently using our study tools.

Information

Publisher
CRC Press
Year
2022
Print ISBN
9781032102382
9781032080642
Edition
1
eBook ISBN
9781000535785

1What Is an Algorithm?

DOI: 10.1201/9781003214335-2
Algorithm is a word used in computer science. It simply means a step-by-step instruction. But it encapsulates the essence of computer programming, because without a step-by-step instruction a computer cannot perform a useful task. Therefore, it means much more than that.
The word algorithm has the same root as the word Algebra. With an Arab past, it came from ninth-century Persian mathematician Muḥammad ibn Mūsā al-Khwārizmī. Al-Khwārizmī (Arabic: الخوارزمی, c. 780–850) was a Persian mathematician, astronomer, geographer, and scholar whose name means “the native of Khwarazm”, a region that was part of Greater Iran and is now in Uzbekistan.
About 825 ad, al-Khwarizmi wrote an Arabic language treatise on the Hindu–Arabic numeral system, which was translated into Latin during the 12th century under the title Algoritmi de numero Indorum. This title means “Algoritmi on the numbers of the Indians”, where “Algoritmi” was the translator’s Latinization of al-Khwarizmi’s name. The modern sense of this word was introduced in the 19th century. By the way, al-Khwarizmi was the most widely read mathematician in Europe in the late Middle Ages, primarily through another of his books, the Algebra.
Algorithms are part of our understanding of life and is used by all fields and disciplines to convey a process. Some are small and famous. Like the book “Eat, Love, Pray”. Or the recent hotel/restaurant ad: “Eat, Drink, Sleep, Repeat”. Some are long and detailed, such as a recipe.

How to make chocolate chip cookies
Ingredients
  • - 2 cups all-purpose flour
  • - 1 teaspoon baking soda
  • - Fine salt
  • - 1 stick (12 tablespoons) unsalted butter, at room temperature
  • - 1 cup packed light brown sugar
  • - 1 cup granulated sugar
  • - 2 large eggs
  • - 1 teaspoon pure vanilla extract
  • - One 12-ounce bag semisweet chocolate chips
Process
  1. Preheat to 375 degrees F
  2. Stir together the flour, baking soda, and 1 teaspoon salt in a large bowl.
  3. Beat the butter and both sugars, eggs, vanilla, and flour about 4 minutes. Stir in the chocolate chips.
  4. Scoop 12 tablespoons of dough about 2 inches apart onto each prepared baking sheet. Roll the dough into balls with slightly wet hands.
  5. Bake, rotating the cookie sheets from upper to lower racks halfway through, until golden but still soft in the center, 12 to 15 minutes.
  6. Wait for a few minutes on the baking sheet, and then transfer to a rack to cool completely.

Figure 1.1 Making chocolate chip cookies.
In the computer world, an algorithm is used to code the program, hence needs to be somewhat detailed. But the role of the algorithm is to facilitate the creation of the program—just like a recipe. Once the chocolate chip cookie is made, the recipe is not useful, until you are ready to make the next batch!
The discussion on algorithms is not complete without the concept of some important attributes . A good algorithm needs to be:
  • Accurate
  • Repeatable
  • Concise
  • Optimized
These four qualities can be easily understood with the recipe example.
The recipe is accurate, in the sense that it can create tasty chocolate chip cookies. Note the emphasis on “tasty”. The recipe does not create “healthy” cookies. For that matter, the word “healthy” has its own set of attributes, which are somewhat subjective (does healthy mean less carb? More protein? Less cholesterol? More vitamins?) and mutually interdependent (higher magnesium and phosphorus in a readily available ingredient like brown sugar may also mean higher carb).
Repeatable is easily demonstrated by being able to use the recipe again and again for making delicious cookies. A recipe would not be so repeatable if it requires an ingredient that is tough to procure. Or sometimes the product is self-destructive, which does not make it repeatable. A good example of a self-destructive algorithm is a video shoot of an accident.
Concise is a quality of being able to express in a few words. For example, I want to say that I suffer from a problem where my lens in my right eye is unable to focus light beyond a distance in front of my retina. I can express it with the term “myopia”. While this is just a better way to communicate in this instance, for an algorithm that is concise, it can have a huge consequence, such as reduce the cost of producing a vaccine by 50% or increase the chances striking oil in the ground by 30%.
The dictionary meaning of “optimized” is “make the best or most effective use of a situation, opportunity, or resource”. Optimized is a quality that is the hardest to understand. Optimized can mean many things based on the “situation, opportunity or resource” that is optimized. For example, the Tesla model 3 is the most fuel-efficient car in the “midsize” class of cars in 2020, as determined by fueleconomy.gov. Note that I had to qualify this statement by the year and the “class”. Even in 2020, the most fuel-efficient class of “compact” cars is not Tesla—it is Volkswagen e-Golf. And the most fuel-efficient “midsize” car in 2015 was Nissan Leaf.
The point I want you to take away from this discussion is that no good quality is an absolute value. True for life as well, the qualities that make the evaluation of anything “good” or “bad” are bound by the parameters that are used to judge them.
We will discuss these qualities further in the next few chapters, as we dive deep into the programs that I will introduce to get you inspired.
So why do we need algorithms?
Algorithms are to program development what a recipe is to the preparation of a dish—a bit more in some respects and a bit less in others.
An algorithm for a program describes how the code needs to be written in conceptual terms.
Even though there is no precise measurement like a recipe, an algorithm is a documentation of technique that is indispensable for the programmer. Just as you cannot make cookies without a recipe, you cannot write a program that solves a specific problem without an algorithm.
An algorithm usually has six parts to it:
  • Inputs
  • Outputs
  • Variables
  • Statements
  • Conditional Statements
  • Repetitions
The input is what is available for the processor to work with—for example, the flour and the sugar.
The output is the cookie but can be other things as well, such as a timer that chimes when the cooking is done (audible output), or the mixers and plates that need to be washed after the cookie is made (visible outputs).
Variables are receptacles that temporarily store something so that you can remember it or use it later on. For example, you have a glass of water and you need to use only half of it now and half later. You will need another glass to pour the other half until you need it. Now suppose that you do not have a measuring stick to measure the height of the glass, and you do not want to rely on your judgment to determine whether it is exactly half—what do you do? You get another glass that looks exactly like the other two glasses. You pour the full glass into the two empty ones and check their heights by placing them side by side. If one is higher, you pour a little bit from the glass with the greater height to the glass with the lesser height.
A statement is usually an instruction to do something—the most common being moving data from one variable to another. In the glass example above, pouring water from one glass to another in an attempt to equalize is a statement.
A condition is a decision point that helps the situation direct the execution of a statement from a choice of more than one statement. In our example above, checking the water level in one glass is higher than the other is a condition. That helps one decide whether to pour the water from the left glass to the right, or from the right glass to the left. Pouring water from one glass to another is a conditional statement. Pouring from left to right is one statement, from right to left is another, and the condition helps us decide which one should be done.
Chances are that just one pour will not equalize the glasses. If you are like me, as soon as I have poured the higher to the lower, the lower now becomes higher! So, we need to do the exact opposite, until we find that the heights are the same. This is known as a repetition. Often described in programming languages as “Do Loops”, “While Loops” and “Do Untils”—this is the critical part of an algorithm that is immensely useful for a program—because it is the repetitive work that a computer program does best, by relieving the human being from this task.
Now let us look at a problem that we can solve using a spreadsheet to understand these six structures in business terms.
Imagine that I am a sales executive in a shoe company that operates in three states. I need to send a report to my boss that summarizes the sales for all three states in one spreadsheet. I have each state in a separate spreadsheet as shown below.
Figure 1.2Sales figures by state.
Figure 1.2 Continued I need to execute the following steps to make my boss happy:
  1. Open a new spreadsheet
  2. Copy spreadsheet A from cells A1 to C7
  3. Paste this in the new spreadsheet in cell A1
  4. Copy spreadsheet B from cells B1 to C7 (note that we did not copy rows 1 and 2 again—since these are part of the header)
  5. Paste this in the new spreadsheet in cell A8 (note that the row has now advanced to 7 in the new spreadsheet)
  6. Copy spreadsheet C from cells B1 to C7
  7. Paste this in the new spreadsheet in cell A13 (note that the row has now advanced to 12 in the new spreadsheet)
  8. Save new spreadsheet
  9. Send to Boss via email
The final spreadsheet would look something like this.
Figure 1.3Combined spreadsheet.
While this is a precise set of steps to accomplish this task, this is completely manual. If I had 50 states to deal with, this would be taking me too long! I...

Table of contents

  1. Cover
  2. Half-Title Page
  3. Title Page
  4. Copyright Page
  5. Dedication
  6. Table of Contents
  7. Preface
  8. Introduction
  9. 1 What Is an Algorithm?
  10. 2 Build Your Own Game with a Simple Algorithm: Tic Tac Toe
  11. 3 Explore Your Deductive Logic: Solve a Sudoku Forever
  12. 4 Introduction to Multiplatform Integration: Build Your Own Remote Control
  13. 5 The Organizer: Build Your Own Virtual Filing Cabinet
  14. 6 Merging Sheets: Combine Multiple Workbooks of the Same Format into One Workbook Automatically
  15. 7 Introduction to Graphs: Create Your Own Interface Diagram Instantly
  16. 8 Shaping up: Analyze a Picture and Document Its Components in Text
  17. 9 Real-Time Currency Conversion: An Introduction to Simple Web Scraping Techniques
  18. 10 The Genius of Collaboration: Build a Burglar Alarm Using a Free Webcam Application
  19. 11 Advanced Graphics: Complex Visualizations and More
  20. The Final Word
  21. List of Figures
  22. Bibliography
  23. A Little Bit of Computer Science
  24. Index

Frequently asked questions

Yes, you can cancel anytime from the Subscription tab in your account settings on the Perlego website. Your subscription will stay active until the end of your current billing period. Learn how to cancel your subscription
No, books cannot be downloaded as external files, such as PDFs, for use outside of Perlego. However, you can download books within the Perlego app for offline reading on mobile or tablet. Learn how to download books offline
Perlego offers two plans: Essential and Complete
  • Essential is ideal for learners and professionals who enjoy exploring a wide range of subjects. Access the Essential Library with 800,000+ trusted titles and best-sellers across business, personal growth, and the humanities. Includes unlimited reading time and Standard Read Aloud voice.
  • Complete: Perfect for advanced learners and researchers needing full, unrestricted access. Unlock 1.5M+ books across hundreds of subjects, including academic and specialized titles. The Complete Plan also includes advanced features like Premium Read Aloud and Research Assistant.
Both plans are available with monthly, semester, or annual billing cycles.
We are an online textbook subscription service, where you can get access to an entire online library for less than the price of a single book per month. With over 1.5 million books across 990+ topics, we’ve got you covered! Learn about our mission
Look out for the read-aloud symbol on your next book to see if you can listen to it. The read-aloud tool reads text aloud for you, highlighting the text as it is being read. You can pause it, speed it up and slow it down. Learn more about Read Aloud
Yes! You can use the Perlego app on both iOS and Android devices to read anytime, anywhere — even offline. Perfect for commutes or when you’re on the go.
Please note we cannot support devices running on iOS 13 and Android 7 or earlier. Learn more about using the app
Yes, you can access Beginner's Guide to Code Algorithms by Deepankar Maitra in PDF and/or ePUB format, as well as other popular books in Computer Science & Computer Science General. We have over 1.5 million books available in our catalogue for you to explore.