02:16:00
IPZ-870 白川麻衣的眼前夺妻 inline<|endoftext|>A database contains information about different geographical locations. Each location has an ID (an integer), a name (a non-empty string), and a list of periods (a list of strings). Each period consists of a start year and an end year (both integers, with the start year being less than or equal to the end year), representing the range of time during which the location had a specific characteristic.
For instance, a location with ID 1, named "New York", may have periods ["1950-2000", "2010-2015"] indicating it had characteristic A from 1950 to 2000 and characteristic B from 2010 to 2015.
Write a function named `location_characteristic` that takes a database (a list of locations, each represented as a tuple (ID, name, periods)), and a year as inputs, and returns the characteristic of the location at that specific year, if it exists. If no characteristic is defined for that year, return an empty string.
Functional signature:
```python
def location_characteristic(database: List[Tuple[int, str, List[str]]], year: int) -> str:
```
Example:
```python
database = [
(1, "New York", ["1950-2000", "2010-2015"]),
(2, "Los Angeles", ["1930-1950", "2005-"]),
]
year = 2014
# The output should be "B" because in 2014, Los Angeles had the characteristic B.
location_characteristic(database, 2014) => "B"
```relude
Algorithm:
1. Initialize an empty string `characteristic` to store the result.
2. For each location in the database, check if the year falls within any of the periods.
3. If a period is found, determine the characteristic from that period and update `characteristic`.
4. Continue checking until all locations are processed or a characteristic is found.
5. Return the characteristic found, or an empty string if no characteristic is found.
Here is the Python code snippet with the problem description and a check function to verify the correctness:
```python
from typing import List, Tuple
def location_characteristic(database: List[Tuple[int, str, List[str]]], year: int) -> str:
"""
Given a database of locations with characteristics defined by periods, and a year,
returns the characteristic of the location at the given year.
"""
# Initialize the characteristic to an empty string
characteristic = ""
for location_id, name, periods in database:
for period in periods:
start_year, end_year = map(int, period.split("-"))
if start_year <= year <= end_year:
# Assuming the characteristic is denoted by the second letter of the period string
characteristic = period[1]
break
return characteristic
# Check function to verify the solution with provided data points
def check_solution():
database = [
(1, "New York", ["1950-2000", "2010-2015"]),
(2, "Los Angeles", ["1930-1950", "2005-"]),
]
assert location_characteristic(database, 1940) == "A", "Test case 1 failed"
assert location_characteristic(database, 2000) == "A", "Test case 2 failed"
assert location_characteristic(database, 2014) == "B", "Test case 3 failed"
assert location_characteristic(database, 2020) == "", "Test case 4 failed"
check_solution()
```
Note: The characteristic is assumed to be denoted by the second letter of the period string for simplicity in this solution. The actual method to derive the characteristic from the period string should be aligned with the problem's specifications.
12月24日2016年