Algorithms

EasyMediumHard
123456789101112
Question 4
Hard

An astronomical database stores information about constellations using their standard three-letter abbreviations (e.g. "AND" for Andromeda, "CYG" for Cygnus, "ORI" for Orion) to quickly query average distance data. There are 88 official constellations.

A program is required to verify if a user-entered three-letter abbreviation is present and lookup its distance from Earth (in million light-years).

Each constellation abbreviation with its distance is stored as a record in a two-dimensional list, constellationTable. The records are sorted in alphabetical order by their constellation abbreviation.

Open file constellation_search.py

Write a program to meet these requirements:

Inputs

  • Prompt for and accept a three-letter constellation abbreviation from the user (AAA to ZZZ, inclusive).
    • Accept both uppercase and lowercase inputs.
    • No other validation of the user input is required.

Process

  • Create an ordered linear search to locate the abbreviation in constellationTable.
    • Stop the search as soon as:
      • The abbreviation is located.
      • The expected alphabetical location of the user's abbreviation is passed.
      • The end of the list is reached after all records have been checked.
    • Ensure the search works correctly for a constellationTable of any length.

Outputs

  • When the constellation abbreviation is located, output the abbreviation and its distance.
  • When the expected alphabetical location is passed, output the next available constellation abbreviation alphabetical match in the list and its distance.
  • When the end of the list is reached without finding or passing the abbreviation, output the last constellation abbreviation in the list and its distance.

Use comments, white space and layout to make the program easier to read and understand.

Do not add any additional functionality.

Save your amended code as constellation_search_finished.py

[15]

Algorithms Questions

  1. GCSE
  2. /Computer Science
  3. /Algorithms