Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update the instructions and the answer of the Ballerina billionare exercise #228

Open
wants to merge 16 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 13 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,20 @@

You have been asked to find the top `x` billionaires by net worth in a given set of countries (number of countries = `N`). To implement that, you have been given access to an API that returns the details of the billionaires by country.

To simplify accessing the API, you can use the [`ims/billionairehub` client](https://lib.ballerina.io/ims/billionairehub/1.0.0/clients/Client) connector to connect to the API which returns details of the billionaires by country.
To simplify accessing the API, you can use a predefined client connector to connect to the API which returns details of the billionaires by country.


```ballerina
import ims/billionairehub;
import ballerina_exercism/http_billion_dollar_question.billionarehub_server as _;
import ballerina_exercism/http_billion_dollar_question.billionarehub_client as billionarehubClient;

# Client ID and Client Secret to connect to the billionaire API
configurable string clientId = ?;
configurable string clientSecret = ?;
type Billionaire record {
string name;
float netWorth;
};

public function getTopXBillionaires(string[] countries, int x) returns string[]|error {
// Create the client connector
billionairehub:Client cl = check new ({auth: {clientId, clientSecret}});
billionarehubClient:BillionareClient cl = check new();

// TODO Write your logic here
return [];
Expand Down Expand Up @@ -55,16 +56,15 @@ Limit: 5
Output

```
["Mukesh Ambani", "Gautam Adani & family", "Zhong Shanshan", "Zhang Yiming", "Ma Huateng"]
["Mukesh Ambani","Zhong Shanshan","Ma Huateng","Colin Huang","Pony Ma"]
```

## Test Environment

* Client ID and Client Secret to connect to the billionaire API have been provided to you in the `tests/Config.toml`. You can use them to initialize the client.
* Use `bal test` to run and test your implementation.

## Hints

* `ims/billionairehub` package's [client](https://lib.ballerina.io/ims/billionairehub/1.0.0/clients/Client) can be used to invoke the get billionaires by country API. You can use the `getBillionaires("<country>")` remote method specifically.
* `http_billion_dollar_question` package's `billionarehub_client` module can be used to invoke the get billionaires by country API. You can use the `getBillionaires("<country>")` remote method specifically.
* You can find the richest people per country first and then combine them to find the top `x`
* [Query expressions](https://ballerina.io/learn/by-example/query-expressions/) will be convenient for filtering and sorting
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,5 @@
"icon": "elons-toys",
"blurb": "Can you find the top billionaires by net worth in a given set of countries?",
"source": "This is an exercise to introduce users to using Ballerina HTTP service data binding",
"source_url": "https://ballerina.io/learn/by-example/http-data-binding"
"source_url": "https://ballerina.io/learn/by-example/http-service-data-binding/"
}
Original file line number Diff line number Diff line change
@@ -1,24 +1,13 @@
import ballerina/io;
import ims/billionairehub;
import ballerina_exercism/http_billion_dollar_question.billionarehub_server as _;
import ballerina_exercism/http_billion_dollar_question.billionarehub_client as billionarehubClient;

configurable string clientId = ?;
configurable string clientSecret = ?;

public function main() returns error? {
billionairehub:Client cl = check new ({auth: {clientId, clientSecret}});
billionairehub:Billionaire[] billionaires = check cl->getBillionaires("France");
io:println(billionaires);

map<boolean> industries = {};
check from var b in billionaires
do {
industries[b.industry] = true;
};
io:println(industries.keys());
}
type Billionaire record {
string name;
float netWorth;
};

public function getTopXBillionaires(string[] countries, int x) returns string[]|error {
billionairehub:Billionaire[] billionaires = [];
Billionaire[] billionaires = [];
foreach string country in countries {
billionaires.push(...check getTopXBillionairesByCountry(country, x));
}
Expand All @@ -29,11 +18,11 @@ public function getTopXBillionaires(string[] countries, int x) returns string[]|
select b.name;
}

function getTopXBillionairesByCountry(string country, int x) returns billionairehub:Billionaire[]|error {
billionairehub:Client cl = check new ({auth: {clientId, clientSecret}});
billionairehub:Billionaire[] billionaires = check cl->getBillionaires(country);
function getTopXBillionairesByCountry(string country, int x) returns Billionaire[]|error {
billionarehubClient:BillionareClient cl = check new();
Billionaire[] billionaires = check cl->getBillionaires(country);
return from var b in billionaires
order by b.netWorth descending
limit x
select b;
}
}
18 changes: 12 additions & 6 deletions exercises/practice/http-billion-dollar-question/main.bal
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
import ims/billionairehub;
import ballerina_exercism/http_billion_dollar_question.billionarehub_server as _;
import ballerina_exercism/http_billion_dollar_question.billionarehub_client as billionarehubClient;

# Client ID and Client Secret to connect to the billionaire API
configurable string clientId = ?;
configurable string clientSecret = ?;
type Billionaire record {
string name;
float netWorth;
};

# Returns the top billionares of the given countries.
#
# + countries - countries to search for billionares
# + x - number of billionares to return
# + return - list of billionares
public function getTopXBillionaires(string[] countries, int x) returns string[]|error {
// Create the client connector
billionairehub:Client cl = check new ({auth: {clientId, clientSecret}});
billionarehubClient:BillionareClient cl = check new();

// TODO Write your logic here
return [];
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import ballerina/http;

type Billionaire record {
string name;
float netWorth;
};

const string clientUrl = "http://localhost:9090";

public client class BillionareClient {
private final http:Client httpClient;

public function init() returns error? {
self.httpClient = check new (clientUrl);
}

remote function getBillionaires(string country) returns Billionaire[]|error {
return self.httpClient->/getBillionaires(country=country);
}
}
Loading