0% found this document useful (0 votes)
38 views4 pages

Assignment 1

The document contains 4 coding questions in Java. The first question checks if a given string contains the substring "chef" and outputs the number of occurrences. The second question calculates the minimum and maximum number of days required to survive in Chocoland. The third question encodes a string by swapping adjacent characters and shifting each character by a fixed amount. The fourth question checks if a chef solved problems of all difficulty levels in a coding contest.

Uploaded by

Raashid
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
38 views4 pages

Assignment 1

The document contains 4 coding questions in Java. The first question checks if a given string contains the substring "chef" and outputs the number of occurrences. The second question calculates the minimum and maximum number of days required to survive in Chocoland. The third question encodes a string by swapping adjacent characters and shifting each character by a fixed amount. The fourth question checks if a chef solved problems of all difficulty levels in a coding contest.

Uploaded by

Raashid
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

Q1: Chef And His Characters

import java.util.*;

public class Main{


public static void main(String args[])
{
int num;
Scanner scan = new Scanner(System.in);
num= scan.nextInt();

for(int p=0;p<num;p++)
{
String str = scan.next();
int count=0;

for(int i=0;i<=str.length()-4;i++)
{
int[] flag = new int[4];
for(int j=i;j<i+4;j++)
{
if(str.charAt(j) == 'c'){ flag[0]=1; }
else if(str.charAt(j) == 'h') { flag[1]=1; }
else if(str.charAt(j) == 'e') { flag[2]=1; }
else if(str.charAt(j) == 'f') { flag[3]=1; }
else{ break; }
}

if(flag[0]==1 && flag[1]==1 && flag[2]==1 && flag[3]==1)


{ count++; }

if(count!=0)
{ System.out.println("lovely " + count);}
else
{ System.out.println("normal"); }
}
}
}
Q2: SURVIVE IN CHOCOLAND
import java.lang.Math;
import java.util.Scanner;

public class Main {

public static void main(String []args){


Scanner scn = new Scanner(System.in);
int t = scn.nextInt();
for (int i = 0; i < t; i++) {
double chocs = (double) scn.nextInt();
double hunger = (double) scn.nextInt();
double surv = (double) scn.nextInt();
int minDays = (int)Math.ceil((hunger*surv)/chocs);
int maxDays = (int)surv - ((int)(surv/7));
System.out.println(minDays <= maxDays ? minDays : -1);
}
}
}
Q3: ENCODING MESSAGE
import java.util.*;
import java.lang.*;
import java.io.*;

class Codechef
{
public static void main (String[] args) throws java.lang.Exception
{
char c;
Scanner sc= new Scanner(System.in);
int n=sc.nextInt();
while(n-->0)
{
int l= sc.nextInt();
String s= sc.next();
char ar[]=new char[l];
for(int i=0;i<l;i++)
{
ar[i]=s.charAt(i);
//System.out.println(ar[i]);
}
if(l%2==0)
{
for(int i=0;i<l;i+=2)
{
char c1=ar[i];
ar[i]=ar[i+1];
ar[i+1]=c1;
}}
else
{
for(int i=0;i<l-1;i+=2)
{
char c1=ar[i];
ar[i]=ar[i+1];
ar[i+1]=c1;
} }

for(int i=0;i<l;i++)
{
int a=ar[i];
int sum=a+(25-(2*(a-97)));
ar[i]=(char)sum;
}
String string="";
for(int i=0;i<l;i++)
{
string=string+ar[i];
}
System.out.println(string);
} } }
Q4: CHEF AND COOK-OFF CONTESTS
import java.io.*;
import java.util.*;
class GFG {
public static void main (String[] args) {
Scanner sc=new Scanner(System.in);
int t= sc.nextInt();

while(t-->0)
{
int n= sc.nextInt();
sc.nextLine();
int problem[]=new int[5];
while(n-->0)
{
String str= sc.nextLine();
if(str.equals("cakewalk"))
problem[0]=1;
else if(str.equals("simple"))
problem[1]=1;
else if(str.equals("easy"))
problem[2]=1;
else if((str.equals("easy-medium"))||(str.equals("medium")))
problem[3]=1;
else if(str.equals("medium-hard")||(str.equals("hard")))
problem[4]=1;
}
int count=0;
for(int i=0;i<5;i++)
{
if(problem[i]==1){
count++;
}
}
if(count==5)
System.out.println("Yes");
else
System.out.println("No");
}
}
}

You might also like