| about | help | code help+videos | done | prefs |
apcsaArraysSensorStatus
HARDER PROBLEM: skip if you are focusing on basic practice
A smart home sensor sends data in a single String separated by pipes |. For example: "temp=72|status=ready". However, sometimes a value needs to include a pipe, so it uses an escape sequence \| like "id=1|temp=98|status=okey\\|dokey". Precondition: The sensor protocol never allows spaces in its data. Given a String input with these characteristics, find and return the longest single element, with pipes included if they were escaped in the original.
HINTS
Use a helper method (or the existing String replace method) to change the escape sequences into spaces because those are guaranteed not to exist in the data, before the split operation. Remember to change them back again afterward! Also: the backslash character must itself be escaped in most Java strings. Finally, the split method of a String doesn't actually accept an ordinary String as a parameter, it accepts a String representing a regular expression pattern. Discussing regex is (way) outside the scope of AP CSA, but the pipe "|" character is special in regular expressions and must itself be escaped, with a backslash that also (yikes) needs escaping as a Java literal. Your split operation is therefore going to need to look like split("\\|") to work correctly. apcsaArraysSensorStatus("id=1|temp=98|status=ok") → "status=ok" apcsaArraysSensorStatus("id=1|temp=98|status=okey\\|dokey") → "status=okey|dokey" apcsaArraysSensorStatus("a|bb|ccc") → "ccc" ...Save, Compile, Run (ctrl-enter) |
Progress graphs:
Your progress graph for this problem
Random user progress graph for this problem
Random Epic Progress Graph
Difficulty: 320
Copyright Nick Parlante 2017 - privacy