How to resolve the algorithm Search a list of records step by step in the Ring programming language
How to resolve the algorithm Search a list of records step by step in the Ring 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 Ring programming language
Source code in the ring programming language
# Project : Search a list of records
cities = [[: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]]
for n = 1 to len(cities)
if cities[n][:name] = "Dar Es Salaam"
see n-1 + nl
ok
next
for n = 1 to len(cities)
if cities[n][:population] < 5.00
see cities[n][:name] + nl
exit
ok
next
for n = 1 to len(cities)
if left(cities[n][:name],1) = "A"
see cities[n][:population] + nl
exit
ok
next
You may also check:How to resolve the algorithm Thue-Morse step by step in the CLU programming language
You may also check:How to resolve the algorithm Roots of unity step by step in the jq programming language
You may also check:How to resolve the algorithm Write float arrays to a text file step by step in the ALGOL 68 programming language
You may also check:How to resolve the algorithm Set step by step in the Maple programming language
You may also check:How to resolve the algorithm Delete a file step by step in the HicEst programming language