Skip to main content

Posts

Showing posts with the label Problem Solving

How I Used a Classic Algorithm to Solve a Real-Life Restaurant Problem

Algorithms and data structures often feel like abstract, academic exercises when you first encounter them. Many of us in university wondered, Will I ever use this in real life? Fast forward to the working world, and the answer is often a resounding yes . Here’s a story of how I used Breadth-First Search (BFS), a classic algorithm, to solve a seemingly impossible problem for a restaurant inventory system. The Restaurant Dilemma Imagine you’re designing a system for a restaurant chain. The system needs to handle recipes, track ingredient usage, and calculate costs for menu items. Sounds straightforward, right? Let’s complicate it. Consider a dish like Lasagna. To make lasagna, you need: Pasta Sheets Bolognese Sauce Cheese But here’s the twist: Bolognese Sauce is itself a recipe that requires: Ground Meat Tomatoes Spices Every ingredient comes from an inventory with details like: Unit of Measurement (UOM) : Grams, Liters, etc. Cost per Unit : Calculated based on the last purchase price...

Building a Tic Tac Toe Console Game in C#

When learning a new programming language, a common exercise is to create a classic game, such as Tic Tac Toe. In this post, I'll guide you through creating a simple, console-based Tic Tac Toe game in C#. First, let's start with the challenge we're trying to solve: Tic Tac Toe is a two-player game. We need to handle input from two players and alternate between them. The game is played on a 3x3 grid. We need to keep track of the state of this grid. A player wins by marking three spots in a row, either horizontally, vertically, or diagonally. The game continues until a player has won or all spots on the grid have been marked, in which case the game is a draw. To address these challenges, we need to develop a problem-solving mindset. Let's break down the problem into smaller, manageable pieces and think about how we could solve each one. This process is known as "decomposition" and it's a fundamental skill in programming and problem-solving in general. Step 1:...

Total Number of Handshakes

Question:  At the annual meeting of Board of Directors of Acme Inc, every one starts shaking hands with everyone else in the room. Given the fact that any two persons shake hand exactly once, Can you tell the total count of handshakes? Answer: If there are n persons in the room, each person will have n-1 handshakes. There are n person in the room. So, total number of handshakes is n*(n-1). but we have counted each handshake twice. Like 1<=>2 and 2<=>1. If we divide the number by 2, we'll get the expected output. So the answer is (n*(n-1))/2. Again, if we consider this situation, first, the first person has n-1 handshakes. Then the second one has n-2. Similarly further, n-3, n-4....3, 2, 1. So the total number becomes (n-1)+(n-2)+(n-3)+....+3+2+1. if we use the formula for summation, we get the answer, that is (n*(n-1))/2.