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 14 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,21 @@

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.billionairehub_server as _;
import ballerina_exercism/http_billion_dollar_question.billionairehub_client as billionairehubClient;

# 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}});
billionairehubClient:billionaireClient cl = check new();

// TODO Write your logic here
return [];
Expand Down Expand Up @@ -55,16 +57,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 `billionairehub_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.billionairehub_server as _;
import ballerina_exercism/http_billion_dollar_question.billionairehub_client as billionairehubClient;

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,9 +18,9 @@ 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 {
billionairehubClient:BillionaireClient cl = check new();
Billionaire[] billionaires = check cl->getBillionaires(country);
return from var b in billionaires
order by b.netWorth descending
limit x
Expand Down
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.billionairehub_server as _;
import ballerina_exercism/http_billion_dollar_question.billionairehub_client as billionairehubClient;

# 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 billionaires of the given countries.
#
# + countries - countries to search for billionaires
# + x - number of billionaires to return
# + return - list of billionaires
public function getTopXBillionaires(string[] countries, int x) returns string[]|error {
// Create the client connector
billionairehub:Client cl = check new ({auth: {clientId, clientSecret}});
billionairehubClient:BillionaireClient 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 BillionaireClient {
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);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"United States":[{"name":"Elon Musk", "netWorth":177}, {"name":"Jeff Bezos", "netWorth":151}, {"name":"Bill Gates", "netWorth":124}, {"name":"Mark Zuckerberg", "netWorth":97}, {"name":"Larry Ellison", "netWorth":93}, {"name":"Larry Page", "netWorth":91.5}, {"name":"Sergey Brin", "netWorth":89}, {"name":"Warren Buffett", "netWorth":88}, {"name":"Steve Ballmer", "netWorth":68}, {"name":"Mukesh Ambani", "netWorth":84}], "China":[{"name":"Zhong Shanshan", "netWorth":68}, {"name":"Zhang Yiming", "netWorth":35}, {"name":"Ma Huateng", "netWorth":65}, {"name":"Jack Ma", "netWorth":48}, {"name":"Colin Huang", "netWorth":55}, {"name":"Pony Ma", "netWorth":55}, {"name":"He Xiangjian", "netWorth":43}, {"name":"Yang Huiyan & family", "netWorth":29}, {"name":"Wang Xing", "netWorth":27}, {"name":"William Lei Ding", "netWorth":27}], "India":[{"name":"Mukesh Ambani", "netWorth":84}, {"name":"Gautam Adani & family", "netWorth":50}, {"name":"Shiv Nadar", "netWorth":23}, {"name":"Radhakishan Damani", "netWorth":16}, {"name":"Lakshmi Mittal", "netWorth":18}, {"name":"Hinduja brothers", "netWorth":17}, {"name":"Cyrus Poonawalla", "netWorth":12}, {"name":"Uday Kotak", "netWorth":15}, {"name":"Sunil Mittal & family", "netWorth":11}, {"name":"Kumar Birla", "netWorth":10}], "Japan":[{"name":"Tadashi Yanai & family", "netWorth":31}, {"name":"Takemitsu Takizaki", "netWorth":28}, {"name":"Masayoshi Son", "netWorth":29}, {"name":"Nobutada Saji & family", "netWorth":28}, {"name":"Takemitsu Takizaki", "netWorth":28}, {"name":"Hiroshi Mikitani", "netWorth":24}, {"name":"Yasumitsu Shigeta", "netWorth":24}, {"name":"Takemitsu Takizaki", "netWorth":28}, {"name":"Takemitsu Takizaki", "netWorth":28}, {"name":"Takemitsu Takizaki", "netWorth":28}], "Hong Kong":[{"name":"Li Ka-shing", "netWorth":35}, {"name":"Lee Shau Kee", "netWorth":28}, {"name":"Raymond Kwok & family", "netWorth":17}, {"name":"Thomas Kwok & family", "netWorth":17}, {"name":"Peter Woo", "netWorth":14}, {"name":"Joseph Lau", "netWorth":17}, {"name":"Cheng Yu-tung & family", "netWorth":17}, {"name":"Lui Che Woo & family", "netWorth":15}, {"name":"Henry Cheng & family", "netWorth":15}, {"name":"Michael Kadoorie & family", "netWorth":13}], "Russia":[{"name":"Vladimir Lisin", "netWorth":24}, {"name":"Vladimir Potanin", "netWorth":23}, {"name":"Alexey Mordashov & family", "netWorth":22}, {"name":"Leonid Mikhelson", "netWorth":20}, {"name":"Gennady Timchenko", "netWorth":20}, {"name":"Vagit Alekperov", "netWorth":19}, {"name":"Alisher Usmanov", "netWorth":19}, {"name":"Viktor Vekselberg", "netWorth":17}, {"name":"Roman Abramovich", "netWorth":17}, {"name":"Andrey Melnichenko", "netWorth":16}], "Italy":[{"name":"Leonardo Del Vecchio & family", "netWorth":25}, {"name":"Stefano Pessina", "netWorth":12}, {"name":"Massimiliana Landini Aleotti & family", "netWorth":13}, {"name":"Giovanni Ferrero", "netWorth":35}, {"name":"Giorgio Armani", "netWorth":8}, {"name":"Sergio Marchionne", "netWorth":5}, {"name":"Stefano Pessina", "netWorth":12}, {"name":"Massimiliana Landini Aleotti & family", "netWorth":13}, {"name":"Giovanni Ferrero", "netWorth":35}, {"name":"Giorgio Armani", "netWorth":8}], "France":[{"name":"Bernard Arnault & family", "netWorth":150}, {"name":"Francoise Bettencourt Meyers & family", "netWorth":71}, {"name":"Francois Pinault & family", "netWorth":42}, {"name":"Alain Wertheimer", "netWorth":33}, {"name":"Gerard Wertheimer", "netWorth":33}, {"name":"Serge Dassault & family", "netWorth":26}, {"name":"Patrick Drahi", "netWorth":21}, {"name":"Xavier Niel", "netWorth":13}, {"name":"Alain Merieux & family", "netWorth":7}, {"name":"Vincent Bollore", "netWorth":7}], "Germany":[{"name":"Beate Heister & Karl Albrecht Jr.", "netWorth":39}, {"name":"Dieter Schwarz", "netWorth":35}, {"name":"Susanne Klatten", "netWorth":29}, {"name":"Stefan Quandt", "netWorth":29}, {"name":"Heinz Hermann Thiele & family", "netWorth":22}, {"name":"Klaus-Michael Kuhne", "netWorth":20}, {"name":"August von Finck", "netWorth":20}, {"name":"Michael Otto & family", "netWorth":19}, {"name":"Hasso Plattner & family", "netWorth":15}, {"name":"Reinhold Wurth & family", "netWorth":12}], "Australia":[{"name":"Gina Rinehart", "netWorth":31}, {"name":"Andrew Forrest", "netWorth":23}, {"name":"Anthony Pratt & family", "netWorth":19}, {"name":"James Packer", "netWorth":3}, {"name":"Harry Triguboff", "netWorth":12}, {"name":"Frank Lowy", "netWorth":6}, {"name":"John Gandel", "netWorth":6}, {"name":"Stan Perron", "netWorth":2}, {"name":"Kerr Neilson", "netWorth":2}, {"name":"Lindsay Fox", "netWorth":2}], "Indonesia":[{"name":"Robert Budi Hartono", "netWorth":20}, {"name":"Michael Hartono", "netWorth":20}, {"name":"Sri Prakash Lohia", "netWorth":13}, {"name":"Eka Tjipta Widjaja & family", "netWorth":9}, {"name":"Anthoni Salim & family", "netWorth":7}, {"name":"Tahir", "netWorth":4}, {"name":"Sukanto Tanoto", "netWorth":4}, {"name":"Peter Sondakh", "netWorth":3}, {"name":"Low Tuck Kwong", "netWorth":3}, {"name":"Putera Sampoerna", "netWorth":3}], "Canada":[{"name":"David Thomson & family", "netWorth":39}, {"name":"Joseph Tsai", "netWorth":10}, {"name":"Galen Weston & family", "netWorth":10}, {"name":"James Irving", "netWorth":8}, {"name":"Jim Pattison", "netWorth":7}, {"name":"Emanuele (Lino) Saputo", "netWorth":5}, {"name":"Alain Bouchard", "netWorth":4}, {"name":"Charles Bronfman", "netWorth":2}, {"name":"Michael Lee-Chin", "netWorth":2}, {"name":"Daryl Katz", "netWorth":2}], "Finland":[{"name":"Antti Herlin & family", "netWorth":13}, {"name":"Heikki Kyostila", "netWorth":5}, {"name":"Ilkka Herlin", "netWorth":4}, {"name":"Mika Anttonen", "netWorth":3}, {"name":"Niklas Herlin", "netWorth":3}, {"name":"Ilona Herlin", "netWorth":3}, {"name":"Ilkka Paananen", "netWorth":2}, {"name":"Hannu Haukka", "netWorth":2}, {"name":"Matti Marjanen", "netWorth":2}, {"name":"Mika Ihamuotila", "netWorth":2}], "Mexico":[{"name":"Carlos Slim Helu & family", "netWorth":52}, {"name":"German Larrea Mota Velasco & family", "netWorth":27}, {"name":"Alberto Bailleres Gonzalez & family", "netWorth":10}, {"name":"Ricardo Salinas Pliego & family", "netWorth":9}, {"name":"Maria Asuncion Aramburuzabala & family", "netWorth":5}, {"name":"Juan Francisco Beckmann Vidal & family", "netWorth":5}, {"name":"Roberto Hernandez Ramirez & family", "netWorth":4}, {"name":"Carlos Hank Rhon", "netWorth":2}, {"name":"Antonio del Valle Ruiz & family", "netWorth":2}, {"name":"Carlos Peralta Quintero", "netWorth":2}]}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import ballerina/http;
import ballerina/io;

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

service / on new http:Listener(9090) {
map<Billionaire[]> topbillionaires = {};

resource function get getBillionaires(string country) returns Billionaire[]|error {
if self.topbillionaires.length() == 0 {
json billionaireDetails = check io:fileReadJson("modules/billionairehub_server/resources/billionaires.json");
self.topbillionaires = check billionaireDetails.cloneWithType();
}
Billionaire[]? billionaires = self.topbillionaires[country];
if billionaires is Billionaire[] {
return billionaires;
}
return error(country + " is not found in the sample dataset.");
}
}

This file was deleted.

12 changes: 6 additions & 6 deletions exercises/practice/http-billion-dollar-question/tests/tests.bal
Original file line number Diff line number Diff line change
Expand Up @@ -24,22 +24,22 @@ function validData() returns map<[string[], int, string[]]> {
"case2": [
["China", "India"],
5,
["Mukesh Ambani", "Gautam Adani & family", "Zhong Shanshan", "Zhang Yiming", "Ma Huateng"]
["Mukesh Ambani","Zhong Shanshan","Ma Huateng","Colin Huang","Pony Ma"]
],
"case3": [
["China", "India", "Japan", "Hong Kong"],
10,
["Mukesh Ambani", "Gautam Adani & family", "Zhong Shanshan", "Zhang Yiming", "Robin Zeng", "Ma Huateng", "Li Ka-shing", "Lee Shau Kee", "Shiv Nadar", "He Xiangjian"]
["Mukesh Ambani","Zhong Shanshan","Ma Huateng","Colin Huang","Pony Ma","Gautam Adani & family","Jack Ma","He Xiangjian","Zhang Yiming","Li Ka-shing"]
],
"case4": [
["Russia", "Italy", "France", "Germany", "Australia"],
3,
["Bernard Arnault & family", "Francoise Bettencourt Meyers & family", "Dieter Schwarz"]
["Bernard Arnault & family","Francoise Bettencourt Meyers & family","Francois Pinault & family"]
],
"case5": [
["Indonesia", "Japan", "Hong Kong"],
7,
["Robin Zeng", "Li Ka-shing", "Lee Shau Kee", "Tadashi Yanai & family", "Takemitsu Takizaki", "R. Budi Hartono", "Michael Hartono"]
["Li Ka-shing","Tadashi Yanai & family","Masayoshi Son","Takemitsu Takizaki","Nobutada Saji & family","Takemitsu Takizaki","Takemitsu Takizaki"]
],
"case6": [
["India"],
Expand All @@ -49,7 +49,7 @@ function validData() returns map<[string[], int, string[]]> {
"case7": [
["Russia"],
3,
["Vladimir Lisin", "Vladimir Potanin"]
["Vladimir Lisin","Vladimir Potanin","Alexey Mordashov & family"]
],
"case8": [
["Mexico"],
Expand All @@ -59,7 +59,7 @@ function validData() returns map<[string[], int, string[]]> {
"case9": [
["Canada", "Finland"],
2,
["Changpeng Zhao", "David Thomson & family"]
["David Thomson & family","Antti Herlin & family"]
]
};
}