DEV Community

dev.to staff
dev.to staff

Posted on

Daily Challenge #70 - Pole Vault Competition Results

Your challenge is to determine the top 3 place finalists in a pole vault competition involving several competitors. This can be very confusing for those who do not know the rules.

As input, you'll receive an array of objects. Each object contains the respective competitor's name (as a string) and their results at the various bar heights(as an array of strings):

[{name: "Sergey", results: ["", "O", "XO", "O", "XXO", "XXX", "", ""]}
{name: "Jan", results: ["", "", "", "O", "O", "XO", "XXO", "XXX"]}
{name: "Bruce", results: ["", "XO", "XXO", "XXX", "", "", "", ""]}
{name: "HerrWert", results: ["XXX", "", "", "", "", "", "", ""]}]

An empty string indicates that the vaulter did not jump at this height for a variety of possible reasons ("passed" at this height, was temporarily called away to a different track and field event, was already eliminated from competition, or withdrew due to injury, for example).

An upper-case X in the string represents an unsuccessful attempt at the height. (As you may know, the vaulter is eliminated from the competition after three consecutive failed attempts.)

An upper-case O represents a successful attempt. If present at all, this will be the final character in the string, because it indicates that the vaulter has now successfully completed this height and is ready to move on.


The first step should be to determine who cleared the greatest height successfully. Who has an "O" mark at a higher array element than any other competitor? In the most straightforward case, you can determine the finalists by this logic.

But what if there's a tie for one of these finishing places? Proceed as follows:

First trace backwards to find the greatest height that both vaulters cleared successfully. Then determine who had the fewest unsuccessful attempts at this height (i.e., the fewest X's in the string for this height). This person wins the tie-break.

But what if they're still tied with one another? Do NOT continue to trace backwards through the heights! Instead, compare their total numbers of unsuccessful attempts at all heights in the competition. The vaulter with the fewest total misses wins the tie-break.

But what if they're still tied? It depends on the finishing place:

  • If it's for second or third place, the tie stands (i.e., is not broken).
  • But if it's for first place, there must be a jump-off (like overtime or penalty kicks in other sports) to break the tie and determine the winner. (This jump-off occurs - hypothetically - after your code runs and is thus not a part of this challenge.)

Return a single object as your result. Each place-finish that is included in the results should have its value as the respective vaulter's name. In the event of a tie, the value of the property is the names of all tied vaulters in alphabetical order, comma-separated, followed by the notation (jumpoff) for first place ties and (tie) for second or third place.

Here are some possible outcomes:
{1st: "Jan", 2nd: "Sergey"; 3rd: "Bruce"} (These results correspond to the sample input data given above.)
{1st: "Julia", 2nd: "Madi, Emily (tie)}
{1st: "Caleb, Dustin (jump-off)", 3rd: "Sam"}
{1st: "Meredith", 2nd: "Maddy", 3rd: "Cierra, Sara (tie)"}
{1st: "Alex, Basti, Max (jump-off)"}

And here are the test arrays:
{"name": "Linda", "results": ["XXO", "O","XXO", "O"]},
{"name": "Vickie", "results": ["O","X", "", ""]},
{"name": "Debbie", "results": ["XXO", "O","XO", "XXX"]},
{"name": "Michelle", "results": ["XO","XO","XXX",""]},
{"name": "Carol", "results": ["XXX", "","",""]}

Good luck!


This challenge comes from CodeWars user HerrWert. Thank you to CodeWars, who has licensed redistribution of this challenge under the 2-Clause BSD License!

Want to propose a challenge idea for a future post? Email yo+challenge@dev.to with your suggestions!

Top comments (2)

Collapse
 
peledzohar profile image
Zohar Peled

The rules are a bit convoluted but it's not much a brain teaser - just a lot of work... The real challenge on this particular one is to code all these rules in a readable way, I guess.