This document outlines a task to process data from a rental website's API. The goal is to retrieve information about available apartment listings, specifically focusing on the num_bedrooms field. This field indicates whether an apartment is a "1-bedroom" (value of 1) or a "studio" (value of 0).
It's important to note that this rental agency exclusively handles studios and 1-bedroom apartments, meaning listings with two or more bedrooms will not be present. Each listing pertains to either a "studio" or a "1-bedroom" apartment, so a single posting will not represent both.
An issue has been identified where the num_bedrooms value is occasionally mistagged. Specifically, studios are sometimes tagged with num_bedrooms = 1, and 1-bedroom apartments with num_bedrooms = 0. Further investigation revealed that this problem stems from how the description field is parsed by the algorithm.
For instance, a description like "Beautiful 1-bedroom apartment with nearby yoga studio." was incorrectly interpreted as a yoga studio, resulting in num_bedrooms = 0 instead of the correct value of 1.
Your task is to develop a function that takes JSON data as input and corrects these num_bedrooms misassignments. The data is retrieved as a string from a GET request, structured like this:
json
[
{
"id": "3",
"agent": "Ton Jett",
"unit": "#12",
"description": "Beautiful 1-bedroom apartment with nearby yoga studio.",
"num_bedrooms": 1
},
...
]
When implementing the correction, adhere to the following rules and edge cases:
- Special Preceding Words: If the words "studio" or "1-bedroom" are immediately preceded by "yoga", "dance", or "art", do not use these terms to determine the
num_bedrooms value.
- No Relevant Keywords: If the
description does not contain either "studio" or "1-bedroom", the num_bedrooms value should remain unchanged.
- Case and Punctuation Insensitivity: These rules must be applied irrespective of punctuation or letter casing within the
description field.
The ultimate objective is to return an array of integers, where each integer represents the corrected num_bedrooms value for each rental listing. For example: [0, 1, 1, 1, 0, 0].
Example
Given the following jsonData string:
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}]"
The function solution(jsonData) should return: [0, 1, 1, 0].
Explanation of Example:
- Listing 1:
description = "This luxurious studio apartment is in the heart of downtown."
- "studio" should correctly result in
num_bedrooms = 0.
- Listing 2:
description = "We have a 1-bedroom available on the third floor."
- "1-bedroom" should correctly result in
num_bedrooms = 1.
- Listing 3:
description = "Beautiful 1-bedroom apartment with nearby yoga studio."
- "1-bedroom" should correctly result in
num_bedrooms = 1. The term "studio" is ignored because it is preceded by "yoga".
- Listing 4:
description = "Beautiful studio with a nearby art studio."
- The first "studio" should result in
num_bedrooms = 0. The second instance of "studio" is ignored because it is preceded by "art".
Input/Output Specification:
- Execution Time Limit: 4 seconds (py3)
- Memory Limit: 1 GB
- Input: A string named
jsonData in JSON format. It is guaranteed that each listing includes the fields "id", "agent", "unit", "description", and "num_bedrooms".
- Guaranteed Constraints:
136 ≤ jsonData.length ≤ 15366.
- Output: An array of integers representing the corrected
num_bedrooms values for all listings.
Note for Python 3 users: If using a NumPy array, ensure the final returned array consists of standard Python int types, not NumPy int64 types. When casting a NumPy array to a list, it produces a list of numpy.int64 integers.