How to resolve the algorithm Search a list of records step by step in the Nim 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 Nim 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 Nim programming language

Source code in the nim programming language

template findIt(data, pred: untyped): int =
  ## Return the index of the first element in "data" satisfying
  ## the predicate "pred" or -1 if no such element is found.
  var result = -1
  for i, it {.inject.} in data.pairs:
    if pred:
      result = i
      break
  result


when isMainModule:

  import strutils

  type City = tuple[name: string; population: float]

  const Cities: seq[City] = @[("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)]

  echo "Index of the first city whose name is “Dar Es Salaam”: ",
      Cities.findIt(it.name == "Dar Es Salaam")

  let idx1 = Cities.findIt(it.population < 5)
  echo "Name of the first city whose population is less than 5 million: ",
      if idx1 == -1: "<none>" else: Cities[idx1].name

  let idx2 = Cities.findIt(it.name.startsWith("A"))
  echo "Population of the first city whose name starts with the letter “A”: ",
      if idx2 == -1: "<none>" else: $Cities[idx2].population


  

You may also check:How to resolve the algorithm Stack step by step in the PureBasic programming language
You may also check:How to resolve the algorithm Perfect shuffle step by step in the Fortran programming language
You may also check:How to resolve the algorithm Tokenize a string with escaping step by step in the Kotlin programming language
You may also check:How to resolve the algorithm Cantor set step by step in the Kotlin programming language
You may also check:How to resolve the algorithm Array length step by step in the i programming language