Advertisement
Guest User

Untitled

a guest
Jun 28th, 2017
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.33 KB | None | 0 0
  1. This is simply a text processing problem. I'd choose Python to implement this, since it's really easy to program and has many useful built-in functions that languages like C or C++ and even Java do not have and would make harder to implement such an easy program. The description I'll give in the following lines, however, explains an idea that can be programmed in any programming language of your choice.
  2.  
  3. Such a simple program like this can be implemented using only well defined and reusable functions.
  4.  
  5. Assumptions
  6. -----------
  7.  
  8. Let's assume each line of the log contains one of the following informations in the specified format:
  9. - A game start event using the token ``GAME_START``
  10. - A game end event using the token ``GAME_END``
  11. - When a user kills another and the gun he used using ``KILL player_name_1 player_name_2 gun_name``
  12. - When a user dies by himself and the cause of the death ``DEATH player_name cause_of_death``
  13.  
  14. Example:
  15.  
  16. ```
  17. GAME_START
  18. KILL psycho_killer kirby66 KNIFE
  19. KILL psycho_killer dilma_13 GRENADE
  20. KILL megazord iron_man SHOTGUN
  21. DEATH psycho_killer POISON
  22. GAME_END
  23. GAME_START
  24. DEATH psycho_killer POISON
  25. DEATH big_big SUICIDE
  26. KILL jason kirby66 SHOTGUN
  27. KILL silvio_santos faustop HANDGUN
  28. KILL silvio_santos jason HANDGUN
  29. GAME_END
  30. ```
  31.  
  32. Use case #1
  33. -----------
  34.  
  35. We need to output a summary with the following informations for each game match: player names, total number of kills, total kills by players. To achieve this goal, we can maintain a Hash table, that we can call ``summary``, to save the player names and their corresponding kills in a game match, and a counter ``total_kills`` which is self explanatory. In this way, we have ``summary[player_name] = number_of_kills``.
  36.  
  37. From the structure of a log, assumed in the beginning of this essay, the info we need to populate this hash is between the tokens ``GAME_START`` and ``GAME_END``.
  38.  
  39. Each line between these tokens, would be splitted using ``' '`` as a separator. The token indicating the event logged by this line would be the first string from the resulting split.
  40.  
  41. If it's a ``KILL`` token, then we know that the next three strings from the resulting string are, in this order, the name of the player that killed (let's call this string ``killer``), the name of the player that died (let's call this string ``victim``), and the gun used (this info is useless to this use case, so we can ignore it). Then, we would add the keys ``killer`` and ``victim`` to the hash, if they are not present, and make ``summary[killer] += 1``. We'd also make ``total_kills += 1``. Note that we need to include the victim as a key in the hash because we need to print all player names in the summary.
  42.  
  43. If it's a ``DEATH`` token then we simply add the player's name to the hash if it's not already present.
  44.  
  45. When we found a ``GAME_END`` token then we print all the info present in the hash to the screen. We could use the following format:
  46.  
  47. ```
  48. -- GAME MATCH #1
  49. TOTAL KILLS = 3
  50.  
  51. dilma_13 (0 kills)
  52. kirby66 (0 kills)
  53. iron_man (0 kills)
  54. megazord (1 kills)
  55. psycho_killer (2 kills)
  56. ```
  57.  
  58. Then we repeat this process until the end of the file.
  59.  
  60. Use case #2
  61. -----------
  62.  
  63. Now we need to produce a ranking of all players ordered by the number of kills of each player. Again, we can maintain a Hash table, that we can call ``summary``, to save the player names and their corresponding kills in a game match, but with a small change from the hash used in Use case #1: now we also need to save the number of times that a player died. So we have: ``summary[player_name] = (number_of_kills, number_of_deaths)``.
  64.  
  65. Differently from Use case #1, we will ignore the tokens ``GAME_START`` and ``GAME_END``, since the ranking considers all game matches.
  66.  
  67. Again, each line is splitted using ``' '`` as a separator.
  68.  
  69. If it's a ``KILL`` token, then, we would add the keys ``killer`` and ``victim`` to the hash, if they are not present, and make ``summary[killer][0] += 1``. and ``summary[victim][1] += 1``.
  70.  
  71. If it's a ``DEATH`` token then we add the player's name to the hash if it's not already present and make ``summary[victim][1] += 1``.
  72.  
  73. To sort the players, we would consider to put the players with more kills and less deaths at the top of the ranking. So, player_A appears first than player_B in the ranking if player_A killed more than player_B considering all matches present in the log file. If player_A and player_B have the same number of kills, we use the number of deaths to differentiate them (the player with less deaths appears first).
  74.  
  75. When we reach the end of the log file, we could print the info using the following format:
  76.  
  77. ```
  78. -- RANK
  79.  
  80. silvio_santos (2 kills, 0 deaths)
  81. psycho_killer (2 kills, 2 deaths)
  82. megazord (1 kills, 0 deaths)
  83. jason (1 kills, 0 deaths)
  84. dilma_13 (0 kills, 0 deaths)
  85. iron_man (0 kills, 0 deaths)
  86. faustop (0 kills, 0 deaths)
  87. kirby66 (0 kills, 0 deaths)
  88. big_big (0 kills, 1 deaths)
  89. ```
  90.  
  91. Use case #3
  92. -----------
  93.  
  94. Instead of printing the ranks, I would feed the resulting hash table to a template engine and then fill a HTML table with the sorted results to present them in a friendly way to the user.
  95.  
  96. Thinking in a application that would run live on the web, the resulting hash with the sorted results could be transformed into a JSON object that would be consumed by a single page app to show these results to the users. Using this approach, a microservice could use the auxiliary functions of the command line tool to generate the JSON and then return it when requested.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement