Advertisement
Guest User

Untitled

a guest
Dec 29th, 2019
281
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.32 KB | None | 0 0
  1. <?php
  2.  
  3. $command = readline();
  4.  
  5. $followersComments = [];
  6. $followersLikes = [];
  7.  
  8. while($command != "Log out")
  9. {
  10. $cmds = explode(': ', $command);
  11. $mainCmd = $cmds[0];
  12. $username = $cmds[1];
  13.  
  14. switch ($mainCmd)
  15. {
  16. case "New follower":
  17. if(!key_exists($username, $followersComments))
  18. {
  19. $followersComments[$username] = 0;
  20. $followersLikes[$username] = 0;
  21. }
  22. break;
  23. case "Like":
  24. $count = $cmds[2];
  25. if(!key_exists($username, $followersLikes))
  26. {
  27. $followersLikes[$username] = $count;
  28. }
  29. else
  30. {
  31. $followersLikes[$username] += $count;
  32. }
  33. break;
  34. case "Comment":
  35. if(!key_exists($username, $followersComments))
  36. {
  37. $followersComments[$username] = 1;
  38. }
  39. else
  40. {
  41. $followersComments[$username] += 1;
  42. }
  43. break;
  44. case "Blocked":
  45. if(!key_exists($username, $followersComments) && !key_exists($username, $followersLikes))
  46. {
  47. echo "$username doesn't exist." . PHP_EOL;
  48. }
  49. else
  50. {
  51. if(key_exists($username, $followersComments))
  52. {
  53. unset($followersComments[$username]);
  54. }
  55.  
  56. if(key_exists($username, $followersLikes))
  57. {
  58. unset($followersLikes[$username]);
  59. }
  60. }
  61. break;
  62. }
  63.  
  64. $command = readline();
  65. }
  66.  
  67. $totalFollowers = [];
  68. $follCommentsAndLikes = [];
  69. foreach ($followersComments as $follower => $value)
  70. {
  71. if(!in_array($follower, $totalFollowers))
  72. {
  73. $totalFollowers[] = $follower;
  74. }
  75.  
  76. $follCommentsAndLikes[$follower]['C'] = $value;
  77. }
  78.  
  79. foreach ($followersLikes as $follower => $value)
  80. {
  81. if(!in_array($follower, $totalFollowers))
  82. {
  83. $totalFollowers[] = $follower;
  84. }
  85.  
  86. $follCommentsAndLikes[$follower]['L'] = $value;
  87.  
  88. }
  89.  
  90.  
  91.  
  92. echo count($totalFollowers) . " followers" . PHP_EOL;
  93. $follCommentsAndLikes = array_filter($follCommentsAndLikes);
  94. print_r($follCommentsAndLikes);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement