Embarrassing Implementation to Basketball One-on-One

Here’s a link to the problem

Hi there, after looking at the solution…man, now it’s so obvious.
Lesson to Myself - ALWAYS THINK BEFORE TYPING

But besides all that, and I know this probably isn’t that important, where did my really embarrassing implementation for this problem go wrong? I get WA on Kattis

#include <bits/stdc++.h>

using namespace std;

// takes a bool that when true indicates that the cumulative scores of Alice and Barbara reached 10
// Now we require a new condition to win
char who_wins(int a_score, int b_score, bool activate)
{
  if (activate) {
    if (a_score > b_score+1) {
      return 'A';
    }
    if (b_score > a_score+1) {
      return 'B';
    }
  }
  else {
    if (a_score == 11) return 'A';
    if (b_score == 11) return 'B';
  }
}

int main()
{
  string inp;
  cin >> inp;

  int alice[201];
  int barbara[201];
  int game_length = 0;
  
  // goes through the string input, assigning each array the score of alice and barbara for that game_length
  // if Alice gets 2 points then a[game_length] = 2 (that is what the static_cast does) and b[game_length] = 0 and vice versa
  for (int i = 0; i < inp.size()-1; i += 2) {
    if (inp[i] == 'A') {
      alice[game_length] = static_cast<int>(inp[i+1]-'0');
      barbara[game_length] = 0;
      ++game_length;
    }
    else {
      barbara[game_length] = static_cast<int>(inp[i+1]-'0');
      alice[game_length] = 0;
      ++game_length;
    }
  }
 
  int cum_a, cum_b;
  cum_a = cum_b = 0;
  bool act = false;
  char res;
  
  // runs the score for each discrete amount of game_length through the who_wins function to get the winner
  for (int i = 0; i < game_length; ++i) {
    cum_a += alice[i];
    cum_b += barbara[i];

    if (cum_a == 10 && cum_b == 10) act = true;
    
    res = who_wins(cum_a, cum_b, act);
  }
  cout << res;
  return 0;
}

Dude, you can reduce the length to like 3-4 lines excluding header files and the main function. I’m not sure what static_cast<int>() means :man_shrugging: so I can’t help debug your code.

Here is a hint:

What happens to the input string when there is a winner?

I’ve understood the “actual” solution. I know the 3-4 liner solution.

When I read the problem, I immediately started typing the code for it without thinking through it at all. Hence the above implementation. I know it’s the stupid way to do it, but regardless, given that the question had a 200 character limit per input, I figured this solution should still work and tried it.

It didn’t work. And now I want to know why. So…help? :grimacing:

static_cast is the same as a C-style cast. Except that the compiler checks it. I’ve added a few comments to the code, please check it out and help me debug it? :slight_smile:

Bump. Somebody please help me out?

Not helpful …

Please see the debugging module. In this case, compile output is relevant. (https://ide.thecodingwizard.me/)

Holy smokes! Benq, what an honor!!

I realize my who_wins function is a partial function which is why I get that output from the compiler.

The two cases that it reaches end of the function and hence gives void output are - 1) if cum_a, cum_b reached 10 and the winning condition never arrived 2) neither of the scores reached a 11, ever

But doesn’t that not matter? I only print out res after running the who_wins function through the entire input. In that case, there’s no way that the last call to who_wins would’ve reached end without giving a non-void output, right?

Let me know if my assumptions are wrong? Thank you!

Not returning from a non-void function in C++ is undefined behavior according to this.

int func() {}
int main() { func(); }

If I run the above code locally, I get

zsh: illegal hardware instruction  ./$1 $@[2,-1]

even though the return value is not used (you may get a different result on your end).

Considering that the following code fails with a runtime error due to a failed assertion, this assumption is incorrect.

#include <bits/stdc++.h>

using namespace std;

// takes a bool that when true indicates that the cumulative scores of Alice and Barbara reached 10
// Now we require a new condition to win
char who_wins(int a_score, int b_score, bool activate)
{
  if (activate) {
    if (a_score > b_score+1) {
      return 'A';
    }
    if (b_score > a_score+1) {
      return 'B';
    }
  }
  else {
    if (a_score == 11) return 'A';
    if (b_score == 11) return 'B';
  }
  return '.';
}

int main()
{
  string inp;
  cin >> inp;

  int alice[201];
  int barbara[201];
  int game_length = 0;
  
  // goes through the string input, assigning each array the score of alice and barbara for that game_length
  // if Alice gets 2 points then a[game_length] = 2 (that is what the static_cast does) and b[game_length] = 0 and vice versa
  for (int i = 0; i < inp.size()-1; i += 2) {
    if (inp[i] == 'A') {
      alice[game_length] = static_cast<int>(inp[i+1]-'0');
      barbara[game_length] = 0;
      ++game_length;
    }
    else {
      barbara[game_length] = static_cast<int>(inp[i+1]-'0');
      alice[game_length] = 0;
      ++game_length;
    }
  }
 
  int cum_a, cum_b;
  cum_a = cum_b = 0;
  bool act = false;
  char res;
  
  // runs the score for each discrete amount of game_length through the who_wins function to get the winner
  for (int i = 0; i < game_length; ++i) {
    cum_a += alice[i];
    cum_b += barbara[i];

    if (cum_a == 10 && cum_b == 10) act = true;
    
    res = who_wins(cum_a, cum_b, act);
  }
  assert(res != '.');
  cout << res;
  return 0;
}

Considering that the following code fails with a runtime error due to a failed assertion, this assumption is incorrect.

Hey, what input did you use where assert gave a runtime error? Is this input a legitimate input? That is, an input where someone did win the basketball game?

All the inputs that fail the assertion (amongst the ones I’ve tried) are inputs that couldn’t have been given by the judge in Kattis, because until that point no clear winner has been decided in the input.

Are there any inputs where we know there is a winner and this assertion fails?

(attaching a picture for the input I tried this with)

You can submit it to Kattis. Figure it out.

Thank you. For not giving it to me.

I cannot believe what messed it up was that I didn’t replace “==” with “>=”.

:bowing_man:

Please help. I did not understand the actual solution. How do we guarantee that the last player to put a shot, wins.

Please help. I did not understand the actual solution. How do we guarantee that the last player to put a shot, wins.

They stop recording scores once there’s a winner.

But it is not mentioned that they stop reading the scores once there’s a winner. Correct?

Otherwise what would they do? Continue recording scores after there’s already a winner? That doesn’t make sense, yeah?

1 Like