The coast redwood tree species, Sequoia sempervirens, includes some of the oldest and tallest living organisms on Earth. Some details concerning individual trees are given in the tab-delimited text file redwood-data.txt
(data courtesy of the Gymnosperm database.)
Write a Python program to read in this data and report the tallest tree and the tree with the greatest diameter.
Although there are better ways of analysing these data (for example, using NumPy), one pure-Python solution is as follows:
data_file = 'redwood-data.txt'
# Indexes of the fields in each record:
NAME, LOCATION, DIAMETER, HEIGHT = 0, 1, 2, 3
f = open(data_file)
# Skip the header lines
f.readline()
f.readline()
tree_data = []
max_height, max_diameter = 0, 0
for i, line in enumerate(f.readlines()):
records = line.split('\t')
diameter = records[DIAMETER] = float(records[DIAMETER])
height = records[HEIGHT] = float(records[HEIGHT])
if diameter > max_diameter:
max_diameter, max_diameter_index = diameter, i
if height > max_height:
max_height, max_height_index = height, i
tree_data.append(records)
highest_tree = tree_data[max_height_index]
print('The highest tree: {}, {} m in {}'.format(highest_tree[NAME],
highest_tree[HEIGHT], highest_tree[LOCATION]))
widest_tree = tree_data[max_diameter_index]
print('The widest tree: {}, with diameter {} m in {}'.format(widest_tree[NAME],
widest_tree[DIAMETER], widest_tree[LOCATION]))
The output is:
The highest tree: Hyperion, 115.61 m in Redwood National Park
The widest tree: Stratosphere Giant, with diameter 5.18 m in Humboldt
Redwoods State Park