How to resolve the algorithm Search a list of records step by step in the Ada programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Search a list of records step by step in the Ada programming language

Table of Contents

Problem Statement

Many programming languages provide convenient ways to look for a known value in a simple list of strings or numbers. But what if the elements of the list are themselves compound records/objects/data-structures, and the search condition is more complex than a simple equality test? Write a function/method/etc. that can find the first element in a given list matching a given condition. It should be as generic and reusable as possible. (Of course if your programming language already provides such a feature, you can use that instead of recreating it.) Then to demonstrate its functionality, create the data structure specified under #Data set, and perform on it the searches specified under #Test cases. The data structure to be used contains the names and populations (in millions) of the 10 largest metropolitan areas in Africa, and looks as follows when represented in JSON: However, you shouldn't parse it from JSON, but rather represent it natively in your programming language.

If any of that is impossible or unreasonable in your programming language, then feel free to deviate, as long as you explain your reasons in a comment above your solution. If your programming language supports higher-order programming, then the most elegant way to implement the requested functionality in a generic and reusable way, might be to write a function (maybe called "find_index" or similar), that takes two arguments: If this is not the approach which would be most natural or idiomatic in your language, explain why, and show what is.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Search a list of records step by step in the Ada programming language

Source code in the ada programming language

with Ada.Strings.Unbounded; use Ada.Strings.Unbounded;
with Ada.Text_IO;

procedure Search_A_List_Of_Records
is
   function "+"(input : in String) return Unbounded_String renames To_Unbounded_String;
   function "+"(input : in Unbounded_String) return String renames To_String;

   type City is record
      name : Unbounded_String;
      population : Float;
   end record;

   type City_Array is array(Positive range <>) of City;
   type City_Array_Access is access City_Array;

   type Cursor is record
      container : City_Array_Access;
      index : Natural;
   end record;

   function Element(C : in Cursor) return City is
   begin
      if C.container = null or C.index = 0 then
         raise Constraint_Error with "No element.";
      end if;

      return C.container.all(C.index);
   end Element;

   function Index_0(C : in Cursor) return Natural is
   begin
      if C.container = null or C.index = 0 then
         raise Constraint_Error with "No element.";
      end if;

      return C.index - C.container.all'First;
   end Index_0;

   function Find
     (container : in City_Array;
      check : not null access function(Element : in City) return Boolean)
      return Cursor
   is
   begin
      for I in container'Range loop
         if check.all(container(I)) then
            return (new City_Array'(container), I);
         end if;
      end loop;
      return (null, 0);
   end;

   function Dar_Es_Salaam(Element : in City) return Boolean is
   begin
      return Element.name = "Dar Es Salaam";
   end Dar_Es_Salaam;

   function Less_Than_Five_Million(Element : in City) return Boolean is
   begin
      return Element.population < 5.0;
   end Less_Than_Five_Million;

   function Starts_With_A(Item : in City) return Boolean is
   begin
      return Element(Item.name, 1) = 'A';
   end Starts_With_A;

   cities : constant City_Array :=
     ((+"Lagos",                21.0),
      (+"Cairo",                15.2),
      (+"Kinshasa-Brazzaville", 11.3),
      (+"Greater Johannesburg", 7.55),
      (+"Mogadishu",            5.85),
      (+"Khartoum-Omdurman",    4.98),
      (+"Dar Es Salaam",        4.7 ),
      (+"Alexandria",           4.58),
      (+"Abidjan",              4.4 ),
      (+"Casablanca",           3.98));
begin
   Ada.Text_IO.Put_Line(Index_0(Find(cities, Dar_Es_Salaam'Access))'Img);
   Ada.Text_IO.Put_Line(+Element(Find(cities, Less_Than_Five_Million'Access)).name);
   Ada.Text_IO.Put_Line(Element(Find(cities, Starts_With_A'Access)).population'Img);
end Search_A_List_Of_Records;


  

You may also check:How to resolve the algorithm Apply a callback to an array step by step in the Picat programming language
You may also check:How to resolve the algorithm Munching squares step by step in the PL/I programming language
You may also check:How to resolve the algorithm Arithmetic-geometric mean step by step in the Scala programming language
You may also check:How to resolve the algorithm Host introspection step by step in the Phix programming language
You may also check:How to resolve the algorithm Ray-casting algorithm step by step in the Racket programming language