How to resolve the algorithm Search a list of records step by step in the Wren programming language
How to resolve the algorithm Search a list of records step by step in the Wren 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 Wren programming language
Source code in the wren programming language
import "/dynamic" for Tuple
var Element = Tuple.create("Element", ["record", "index"])
var findFirst = Fn.new { |seq, pred|
var i = 0
for (e in seq) {
if (pred.call(e)) return Element.new(e, i)
i = i + 1
}
return Element.new(null, -1)
}
var City = Tuple.create("City", ["name", "pop"])
var cities = [
City.new("Lagos", 21.0),
City.new("Cairo", 15.2),
City.new("Kinshasa-Brazzaville", 11.3),
City.new("Greater Johannesburg", 7.55),
City.new("Mogadishu", 5.85),
City.new("Khartoum-Omdurman", 4.98),
City.new("Dar Es Salaam", 4.7),
City.new("Alexandria", 4.58),
City.new("Abidjan", 4.4),
City.new("Casablanca", 3.98)
]
var index = findFirst.call(cities) { |c| c.name == "Dar Es Salaam" }.index
System.print("Index of the first city whose name is 'Dar Es Salaam' is %(index).")
var city = findFirst.call(cities) { |c| c.pop < 5 }.record.name
System.print("First city whose population is less than 5 million is %(city).")
var pop = findFirst.call(cities) { |c| c.name[0] == "A" }.record.pop
System.print("The population of the first city whose name begins with 'A' is %(pop).")
You may also check:How to resolve the algorithm Sort disjoint sublist step by step in the ERRE programming language
You may also check:How to resolve the algorithm Guess the number step by step in the Rust programming language
You may also check:How to resolve the algorithm Hash from two arrays step by step in the Jsish programming language
You may also check:How to resolve the algorithm Simple database step by step in the Tcl programming language
You may also check:How to resolve the algorithm Check that file exists step by step in the COBOL programming language