[Solved] Casper at the Carnival with Python, C++, Java, C

Casper at the Carnival: The Circoloco Children Carnival is the City’s largest and successful event dedicated to children and families. Casper is a smart little boy who loves eating cookies and drinking fresh juices. He visits the carnival with his parents and is going to spend N minutes at the event ground. Each minute he either eats a cookie or drinks fresh juice. Cookies are very sweet and thus Casper’s parents have instructed him to drink fresh juice in the next minute, after eating a cookie.

Casper at the Carnival with Python, C++, Java, C

You are given whether he ate a cookie or drank fresh juice in each of the N minutes. Your task is to check if Casper followed his parents’ instructions. That is, you need to verify whether after each eaten cookie he drinks fresh juice in the next minute.

Input Format:
The first line of the input contains an integer N denoting the number of minutes.
The second line of the input contains N space-separated strings S1S2, …,SN. The string Si is either “cookie” (if Casper eats a cookie in the i-th minute) or “juice” (otherwise).

Output Format:
Output a single line containing the answer — “Yes“(without quotes) if Casper followed his parents’ instructions, and “No“(without quotes) otherwise, both without the quotes.
Refer sample input and output for formatting specifications.

Sample Input1:
7
cookie juice juice cookie juice cookie juice
Sample Output1:
Yes

Sample Input 2:
5
cookie cookie juice juice juice
Sample Output 2:
No

Solution

n=int(input())
s=list(input().split())
#sauravhathi
if s[-1]=="cookie" or (n==1 and s[0]=="cookie"):
    print("NO")
else:
    for i in range(0,n-1):
        #sauravhathi
        if s[i]=="cookie" and s[i+1]=="cookie":
            print("No")
            break
    else:
        print("Yes")
#include <iostream>
using namespace std;

int main() {
    //sauravhathi
	    int n;cin>>n;
	    string a[n];
	    for(int i=0;i<n;i++)  cin>>a[i];
	    int c = 0;
	    for(int i=0;i<n;i++){
	        if(a[i] == "cookie"  and a[i+1] != "juice"){
	              c++;
	              //sauravhathi
	              break;
	        }
	    }
	    if(c){
	        cout<<"No"<<endl;
	    }
	    else{
	        cout<<"Yes"<<endl;
	    }

	return 0;
	//sauravhathi
}
#include <stdio.h>
#include<string.h>
int main(void) {
    int N,i;
        scanf("%d",&N);
        char S[100][50];
        for(i=0;i<N;i++)
        //sauravhathi
         scanf("%s",S[i]);
        for(i=0;i<N-1;i++)
        {
            if((strcmp(S[i],"cookie")==0)&&(strcmp(S[i+1],"juice")!=0))
             break;
        }
        if((strcmp(S[N-1],"cookie")==0))
        //sauravhathi
         i=-1;
        if(i==N-1)
            printf("Yes\n");
        else
         printf("No\n");
    
	return 0;//sauravhathi
}

import java.util.*;
import java.lang.*;
import java.io.*;
class Main
{
	public static void main (String[] args)
	{
		Scanner sc=new Scanner(System.in);
		    int n=sc.nextInt();
		    boolean f=true;
		    String ar[]=new String[n];
		    for(int x=0;x<n;x++)
		    {
		        ar[x]=sc.next();
		    }
		    if(ar[n-1].equals("cookie"))
		    {
		        f=false;
		    }
		    else
		    {
    		    for(int x=0;x<n-1;x++)
    		    {
    		        if(ar[x].equals("cookie") && (!(ar[x+1].equals("juice"))))
    		        {
    		            f=false;
    		            break;
    		        }
    		    }
		    }
    		
		    if(f)
		    {
		        System.out.println("Yes");
		    }
		    else
		    {
		        System.out.println("No");
		    }
	}
}

Happy Learning – If you require any further information, feel free to contact me.

Share your love
Saurav Hathi

Saurav Hathi

I'm currently studying Bachelor of Computer Science at Lovely Professional University in Punjab.

📌 Nodejs and Android 😎
📌 Java

Articles: 444

Leave a Reply

Your email address will not be published. Required fields are marked *