CSE 212: Programming with Data Structures

W02 Analyze: Stacks

Overview

This assignment must be completed individually to ensure you are meeting all course outcomes. You should not complete this assignment with a group. If you obtain help from a tutor, the tutor should help you understand principles but should not help you answer these problems. It is an honor code violation to obtain answers for these problems from others including using the internet (i.e. sites that allow students to share their solutions).

Instructions

All of the coding files for this assignment will be found in the course week02/analyze folder in the course repository.

This assignment will be submitted as an I-Learn Quiz.

First Mystery Stack

  1. Examine the following code for MysteryStack1. Do not execute this code. Instead, using the techniques from the learning materials this week, analyze this code to determine its purpose and the way a stack is useful.
  2. 
    public static class MysteryStack1
    {
        public static string Run(string text)
        {
            var stack = new Stack();
            foreach (var letter in text)
                stack.Push(letter);
    
            var result = "";
            while (stack.Count > 0)
                result += stack.Pop();
    
            return result;
        }
    }
    
  3. Determine the output of the function if the input text is equal to the following:
    • racecar
    • stressed
    • a nut for a jar of tuna

Second Mystery Stack

  1. Examine the following code for MysteryStack2. Do not execute this code. Instead, using the techniques from the learning materials this week, analyze this code to determine its purpose and the way a stack is useful.
  2. 
      public static class MysteryStack2
      {
          private static bool IsFloat(string text)
          {
              return float.TryParse(text, out _);
          }
      
          public static float Run(string text)
          {
              var stack = new Stack();
              foreach (var item in text.Split(' '))
              {
                  if (item == "+" || item == "-" || item == "*" || item == "/")
                  {
                      if (stack.Count < 2)
                          throw new ApplicationException("Invalid Case 1!");
      
                      var op2 = stack.Pop();
                      var op1 = stack.Pop();
                      float res;
                      if (item == "+")
                      {
                          res = op1 + op2;
                      }
                      else if (item == "-")
                      {
                          res = op1 - op2;
                      }
                      else if (item == "*")
                      {
                          res = op1 * op2;
                      }
                      else
                      {
                          if (op2 == 0)
                              throw new ApplicationException("Invalid Case 2!");
      
                          res = op1 / op2;
                      }
      
                      stack.Push(res);
                  }
                  else if (IsFloat(item))
                  {
                      stack.Push(float.Parse(item));
                  }
                  else if (item == "")
                  {
                  }
                  else
                  {
                      throw new ApplicationException("Invalid Case 3!");
                  }
              }
      
              if (stack.Count != 1)
                  throw new ApplicationException("Invalid Case 4!");
      
              return stack.Pop();
          }
      }
    
  3. Determine the result of the function if the following inputs were provided:
    • 5 3 7 + *
    • 6 2 + 5 3 - /
  4. Consider possible values for the input parameter text that would result in the function doing the following:
    • Display "Invalid Case 1!"
    • Display "Invalid Case 2!"
    • Display "Invalid Case 3!"
    • Display "Invalid Case 4!"

Submission

Other Links: