0

Problem Statement     

|-O-|    |-O-|    |-O-|    |-O-|    |-O-|

Above you see a tiny fraction of our glorious imperial fleet, the Twin Ion Engine starfighters (known as "TIE fighters" for short). Each TIE fighter consists of two solar array wings ('|'), two wing braces ('-') and one central cabin ('O').

Even though our TIE fighters are clearly superior to the X-wing fighters flown by those pesky rebels, occasionally some TIE fighter will receive some accidental combat damage. Whenever a damaged TIE fighter is recovered, we disasemble it and salvage the parts that are still in working condition. It is now up to you to use these salvaged parts to assemble as many complete TIE fighters as possible.

You are given the string salvagedParts. Each character of salvagedParts represents one piece of machinery that has been salvaged. As mentioned above, the characters '|', '-', and 'O' (uppercase oh) represent the parts you need to build a TIE fighter. There may also be other characters in salvagedParts. These represent machinery that isn't used in a TIE fighter.

  Class: TIEFighterAssembly

Method: assemble

Parameters: string

Returns: int

Method signature: int assemble(string salvagedParts)

(be sure your method is public)

Constraints:

salvagedParts will contain between 1 and 50 characters, inclusive.

Each character of salvagedParts will be one of "|-O=+()*" (quotes for clarity).

Examples

"|-O-|" Returns: 1 One fully functional TIE fighter.

My code to this is

class TIEFighterAssembly
{
    public:
    int assemble(string salvagedParts)
    {
        int a=0,b=0,c=0;
        for(int i=0;i<salvagedParts.length();i++)
        {
            if(salvagedParts[i]=='-')a++;
            else if(salvagedParts[i]=='|')b++;
            else if(salvagedParts[i]=='O')c++;
        }
        int sum=0;
        while(c)
        {
            if(c>0&&a>=2&&b>=2)
            {
                sum++;
                c--;
                a-=2;
                b-=2;
            }
            else c=0;
        }
        return sum;
    }
};

I got this error


Your code did not compile:

errors compiling:

In file included from top level:3:0:
TIEFighterAssembly.cc:4:15: error: 'string' has not been declared
  int assemble(string salvagedParts)
               ^
TIEFighterAssembly.cc: In member function 'int TIEFighterAssembly::assemble(int)':
TIEFighterAssembly.cc:7:31: error: request for member 'length' in 'salvagedParts', which is of non-class type 'int'
   for(int i=0;i<salvagedParts.length();i++)
                               ^
TIEFighterAssembly.cc:9:22: error: invalid types 'int[int]' for array subscript
    if(salvagedParts[i]=='-')a++;
                      ^
TIEFighterAssembly.cc:10:27: error: invalid types 'int[int]' for array subscript
    else if(salvagedParts[i]=='|')b++;
                           ^
TIEFighterAssembly.cc:11:27: error: invalid types 'int[int]' for array subscript
    else if(salvagedParts[i]=='O')c++;
                           ^
In file included from top level:10:0:
Your class or method was improperly declared: In function 'int _wrapper::thunk(std::string)':
Your class or method was improperly declared:20034:3: error: no matching function for call to 'TIEFighterAssembly::assemble(std::string&)'
Your class or method was improperly declared:20034:3: note: candidate is:
In file included from top level:3:0:
TIEFighterAssembly.cc:4:6: note: int TIEFighterAssembly::assemble(int)
  int assemble(string salvagedParts)
      ^
TIEFighterAssembly.cc:4:6: note:   no known conversion for argument 1 from 'std::string {aka std::basic_string<char>}' to 'int'
In file included from top level:10:0:
Your class or method was improperly declared:20037:1: warning: control reaches end of non-void function [-Wreturn-type]

Can anyone help me how to submit a problem in TopCoder

Iroh
  • 5
  • 2
  • 5
  • See all the stuff if your source file you didn't include here, specifically *above* the code you chose to post? The `#include` list? The [highly-discouraged `using namespace std;`](https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice) ? Pretty big assumption "top-coder" is going to do all of that for you. I agree with Evg; the only thing to come out of the "top-coder" is people that are semi-literate at using "top-coder". Get a good book. – WhozCraig Feb 15 '21 at 11:00

1 Answers1

0

Your method is correct, your code however can be modified to work without errors:

#include <string>

class TIEFighterAssembly
{
    public:
    static int assemble(std::string salvagedParts)
    {
        int a=0,b=0,c=0;
        for(size_t i=0;i<salvagedParts.length();i++)
        {
            char character = salvagedParts[i];
            if(character=='-')a++;
            else if(character=='|')b++;
            else if(character=='O')c++;
        }
        int sum=0;
        while(c)
        {
            if(a>=2&&b>=2)
            {
                sum++;
                c--;
                a-=2;
                b-=2;
            }
            else c=0;
        }
        return sum;
    }
};

As others mentioned you used string instead of std::string. Also your code will have less overhead if you store the character first and then compare it.

Your method also can become simpler if you simply return the lesser variable from a,b and c:

#include <string>

class TIEFighterAssembly
{
    public:
    static int assemble(std::string salvagedParts)
    {
        int a=0,b=0,c=0;
        for(size_t i=0;i<salvagedParts.length();i++)
        {
            char character = salvagedParts[i];
            if(character=='-')a++;
            else if(character=='|')b++;
            else if(character=='O')c++;
        }
        b/=2;
        a/=2;
        return(a < b && a < c ? a : (b < c ? b : c));
    }
};
newbie
  • 415
  • 3
  • 11