For twenty years, HOEPLI. it has been the e-commerce service of one of the largest bookshops in Italy and Europe, founded back in 1870 and based in Milan. It boasts a catalogue of over 500, 000 books, a range of discounts and offers that are always up-to-date and extremely convenient, and an extremely fast delivery service that allows orders to be delivered throughout Italy in just a few days. HOEPLI. it is an absolute point of reference for book lovers, thanks to its extremely rich and always up-to-date offer, and a system of news and book recommendations guaranteed by the work of forty specialised booksellers. The available titles range from classic to modern literature, photography books, technical and specialized texts for school, preparation texts for university exams, non-fiction and everything that is available in the publishing market you will find at Hoepli. it. Happy reading!
To solve this problem, we need to reformat system requirements given in JSON format into a structured HTML output. The requirements can either be provided as a list of key-value pairs for a single system or as separate specifications for multiple systems (e.g., Windows, Mac, Linux).
### Approach
1. **Check Input Structure**: Determine if the input is a list of key-value pairs for a single system or includes separate specifications for multiple systems.
2. **Parse Requirements**:
- If the input includes separate systems (e.g., "Windows", "Mac", "Linux"), parse each system's requirements from an HTML string and extract each list item.
- If the input is a single system, directly use the key-value pairs.
3. **Generate HTML Structure**:
- For each system, create a `div` element with a heading indicating the system name.
- Include a paragraph with "MINIMUM SPECS".
- Construct an unordered list and populate it with the parsed requirements.
4. **Construct the Complete HTML Output**: Combine all system requirement sections into a single HTML string without any markdown or additional formatting.
### Solution Code
```python
import json
def reformat_system_requirements(json_input):
# Parse the JSON input
data = json.loads(json_input)
output_html = []
for item in data:
if 'system' in item:
# Handle case where each item is for a different OS
system = item['system']
req_html = item['requirement']
# Parse the requirement HTML to extract list items
# This is a simplified approach using string splitting
li_start = '
'
li_end = ''
items = req_html.split(li_start)[1:-1] # Exclude the first and last which are parts before first li and after last li
# Process each item to extract span and text
requirements = []
for li in items:
span_start = '
'
span_end = ''
content = li.split(span_start)
if len(content) < 2:
continue # skip if span not found
key_part = content[1].split(span_end)[0].strip()
value_part = li.split('')[1].split(li_end)[0].strip()
requirements.append((key_part, value_part))
# Create the HTML div for this system
div_html = f'
{system} System Requirements
MINIMUM SPECS
'
for key, value in requirements:
div_html += f'- {key} {value}
'
div_html += '
'
output_html.append(div_html)
else:
# Handle single system case, assuming all items are for the same system
if len(output_html) == 0:
# Initialize the div
div_html = '
PC System Requirements
MINIMUM SPECS
'
for key in item:
value = item[key]
div_html += f'- {key}: {value}
'
div_html += '
'
output_html.append(div_html)
# Combine all parts
final_html = ''.join(output_html)
return final_html
# Example usage:
json_input = '[{"OS":"Windows XP or Windows Vista"},{"Processor":"1.8 GHz"},{"Memory":"512MB RAM (1 GB recommended)"},{"Graphics":"3D graphics card compatible with DirectX 8 (compatible with DirectX 9 recommended)"},{"Hard Drive":"2GB"},{"Additional":"Mouse, Keyboard"}]'
print(reformat_system_requirements(json_input))
```
### Explanation
The code provided converts the input JSON into the required HTML format. It handles both single system specifications and multiple systems (e.g., Windows, Mac, Linux) separately. The approach involves:
- Parsing the input JSON to determine if it includes separate systems or a single system.
- For each system, constructing a `div` element with a heading, a "MINIMUM SPECS" paragraph, and an unordered list of requirements.
- Extracting and formatting each requirement correctly, whether it's a simple key-value pair or part of an HTML string.
This method ensures the output is clean, well-structured, and adheres to the specified guidelines.
To download your Gift Card you will receive security codes with an expiration date and usage instructions.
分享