Jump to content

Snakes, Mongooses and the Ultimate Election Code Chef Challenge

I tried this challenge out but keep getting an error when I submit it even tho the code runs just fine. Tried some sample inputs and some of my own inputs and I'm not getting any errors. Any reason why I'm getting an error?
You can check out the challenge here >> https://www.codechef.com/problems/SNELECT

import java.util.Scanner;

class Exp {

    public static void main(String[] args) {
        Scanner ob=new Scanner(System.in);
        int t=ob.nextInt();
        for (int i = 0; i <= t; i++) {
            String arr=new String();
            arr=ob.next();
            char char_arr[]=arr.toCharArray();
            int count=0,count_s=0,count_m=0;
            for (int j = 1; j < char_arr.length; j++) {
                if(char_arr[j-1]=='s'&&char_arr[j]=='m')
                {
                    char_arr[j-1]='*';
                    count++;
                    j++;
                }
                else if(char_arr[j-1]=='m'&&char_arr[j]=='s')
                {
                    char_arr[j]='*';
                    count++;
                }
                if(count>=2)
                    break;
            }
            for(int z=0;z<char_arr.length;z++)
            {
                if(char_arr[z]=='s')
                    count_s++;
                else if(char_arr[z]=='m')
                    count_m++;
            }
            if(count_m>count_s)
                System.out.println("mongooses");
            else if(count_m<count_s)
                System.out.println("snakes");
            else
                System.out.println("tie");
        }
    }
}

Link to comment
Share on other sites

Link to post
Share on other sites

wow, this got me seriously distracted, I was trying to do it with RegEx and Java 8 Streams and completely failed. 

 

It was driving me nuts too as to why I was failing so I wrote a test bench with JUnit and used another java answer that passed on the submissions to determine the correct answer. The strings are randomly generated with a coin flip. So I took your code and ran it through the bench and here's some results:

 

************ START OF TEST *****************
snakes = 34
mongoose = 38
TEST STRING LENGTH: `54`
ssssmmmsmsmsmssmsmmsmssmmmssmmmssssssssssmmmmsssssssss
new arr: `sss*mmm*msmsmssmsmmsmssmmmssmmmssssssssssmmmmsssssssss`
Expected: `mongooses` {snakes:`19` mongoose:`20` kills:`15`}
Result: `snakes` {snakes:`32` mongoose:`20` kills:`2`}

 

I found that if you do replace the break and don't exit out of the loop and let it process the full string it works.

if(count>=2)
  break;

//REPLACE WITH
if(count>=2)
  continue;

************ START OF TEST *****************
snakes = 34
mongoose = 38
TEST STRING LENGTH: `54`
ssssmmmsmsmsmssmsmmsmssmmmssmmmssssssssssmmmmsssssssss
new arr: `sss*mmm*m*m*m**m*mm*m**mmm**mmm*ssssssss*mmmm*ssssssss`
Expected: `mongooses` {snakes:`19` mongoose:`20` kills:`15`}
Result: `mongooses` {snakes:`19` mongoose:`20` kills:`15`}
************ END OF TEST *****************

 

I ran this 140,000 times with generated random strings of random length and it passed every time.

Link to comment
Share on other sites

Link to post
Share on other sites

14 hours ago, JStravinski said:

wow, this got me seriously distracted, I was trying to do it with RegEx and Java 8 Streams and completely failed. 

 

It was driving me nuts too as to why I was failing so I wrote a test bench with JUnit and used another java answer that passed on the submissions to determine the correct answer. The strings are randomly generated with a coin flip. So I took your code and ran it through the bench and here's some results:

 

************ START OF TEST *****************
snakes = 34
mongoose = 38
TEST STRING LENGTH: `54`
ssssmmmsmsmsmssmsmmsmssmmmssmmmssssssssssmmmmsssssssss
new arr: `sss*mmm*msmsmssmsmmsmssmmmssmmmssssssssssmmmmsssssssss`
Expected: `mongooses` {snakes:`19` mongoose:`20` kills:`15`}
Result: `snakes` {snakes:`32` mongoose:`20` kills:`2`}

 

I found that if you do replace the break and don't exit out of the loop and let it process the full string it works.


if(count>=2)
  break;

//REPLACE WITH
if(count>=2)
  continue;

************ START OF TEST *****************
snakes = 34
mongoose = 38
TEST STRING LENGTH: `54`
ssssmmmsmsmsmssmsmmsmssmmmssmmmssssssssssmmmmsssssssss
new arr: `sss*mmm*m*m*m**m*mm*m**mmm**mmm*ssssssss*mmmm*ssssssss`
Expected: `mongooses` {snakes:`19` mongoose:`20` kills:`15`}
Result: `mongooses` {snakes:`19` mongoose:`20` kills:`15`}
************ END OF TEST *****************

 

I ran this 140,000 times with generated random strings of random length and it passed every time.

Okay I get that but the challenge says that the mongoose can eat only two snakes. (See example 4 in the link)

btw I tried submitting the answer with the continue statement and it still showed that the answer is wrong.

Link to comment
Share on other sites

Link to post
Share on other sites

On 6/9/2017 at 9:48 PM, joshkia said:

Okay I get that but the challenge says that the mongoose can eat only two snakes. (See example 4 in the link)

btw I tried submitting the answer with the continue statement and it still showed that the answer is wrong.

There is no max rule actually - it's just badly worded 4th example.

You should delete the:

if(count>=2)
  break;

I also didn't notice you checking if the moose already ate a snake. It's stated that moose can eat only one snake. Here is example of your code with the check in place and the class is Main.

 

import java.util.Scanner;
class Main{
    public static void main(String[] args) {
        Scanner ob=new Scanner(System.in);
        int t=ob.nextInt();
        for (int i = 0; i <= t; i++) {
            String arr=new String();
            arr=ob.next();
            char char_arr[]=arr.toCharArray();
            int count=0,count_s=0,count_m=0;
            for (int j = 1; j < char_arr.length; j++) {
                if(char_arr[j-1]=='s'&&char_arr[j]=='m')
                {
                    char_arr[j-1]='*';
                    char_arr[j]='M';
		    count++;
                    j++;
                }
                else if(char_arr[j-1]=='m'&&char_arr[j]=='s')
                {
                    char_arr[j]='*';
                    char_arr[j-1]='M';
                    count++;
                }
            }
            for(int z=0;z<char_arr.length;z++)
            {
                if(char_arr[z]=='s')
                    count_s++;
                else if(char_arr[z]=='m'||char_arr[z]=='M')
                    count_m++;
            }
            if(count_m>count_s)
                System.out.println("mongooses");
            else if(count_m<count_s)
                System.out.println("snakes");
            else
                System.out.println("tie");
        }
    }
}

 

Link to comment
Share on other sites

Link to post
Share on other sites

  • 2 weeks later...
On 6/9/2017 at 10:13 AM, JStravinski said:

wow, this got me seriously distracted, I was trying to do it with RegEx and Java 8 Streams and completely failed. 

 

It was driving me nuts too as to why I was failing so I wrote a test bench with JUnit and used another java answer that passed on the submissions to determine the correct answer. The strings are randomly generated with a coin flip. So I took your code and ran it through the bench and here's some results:

 

************ START OF TEST *****************
snakes = 34
mongoose = 38
TEST STRING LENGTH: `54`
ssssmmmsmsmsmssmsmmsmssmmmssmmmssssssssssmmmmsssssssss
new arr: `sss*mmm*msmsmssmsmmsmssmmmssmmmssssssssssmmmmsssssssss`
Expected: `mongooses` {snakes:`19` mongoose:`20` kills:`15`}
Result: `snakes` {snakes:`32` mongoose:`20` kills:`2`}

 

I found that if you do replace the break and don't exit out of the loop and let it process the full string it works.


if(count>=2)
  break;

//REPLACE WITH
if(count>=2)
  continue;

************ START OF TEST *****************
snakes = 34
mongoose = 38
TEST STRING LENGTH: `54`
ssssmmmsmsmsmssmsmmsmssmmmssmmmssssssssssmmmmsssssssss
new arr: `sss*mmm*m*m*m**m*mm*m**mmm**mmm*ssssssss*mmmm*ssssssss`
Expected: `mongooses` {snakes:`19` mongoose:`20` kills:`15`}
Result: `mongooses` {snakes:`19` mongoose:`20` kills:`15`}
************ END OF TEST *****************

 

I ran this 140,000 times with generated random strings of random length and it passed every time.

Can you tell me how to test it with multiple randomly generate strings?

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×