Find the brands you love at prices that work for you with a TJX gift card! Our buyers negotiate amazing deals with top designers and pass the savings to you. Departments include apparel, shoes, home, beauty, and accessories. The TJX gift card is redeemable at over 3200 T. J. Maxx, Marshalls, HomeGoods, Homesense and Sierra (in the U. S. and Puerto Rico) and online at tjmaxx. com, sierra. com and marshalls. com.
The solution involves analyzing the input structure and generating the appropriate HTML based on whether each object represents a separate system or part of a single system's requirements.
### Approach
1. **Identify Input Structure**: Check if any object in the input array contains a "system" key. This determines whether we're dealing with multiple systems or a single system's requirements.
2. **Process Multiple Systems**: If "system" key exists, each object is treated as a separate system. Extract the system name and its requirements, then generate an HTML section for each.
3. **Process Single System**: If no "system" key is found, collect all key-value pairs from all objects and format them into a single HTML section.
4. **Generate HTML**: Construct the HTML string for each system or the combined requirements, ensuring proper structure and indentation.
### Solution Code
```python
import json
def reformat_html(json_str):
data = json.loads(json_str)
has_system = any('system' in obj for obj in data)
html_parts = []
if has_system:
for obj in data:
system_name = obj.get('system', '')
requirements = obj.get('requirement', '')
html = f'''
{system_name} System Requirements
MINIMUM SPECS
{requirements}
'''
html_parts.append(html)
else:
requirements = []
for obj in data:
for key, value in obj.items():
requirements.append(f'
{key}: {value}')
html = f'''
PC System Requirements
MINIMUM SPECS
'''
html_parts.append(html)
return '\n
\n
\n'.join(html_parts)
# Example usage:
input_json = '''
[
{"system":"Windows","requirement":"
- OS: Windows 7 or higher
- Processor: 1.5 Ghz
- Memory: 2 GB RAM
- Graphics: 256MB DirectX 9 or higher
- Storage: 1 GB available space
- Additional Notes: A printed copy of the Bomb Defusal Manual required. HTC Vive or Oculus Rift/DK2 required for VR play.
"},
{"system":"Mac","requirement":"
- OS: OS X 10.9 or later
- Processor: 1.5GHz Intel Processor
- Graphics: 256MB or higher
- Storage: 1 GB available space
- Additional Notes: Virtual reality not supported.
"}
]
'''
output_html = reformat_html(input_json)
print(output_html)
```
### Explanation
- **Input Handling**: The input is parsed as JSON to extract data structures. Each object is checked for the presence of a "system" key to determine if it represents a separate system.
- **HTML Generation**: For each identified system, the corresponding HTML section is generated with appropriate headers and requirements. If handling a single system, all key-value pairs from all objects are compiled into a single HTML section.
- **String Formatting**: The HTML is constructed with proper nesting and indentation to match the required structure, ensuring clarity and correctness in the output.
This approach efficiently processes both types of input structures and generates the corresponding HTML output as specified.
分享