logo CodeStepByStep logo

BaggageColors2

Write a method named BaggageColors2 (similar to the previous exercise, baggageColors) except in this version you will find and return the total number of bags that can be contained inside one "shiny gold" bag. As before, your method accepts a string representing a file name as a parameter where each line of the file data names a unique type of bag, such as a "light red bag", along with what other kinds of bags that bag contains. For example, suppose the file named baggage.txt contains the following:

light red bags contain 1 bright white bag, 2 muted yellow bags.
dark orange bags contain 3 bright white bags, 4 muted yellow bags.
bright white bags contain 1 shiny gold bag.
shiny gold bags contain 1 dark olive bag, 2 vibrant plum bags.
dark olive bags contain 3 faded blue bags, 4 dotted black bags.
muted yellow bags contain 2 shiny gold bags, 9 faded blue bags.
vibrant plum bags contain 5 faded blue bags, 6 dotted black bags.
faded blue bags contain no other bags.
dotted black bags contain no other bags.
  • faded blue bags contain 0 other bags.
  • dotted black bags contain 0 other bags.
  • vibrant plum bags contain 11 other bags: 5 faded blue bags and 6 dotted black bags.
  • dark olive bags contain 7 other bags: 3 faded blue bags and 4 dotted black bags.

A single shiny gold bag contains 1 dark olive bag (and the 7 bags within it) plus 2 vibrant plum bags (and the 11 bags within each of those): 1 + 1*7 + 2 + 2*11 = 32 bags. So the call of BaggageColors2("baggage.txt") should return 32.

You may assume that the file exists and is readable, that it follows the format described above, and that there will be at least one line of input in the file.

Method: Write a C# method as described, not a complete program or class.

You must log in before you can solve this problem.

Log In

Need help?

Stuck on an exercise? Contact your TA or instructor.

If something seems wrong with our site, please

Is there a problem? Contact us.