Correct Apartment Bedroom Counts
Problem statement
We want to query an API endpoint to receive data about currently available apartment listings from a rental website. Among the data fields is a column called num_bedrooms, which takes the value of 1 for a "1-bedroom" apartment and 0 for a "studio".
Note: This rental agency only works with studios and 1-bedroom apartments, so there will never be 2+ bedroom listings. Each listing includes information about a "studio" or a "1-bedroom" apartment, so there will never be a listing with both a "studio" and "1-bedroom" offerings in one posting.
The algorithm used occasionally mistags the num_bedrooms value. Specifically, sometimes a "studio" is tagged as having num_bedrooms = 1 or a "1-bedroom" is tagged as num_bedrooms = 0. Further investigation revealed it to be an issue with one of the data fields, description, and the way our algorithm parsed the field to extract a num_bedrooms value.
For example: "description": "Beautiful 1-bedroom apartment with nearby yoga studio." was detected as a yoga studio instead of 1-bedroom and incorrectly had num_bedrooms = 0.
Your task is to write a function that takes in the jsonData and corrects this problem. The GET request retrieves the data as a string which looks like this:
jsonData = [
{
"id": "3",
"agent": "Ton Jett",
"unit": "#12",
"description": "Beautiful 1-bedroom apartment with nearby yoga studio.",
"num_bedrooms": 1
},
...
]While correcting the problem, remember the following edge cases:
- If the word
"studio"or"1-bedroom"is preceded immediately by any of the words:"yoga","dance"or"art", don't consider it fornum_bedroomsvalue. - If the description does not contain the word
"studio"or"1-bedroom", do not change the value fornum_bedrooms. - The rules above should be applied regardless of punctuation or letter casing within the description field.
Your end goal is to return an array of integers representing num_bedrooms for each rental listing, example: [0, 1, 1, 1, 0, 0].
The source labels the following case Example.
For
jsonData = "[{\"id\":\"1\",\"agent\":\"Radulf Katlego\",\"unit\":\"#3\",\"description\":\"This luxurious studio apartment is in the heart of downtown.\",\"num_bedrooms\":1},{\"id\":\"2\",\"agent\":\"Kelemen Konrad\",\"unit\":\"#36\",\"description\":\"We have a 1-bedroom available on the third floor.\",\"num_bedrooms\":1},{\"id\":\"3\",\"agent\":\"Ton Jett\",\"unit\":\"#12\",\"description\":\"Beautiful 1-bedroom apartment with nearby yoga studio.\",\"num_bedrooms\":1},{\"id\":\"4\",\"agent\":\"Fishel Salman\",\"unit\":\"#13\",\"description\":\"Beautiful studio with a nearby art studio.\",\"num_bedrooms\":1}]"the output should be solution(jsonData) = [0, 1, 1, 0].
The above jsonData represents the following JSON:
[
{
"id": "1",
"agent": "Radulf Katlego",
"unit": "#3",
"description": "This luxurious studio apartment is in the heart of downtown.",
"num_bedrooms": 1
},
{
"id": "2",
"agent": "Kelemen Konrad",
"unit": "#36",
"description": "We have a 1-bedroom available on the third floor.",
"num_bedrooms": 1
},
{
"id": "3",
"agent": "Ton Jett",
"unit": "#12",
"description": "Beautiful 1-bedroom apartment with nearby yoga studio.",
"num_bedrooms": 1
},
{
"id": "4",
"agent": "Fishel Salman",
"unit": "#13",
"description": "Beautiful studio with a nearby art studio.",
"num_bedrooms": 1
}
]FastPrep practice clarification
- The source's two explanation sentences using
followed byare retained verbatim below. For judging, follow the explicit source rule: ignore a target occurrence when its immediately preceding word isyoga,dance, orart. - Match the complete words
studioand1-bedroomcase-insensitively. Punctuation separates words, soART...studiohasartas the immediately preceding word. - Return corrected values in the same order as the listings in
jsonData. If no non-ignored target occurs, retain that listing's originalnum_bedrooms.
Function
solution(jsonData: String) → int[]Examples
Example 1
jsonData = "[{\"id\":\"1\",\"agent\":\"Radulf Katlego\",\"unit\":\"#3\",\"description\":\"This luxurious studio apartment is in the heart of downtown.\",\"num_bedrooms\":1},{\"id\":\"2\",\"agent\":\"Kelemen Konrad\",\"unit\":\"#36\",\"description\":\"We have a 1-bedroom available on the third floor.\",\"num_bedrooms\":1},{\"id\":\"3\",\"agent\":\"Ton Jett\",\"unit\":\"#12\",\"description\":\"Beautiful 1-bedroom apartment with nearby yoga studio.\",\"num_bedrooms\":1},{\"id\":\"4\",\"agent\":\"Fishel Salman\",\"unit\":\"#13\",\"description\":\"Beautiful studio with a nearby art studio.\",\"num_bedrooms\":1}]"return = [0, 1, 1, 0]In the first listing, description = "This luxurious studio apartment is in the heart of downtown."
"studio"should havenum_bedrooms = 0;
In the second listing, description = "We have a 1-bedroom available on the third floor."
"1-bedroom"should havenum_bedrooms = 1;
In the third listing, description = "Beautiful 1-bedroom apartment with nearby yoga studio."
"1-bedroom"should havenum_bedrooms = 1. Ignore"studio"since it is followed by"yoga".
In the fourth listing, description = "Beautiful studio with a nearby art studio."
"studio"should havenum_bedrooms = 0. Ignore the second appearance of"studio"since it is followed by"art".
Example 2
jsonData = "[{\"id\":\"5\",\"agent\":\"Ari Lane\",\"unit\":\"#21\",\"description\":\"Bright YOGA, studio next to transit.\",\"num_bedrooms\":1},{\"id\":\"6\",\"agent\":\"Mina Park\",\"unit\":\"#22\",\"description\":\"A DANCE/1-BEDROOM rehearsal room is listed nearby.\",\"num_bedrooms\":0},{\"id\":\"7\",\"agent\":\"Noah Reed\",\"unit\":\"#23\",\"description\":\"Quiet apartment near the river.\",\"num_bedrooms\":1}]"return = [1, 0, 1]FastPrep practice example: YOGA, studio and DANCE/1-BEDROOM are ignored because punctuation does not break the immediately preceding-word relationship. The last listing has no target word, so its original value remains 1.
Constraints
- Input/Output
[execution time limit] 4 seconds (py3)[memory limit] 1 GB[input] string jsonData- String in JSON format. It's guaranteed that each listing contains the fields
"id","agent","unit","description"and"num_bedrooms". - Guaranteed constraints:
136 ≤ jsonData.length ≤ 15366.
- String in JSON format. It's guaranteed that each listing contains the fields
[output] array.integer- Return an array that contains the correct values for
num_bedroomsfor all of the listings injsonData.
- Return an array that contains the correct values for
- FastPrep practice clarification:
jsonDatais valid standard JSON;descriptionis a string,num_bedroomsis0or1, and each source target or modifier is matched as a complete ASCII word.
Source note: The first four source images show the complete prompt, worked example, and original limits. The fifth shows the author's illustrative Python reference; where it differs, the explicit rule and worked example control.