Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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.
- Such a simple program like this can be implemented using only well defined and reusable functions.
- Assumptions
- -----------
- Let's assume each line of the log contains one of the following informations in the specified format:
- - A game start event using the token ``GAME_START``
- - A game end event using the token ``GAME_END``
- - When a user kills another and the gun he used using ``KILL player_name_1 player_name_2 gun_name``
- - When a user dies by himself and the cause of the death ``DEATH player_name cause_of_death``
- Example:
- ```
- GAME_START
- KILL psycho_killer kirby66 KNIFE
- KILL psycho_killer dilma_13 GRENADE
- KILL megazord iron_man SHOTGUN
- DEATH psycho_killer POISON
- GAME_END
- GAME_START
- DEATH psycho_killer POISON
- DEATH big_big SUICIDE
- KILL jason kirby66 SHOTGUN
- KILL silvio_santos faustop HANDGUN
- KILL silvio_santos jason HANDGUN
- GAME_END
- ```
- Use case #1
- -----------
- 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``.
- 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``.
- 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.
- 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.
- If it's a ``DEATH`` token then we simply add the player's name to the hash if it's not already present.
- 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:
- ```
- -- GAME MATCH #1
- TOTAL KILLS = 3
- dilma_13 (0 kills)
- kirby66 (0 kills)
- iron_man (0 kills)
- megazord (1 kills)
- psycho_killer (2 kills)
- ```
- Then we repeat this process until the end of the file.
- Use case #2
- -----------
- 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)``.
- Differently from Use case #1, we will ignore the tokens ``GAME_START`` and ``GAME_END``, since the ranking considers all game matches.
- Again, each line is splitted using ``' '`` as a separator.
- 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``.
- 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``.
- 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).
- When we reach the end of the log file, we could print the info using the following format:
- ```
- -- RANK
- silvio_santos (2 kills, 0 deaths)
- psycho_killer (2 kills, 2 deaths)
- megazord (1 kills, 0 deaths)
- jason (1 kills, 0 deaths)
- dilma_13 (0 kills, 0 deaths)
- iron_man (0 kills, 0 deaths)
- faustop (0 kills, 0 deaths)
- kirby66 (0 kills, 0 deaths)
- big_big (0 kills, 1 deaths)
- ```
- Use case #3
- -----------
- 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.
- 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