Starbeamrainbowlabs

Stardust
Blog

Coding Conundrums 3

Number three in a series of more than two. But probably less than a thousand.

I have finished my solutions to Rob Miles' Coding Conundrums. If you want to complete them yourself before looking at my solutions, take a look at the pdf.

My solutions share quite a few functions, perhaps I should look into writing a library for them.

3.1: Total Carp

People keep coming to me and bragging about the fish that they just caught. They tell me the make of the fish, "Carp" for example, and how much it weighed, 2000 grams for example.

I want a program into which I can type:

  • The name of each latest fish
  • The weight of each latest fish
  • When I enter a fish called "Finished" (fish pun alert) I want the program to print out:

  • The heaviest fish(name and weight)
  • The lightest fish(name and weight)
  • The total weight of all the fish that have been entered

My solution uses structs - do you remember these from semester 1?

using System;
using System.Collections.Generic;

public class Program
{
    public struct Fish
    {
        public string Name;
        public int Weight;

        public Fish(string inName, int inWeight)
        {
            Name = inName;
            Weight = inWeight;
        }

        public override string ToString()
        {
            return String.Format("{0} at {1}g", Name, Weight);
        }
    }

    public static List<Fish> FishList = new List<Fish>();

    public static int totalWeight;

    public static void Main(string[] args)
    {
        int i = 1;
        while(true)
        {
            string inFishName = ReadString(String.Format("Enter fish {0}'s name: ", i));

            if(inFishName.ToLower() == "finished")
                break;

            int inFishWeight = ReadNumber(String.Format("Enter fish {0}'s weight (in grams): ", i));

            FishList.Add(new Fish(inFishName, inFishWeight));

            Console.WriteLine("Fish added.");
        }

        /// calculate the statistics ///
        // variable declaration
        Fish heaviestFish = FishList[0];
        Fish lightestFish = FishList[0];

        foreach(Fish currentFish in FishList)
        {
            if (currentFish.Weight > heaviestFish.Weight)
                heaviestFish = currentFish;
            if (currentFish.Weight < lightestFish.Weight)
                lightestFish = currentFish;

            totalWeight += currentFish.Weight;
        }

        // Print out the statistics calculated above
        Console.WriteLine("Total weight: {0}", totalWeight);
        Console.WriteLine("Heaviest fish: {0}", heaviestFish.ToString());
        Console.WriteLine("Lightest fish: {0}", lightestFish.ToString());
    }

    /// <summary>
    /// Reads a string in from the user.
    /// </summary>
    /// <param name="prompt">The prompt to display</param>
    /// <param name="minlength">The minimum length of the input the user enters.</param>
    /// <returns>The string the user entered.</returns>
    public static string ReadString(string prompt, int minlength = 1)
    {
        while(true)
        {
            Console.Write(prompt);
            string line = Console.ReadLine();
            if(line.Length < minlength)
            {
                Console.WriteLine("Please enter something that is at least {0} characters.");
                continue;
            }

            return line;
        }
    }

    /// <summary>
    /// Reads a number from the user.
    /// </summary>
    /// <remarks>Depends on ReadString()</remarks>
    /// <param name="prompt">The prompt to display to the user.</param>
    /// <returns>The number that the user entered</returns>
    public static int ReadNumber(string prompt)
    {
        while(true)
        {
            string line = ReadString(prompt).Trim();
            try {
                return int.Parse(line);
            }
            catch
            {
                Console.WriteLine("Sorry, that was not a valid number.");
            }
        }
    }
}

(Pastebin, 32 bit binary)

3.2: Fish Popularity Contest

It occurs to me that it would be useful to add another feature to the fish program above. I'd like to know which is the most popular fish. That way I can buy shares in it. Or something. So I've added the following extra requirement:

  • The most popular fish (i.e. the fish that we have seen the most of)

Note that my keyboard skills are not very gud. And so if I type CArp rather than Carp I'd like the program to behave sensibly and count them both as the same make of fish.

The addition of another List to the loop at the end of the program and a few extra bits and pieces yields FishListPlus:

using System;
using System.Collections.Generic;

public class Program
{
    public struct Fish
    {
        public string Name;
        public int Weight;

        public Fish(string inName, int inWeight)
        {
            Name = inName;
            Weight = inWeight;
        }

        public override string ToString()
        {
            return String.Format("{0} at {1}g", Name, Weight);
        }
    }

    public static List<Fish> FishList = new List<Fish>();

    public static int totalWeight;

    public static void Main(string[] args)
    {
        int i = 1;
        while(true)
        {
            string inFishName = ReadString(String.Format("Enter fish {0}'s name: ", i));

            if(inFishName.ToLower() == "finished")
                break;

            int inFishWeight = ReadNumber(String.Format("Enter fish {0}'s weight (in grams): ", i));

            FishList.Add(new Fish(inFishName, inFishWeight));

            Console.WriteLine("Fish added.");
        }

        /// calculate the statistics ///
        // variable declaration
        Fish heaviestFish = FishList[0];
        Fish lightestFish = FishList[0];

        Dictionary<string, int> PopularFish = new Dictionary<string, int>();

        foreach(Fish currentFish in FishList)
        {
            if (currentFish.Weight > heaviestFish.Weight)
                heaviestFish = currentFish;
            if (currentFish.Weight < lightestFish.Weight)
                lightestFish = currentFish;

            totalWeight += currentFish.Weight;

            int CurrentFishCount;
            if(PopularFish.TryGetValue(currentFish.Name.ToLower(), out CurrentFishCount))
            {
                PopularFish[currentFish.Name.ToLower()]++;
            }
            else
            {
                PopularFish[currentFish.Name.ToLower()] = 1;
            }
        }

        // Print out the statistics calculated above
        Console.WriteLine(" ----------------------------- ");
        Console.WriteLine("| {0,-12} | {1,12} |", "Name", "Count");
        Console.WriteLine("|--------------|--------------|");
        foreach(KeyValuePair<string, int> FishCount in PopularFish)
        {
            Console.WriteLine("| {0,-12} | {1,12} |", FishCount.Key, FishCount.Value);
        }
        Console.WriteLine(" ----------------------------- ");

        Console.WriteLine("Total weight: {0}", totalWeight);
        Console.WriteLine("Heaviest fish: {0}", heaviestFish.ToString());
        Console.WriteLine("Lightest fish: {0}", lightestFish.ToString());
    }

    /// <summary>
    /// Reads a string in from the user.
    /// </summary>
    /// <param name="prompt">The prompt to display</param>
    /// <param name="minlength">The minimum length of the input the user enters.</param>
    /// <returns>The string the user entered.</returns>
    public static string ReadString(string prompt, int minlength = 1)
    {
        while(true)
        {
            Console.Write(prompt);
            string line = Console.ReadLine();
            if(line.Length < minlength)
            {
                Console.WriteLine("Please enter something that is at least {0} characters.");
                continue;
            }

            return line;
        }
    }

    /// <summary>
    /// Reads a number from the user.
    /// </summary>
    /// <remarks>Depends on ReadString()</remarks>
    /// <param name="prompt">The prompt to display to the user.</param>
    /// <returns>The number that the user entered</returns>
    public static int ReadNumber(string prompt)
    {
        while(true)
        {
            string line = ReadString(prompt).Trim();
            try {
                return int.Parse(line);
            }
            catch
            {
                Console.WriteLine("Sorry, that was not a valid number.");
            }
        }
    }
}

(Pastebin, 32 bit binary)

3.3: Ingredients Tracker

Talking of food, I want something to keep track of ingredients that I might have in my kitchen. My program will have two modes:

  1. The program will start in this mode. I can enter the name and amount of each ingredient. If I type in the name "Search" the program will go into mode 2 (see below).
  2. I can type the name of an ingredient and the program will tell me how much of that ingredient I have in stock. If I type in the name "Enter" the program will go into mode 1 (see above)

If you fancy getting clever, you can add a third command, "List", which will list out all the ingredients that have been entered.This is making me quite hungry.

I think I’ll go off for a pie while you work on these.

I decided to use a dictionary for this one, which holds the amount of each item as an int, with the name as the key. Since you can't have duplicate keys, I ask the user if they type in the same key more than once whether they do actually want to overwrite their previous entry.

using System;
using System.Collections.Generic;

public class Program
{
    public enum Mode
    {
        DataEntry,
        Search
    }

    public static Mode CurrentMode = Mode.DataEntry;

    static Dictionary<string, int> Stock = new Dictionary<string, int>();

    public static void Main()
    {
        Console.WriteLine("Kitechen Tracker v0.2");
        Console.WriteLine("---------------------");
        Console.WriteLine("Coding Conundrum 3.3, challenge set by Rob Miles, conquered by Starbeamrainbowlabs");
        Console.WriteLine("Enter the special name 'search' to switch to search mode");
        Console.WriteLine("Enter the special name 'enter' to switch back to data entry mode");
        Console.WriteLine("Enter the special name 'list' at any time to view the current stock");
        Console.WriteLine();
        while(true)
        {
            switch (CurrentMode)
            {
                case Mode.DataEntry:
                    string inName = ReadString("Enter the name of the next ingredient: ").Trim().ToLower();
                    switch(inName)
                    {
                        case "search":
                            Console.WriteLine("Switching to search mode.");
                            CurrentMode = Mode.Search;
                            continue;

                        case "list":
                            ListStock();
                            continue;

                        default:
                            int inAmount = ReadNumber("Enter the amount: ");
                            int currentAmount;
                            if(Stock.TryGetValue(inName, out currentAmount))
                            {
                                if(!Confirm(String.Format("There is already an entry for {0} (with an amount of {1}). Do you want to overwrite it? (y / n)", inName, currentAmount)))
                                {
                                    continue;
                                }
                            }
                            Console.WriteLine("{0} has been added to the list.", inName);
                            Stock[inName] = inAmount;
                            break;
                    }
                    break;

                case Mode.Search:
                    string searchString = ReadString("Enter ingredient name: ").Trim().ToLower();
                    switch(searchString)
                    {
                        case "enter":
                            Console.WriteLine("Switching to data entry mode");
                            CurrentMode = Mode.DataEntry;
                            continue;

                        case "list":
                            ListStock();
                            continue;

                        default:
                            int amountInStock;
                            if(Stock.TryGetValue(searchString, out amountInStock))
                            {
                                Console.WriteLine("Amount of {0}: {1}", searchString, amountInStock);
                            }
                            else
                            {
                                Console.WriteLine("There isn't anything called {0} in stock at the moment.", searchString);
                            }
                            break;
                    }
                    break;
            }
        }
    }

    public static void ListStock()
    {
        Console.WriteLine("Current Stock:");
        foreach(KeyValuePair<string, int> Ingredient in Stock)
        {
            Console.WriteLine("{0,-12}: {1}", Ingredient.Key, Ingredient.Value);
        }
    }

    /// <summary>
    /// Asks the user a simple yes / no question.
    /// </summary>
    /// <param name="prompt">The prompt to display</param>
    /// <returns>The user's choice as a boolean</returns>
    public static bool Confirm(string prompt)
    {
        Console.WriteLine(prompt);
        while(true)
        {
            ConsoleKeyInfo nextChar = Console.ReadKey(true);
            switch(nextChar.Key.ToString().ToLower())
            {
                case "y":
                    return true;
                case "n":
                    return false;
            }
        }
    }

    /// <summary>
    /// Reads a string in from the user.
    /// </summary>
    /// <param name="prompt">The prompt to display</param>
    /// <param name="minlength">The minimum length of the input the user enters.</param>
    /// <returns>The string the user entered.</returns>
    public static string ReadString(string prompt, int minlength = 1)
    {
        while (true)
        {
            Console.Write(prompt);
            string line = Console.ReadLine();
            if (line.Length < minlength)
            {
                Console.WriteLine("Please enter something that is at least {0} characters.");
                continue;
            }

            return line;
        }
    }

    /// <summary>
    /// Reads a number from the user.
    /// </summary>
    /// <remarks>Depends on ReadString()</remarks>
    /// <param name="prompt">The prompt to display to the user.</param>
    /// <returns>The number that the user entered</returns>
    public static int ReadNumber(string prompt)
    {
        while (true)
        {
            string line = ReadString(prompt).Trim();
            try
            {
                return int.Parse(line);
            }
            catch
            {
                Console.WriteLine("Sorry, that was not a valid number.");
            }
        }
    }
}

(Pastebin, 32 bit binary)

Tag Cloud

3d 3d printing account algorithms android announcement architecture archives arduino artificial intelligence artix assembly async audio automation backups bash batch blender blog bookmarklet booting bug hunting c sharp c++ challenge chrome os cluster code codepen coding conundrums coding conundrums evolved command line compilers compiling compression containerisation css dailyprogrammer data analysis debugging demystification distributed computing dns docker documentation downtime electronics email embedded systems encryption es6 features ethics event experiment external first impressions freeside future game github github gist gitlab graphics hardware hardware meetup holiday holidays html html5 html5 canvas infrastructure interfaces internet interoperability io.js jabber jam javascript js bin labs learning library linux lora low level lua maintenance manjaro minetest network networking nibriboard node.js open source operating systems optimisation own your code pepperminty wiki performance phd photos php pixelbot portable privacy problem solving programming problems project projects prolog protocol protocols pseudo 3d python reddit redis reference releases rendering resource review rust searching secrets security series list server software sorting source code control statistics storage svg systemquery talks technical terminal textures thoughts three thing game three.js tool tutorial tutorials twitter ubuntu university update updates upgrade version control virtual reality virtualisation visual web website windows windows 10 worldeditadditions xmpp xslt

Archive

Art by Mythdael