Bloomberg Laundry Day - count items in list, check socks for pairs, and organize all items alphabetically

时间:2017-11-08 22:08:31

标签: java testing

I'm trying to solve the following problem: https://codecon.bloomberg.com/challenger-series/33

Input Specifications

Each article of clothing will have its own separate line. You have a penchant for hoarding, so there is no guarantee as to the number of pieces, but you can assure yourself that each article can be easily categorized by description (name).

Articles of clothing will be fed in as line-delimited list. See below for examples.

Output Specifications

Output should be an alphabetically (case-insensitive) sorted, line-delimited list of the articles of clothing along with their count. Each field (count, category) should be separated by a pipe (|). If you come across a sock without a soulmate, the count should be designated by a 0 (zero). Socks that are in pairs should be on separate lines from the socks of the same category without pairs, and should come before the pairless sock. See below for examples.

Sample Input/Output

INPUT

white shirt
polka dot sock
red sock
superhero shirt
torn jeans
polka dot sock
white shirt
polka dot sock

OUTPUT

1|polka dot sock
0|polka dot sock
0|red sock
1|superhero shirt
1|torn jeans
2|white shirt

My attempt at a solution (https://codereview.stackexchange.com/questions/179931/bloomberg-laundry-day-count-items-in-list-check-socks-for-pairs-and-organize) is passing the sample test case (above) but failing secret test cases.

So, I'm wondering what extra information I should be extracting from the input and output specifications? For example, I'm storing my clothing and the associated counts in a TreeMap, but I first convert the clothing description into small caps, because I figure that a White Shirt is the same thing as a white shirt. But I've tried other things - I've tried (a) storing the original string and using CASE_INSENSITIVE_ORDER for my tree map, and (b) storing the original strings and the counts for those strings in a HashMap, and storing all unique original strings in an ArrayList, then sorting all the original strings by CASE_INSENSITIVE_ORDER at the end, then using this order to print the results. Each of these attempts succeeds on the sample test case and fails some secret test.

How can I write useful tests for this? I've tried the corner case of an empty input, and my program works as expected. What else can I look out for?

1 个答案:

答案 0 :(得分:0)

在您的示例代码中:

String clothingItem = stdin.nextLine().toLowerCase();
incrementCount(clothingCount, clothingItem);

它看起来像是存储小写字符串,而不是原始字符串。问题要求原始字符串,SORTED就像它不区分大小写一样。