How to resolve the algorithm Search a list of records step by step in the 8th programming language
How to resolve the algorithm Search a list of records step by step in the 8th 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 8th programming language
Source code in the 8th programming language
[
{ "name": "Lagos", "population": 21.0 },
{ "name": "Cairo", "population": 15.2 },
{ "name": "Kinshasa-Brazzaville", "population": 11.3 },
{ "name": "Greater Johannesburg", "population": 7.55 },
{ "name": "Mogadishu", "population": 5.85 },
{ "name": "Khartoum-Omdurman", "population": 4.98 },
{ "name": "Dar Es Salaam", "population": 4.7 },
{ "name": "Alexandria", "population": 4.58 },
{ "name": "Abidjan", "population": 4.4 },
{ "name": "Casablanca", "population": 3.98 }
] var, cities-raw
"Index of first occurrence of 'Dar Es Salaam': " .
"Dar Es Salaam" >r cities-raw @
(
"name" m:@ r@ s:= if
drop . cr ;;
then
2drop
) a:each drop rdrop
"The name of the first city in this list whose population is less than 5 million: " .
5 >r cities-raw @
(
nip
"population" m:@ r@ n:< if
"name" m:@ . cr break
then
drop
) a:each drop rdrop
"The population of the first city in this list whose name starts with the letter \"A\": " .
'A >r cities-raw @
(
nip
"name" m:@ 0 s:@ r@ n:= if
drop "population" m:@ . cr break
then
2drop
) a:each drop rdrop
bye
You may also check:How to resolve the algorithm Strip a set of characters from a string step by step in the Emacs Lisp programming language
You may also check:How to resolve the algorithm Sorting algorithms/Sleep sort step by step in the Elixir programming language
You may also check:How to resolve the algorithm Strip block comments step by step in the SNOBOL4 programming language
You may also check:How to resolve the algorithm Largest number divisible by its digits step by step in the Ring programming language
You may also check:How to resolve the algorithm Langton's ant step by step in the Ol programming language