Add get_longest_chain function and comments.

This commit is contained in:
Ceda EI 2018-04-03 20:02:23 +05:30
parent d8b8693a43
commit fedd816e73
1 changed files with 27 additions and 0 deletions

View File

@ -7,6 +7,7 @@ if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
# Sends the given text wrapped in triple backticks as Markdown.
function send_code($post_message) {
global $bot_api;
global $chat_id;
@ -23,6 +24,8 @@ function send_code($post_message) {
$result = file_get_contents($url, false, $context);
}
# Takes a user array as input. Checks who follows him/her and adds
# him/her to $output array. Exits when a user is followed by no one.
function get_chain_from_user($user) {
global $conn;
$output = array($user);
@ -43,5 +46,29 @@ function get_chain_from_user($user) {
return $output;
}
# First generates a list of people not following anyone (end_points)
# Then runs get_chain_from_user on all of them and stores them in
# $chains array. Then compares the arrays inside $chains array and
# returns the longest one.
function get_longest_chain() {
global $conn;
$query = "SELECT user_id, username FROM users WHERE follows = -1;";
$end_points = $conn->query($query);
if ($end_points->num_rows == 0) {
return array();
}
$chains = array();
while ($end_point = $end_points->fetch_assoc()){
$chain = get_chain_from_user($end_points);
array_push($chains, $chain);
}
$longest_chain_index = 0;
for ($i = 0; $i < count($chains); $i++){
if (count($chains[$i]) > count($chains[$longest_chain_index])) {
$longest_chain_index = $i;
}
}
return $chains[$longest_chain_index];
}
$conn->close();
?>