Skip to content
Search lessons, topics, tests…
Esc

    ↑ ↓ moveEnter openEsc close

    Coding problem

    Number the invoice lines

    EasyAbout 15 minFreeLearn it: Streaming Operators: Where, Select and Take in O(1) Extra Space

    The task

    An invoice lists only the products a customer actually bought, numbered 1, 2, 3 down the page.

    Complete NumberLines(List<OrderLine> lines). OrderLine is a record with a Product, a Quantity and a UnitPrice; InvoiceRow is a record with a Number, a Product and a Total. Leave out the lines whose Quantity is 0, keep the other lines in their order, and turn each into an InvoiceRow. The rows are numbered 1, 2, 3 and so on, with no gaps, and Total is Quantity times UnitPrice. An empty list gives an empty list. The list and its lines are never null, quantities are 0 or more, and unit prices are more than 0.

    Signature

    public record OrderLine(string Product, int Quantity, decimal UnitPrice);
    public record InvoiceRow(int Number, string Product, decimal Total);
    public static List<InvoiceRow> NumberLines(List<OrderLine> lines)

    Examples

    • [("pen", 3, 1.50), ("ink", 0, 4.00), ("pad", 2, 2.25)] → [(1, "pen", 4.50), (2, "pad", 4.50)]. The ink line has no quantity, so it is left out and the pad becomes row 2.
    • [("ink", 0, 4.00)] → []. Nothing was bought.

    Limits: Up to 300,000 lines.

    The tests check: an empty list, only lines without quantity, one line, left-out lines at the start, middle and end leaving no gaps, totals with cents, that the lines keep their order, two identical lines getting their own numbers, and 300,000 lines in time.

    Hints

      The solution opens by itself when all tests pass. You remember more if you try first.

      Sign in to see the solution

      Your code

      C#Solution.csLoading the editor…

      Already included: using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Net.Http; using System.Threading; using System.Threading.Tasks;

      public record OrderLine(string Product, int Quantity, decimal UnitPrice);
      public record InvoiceRow(int Number, string Product, decimal Total);
      
      public static class Solution
      {
          // Leaves out the lines without quantity and numbers the others 1, 2, 3 with their totals.
          public static List<InvoiceRow> NumberLines(List<OrderLine> lines)
          {
              throw new NotImplementedException();
          }
      }

      Sign in to run and submit your code. It stays here while you do.