Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

5 6181746519327113983

Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 16

******************Assignment1**********************

/*Program 1 */
import java.util.*;
public class busbus{
public static void main(String args[]){
Scanner s=new Scanner(System.in);
int n=s.nextInt();
for(int i=1;i<=n;i++)
{
if(i%7==0&&i%10==7){
System.out.println("BusBus");
}
else if(i%7==0||i%10==7){
System.out.println("Bus");
}
else {
System.out.println(i);
}
}
}
}

/*Program 2 */
import java.util.*;
public class crazy{
public static void main(String args[]){
Scanner s = new Scanner(System.in);
int n=s.nextInt();
long a,du,c=0,sum=0,y;
for(int i=0;i<n;i++)
{
a=s.nextLong();
du=a;
while(a!=0)
{
a=a/10;
c++;
}
a=du;
for(int j=0;j<c;j++)
{
y=a%10;
sum+=Math.pow(y,c);
a=a/10;
}
if(sum==du)
System.out.println(sum +" is a crazy number");
else
System.out.println(du +" is not a crazy number");
sum=0;
c=0;
}
}
}

******************Assignment1**********************
******************Test2**********************

/*Program 1 */
import java.util.*;
public class PhoneKeyPad {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String str = sc.nextLine();
String two = "ABC", three = "DEF", four = "GHI", five = "JKL",
six = "MNO", seven = "PQRS", eight = "TUV", nine = "WXYZ";
String output = "";
int len = str.length();
for(int i=0;i<len;i++){
String ch = str.charAt(i) + "";

if(two.contains(ch)) output +="2";


else if(three.contains(ch)) output +="3";
else if(four.contains(ch)) output +="4";
else if(five.contains(ch)) output +="5";
else if(six.contains(ch)) output +="6";
else if(seven.contains(ch)) output +="7";
else if(eight.contains(ch)) output +="8";
else if(nine.contains(ch)) output +="9";
}
System.out.println(output);
}
}

/*Program 2*/
import java.util.*;
public class Voters {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int totalCandidates = sc.nextInt();
int[] arr = new int[totalCandidates];
while(true){
int vote = sc.nextInt();
if(vote==0) break;
arr[vote-1]++;
}
int candidate = 0, maxVote = 0;
for(int i=0;i<totalCandidates;i++){
if(arr[i]>maxVote){
maxVote = arr[i];
candidate = i;
}
}
System.out.println(candidate+1);
System.out.println(maxVote);
}
}

******************Test2**********************

******************Test3**********************

/*Program1*/
/*Split Array*/
import java.util.Scanner;
class Split{
static int SplitPoint(int arr[],int n){
int lhs=0;
for(int i=0;i<n;i++)
lhs+=arr[i];
int rhs=0;
for(int i=n-1;i>=0;i--){
rhs+=arr[i];
lhs-=arr[i];
if(rhs==lhs)
return lhs;
}
return -1;
}
static void print(int arr[],int n){
int sp=SplitPoint(arr,n);
if(sp==-1 || sp==n){
System.out.println("-1");
return;
}
else{
System.out.println(sp);
}
}
public static void main(String[] args){
Scanner sc=new Scanner(System.in);
int t=sc.nextInt();
for(int i=0;i<t;i++){
int n=sc.nextInt();
int[] arr=new int[100];
for(int j=0;j<n;j++){
arr[j]=sc.nextInt();
}
print(arr,n);
}
}
}

/*Program2*/

import java.util.*;
public class Unique{
static boolean Anagram(String s1,String s2){
boolean n=false;
if(s1.length()!=s2.length())
n=false;
else{
char[] c1=s1.toLowerCase().toCharArray();
char[] c2=s2.toLowerCase().toCharArray();
Arrays.sort(c1);
Arrays.sort(c2);
n=Arrays.equals(c1,c2);
}
return n;
}
public static void main(String []args){
Scanner ob=new Scanner(System.in);
int count=0,i,j;
String s=ob.nextLine();
s=s.substring(0,s.length()-1);
String arr[]=s.split(" ");

for(i=0;i<arr.length;i++){
for(j=i+1;j<arr.length;j++){
if(Anagram(arr[i],arr[j]))
{
arr[j]="";

}
}
if(arr[i]!="")
count++;
}
System.out.println(count);
}
}

******************Test3**********************

******************Assignment2**********************

/*Program1*/
/*Split Array*/

import java.util.Scanner;
class Split{
static int SplitPoint(int arr[],int n){
int lhs=0;
for(int i=0;i<n;i++)
lhs+=arr[i];
int rhs=0;
for(int i=n-1;i>=0;i--){
rhs+=arr[i];
lhs-=arr[i];
if(rhs==lhs)
return lhs;
}
return -1;
}
static void print(int arr[],int n){
int sp=SplitPoint(arr,n);
if(sp==-1 || sp==n){
System.out.println("-1");
return;
}
else{
System.out.println(sp);
}
}
public static void main(String[] args){
Scanner sc=new Scanner(System.in);
int t=sc.nextInt();
for(int i=0;i<t;i++){
int n=sc.nextInt();
int[] arr=new int[100];
for(int j=0;j<n;j++){
arr[j]=sc.nextInt();
}
print(arr,n);
}
}
}

/*Program2*/
/*OpenIgnite Calling- 40 min*/

import java.util.Scanner;
public class OpenIgnite{
static int Pos(int n, int m, int k){
if(m<=n-k+1)
return m+k-1;
m-=n-k+1;
return (m%n==0) ? n : (m%n);
}
public static void main(String[] args){
Scanner in=new Scanner(System.in);
int x=in.nextInt();
for(int i=0;i<x;i++){
int nt=in.nextInt();
int sc=in.nextInt();
int fs=in.nextInt();
System.out.println(Pos(nt,sc,fs));
}
}
}

/*Program3*/
/*Unique words*/

import java.util.*;
public class Unique{
static boolean Anagram(String s1,String s2){
boolean n=false;
if(s1.length()!=s2.length())
n=false;
else{
char[] c1=s1.toLowerCase().toCharArray();
char[] c2=s2.toLowerCase().toCharArray();
Arrays.sort(c1);
Arrays.sort(c2);
n=Arrays.equals(c1,c2);
}
return n;
}
public static void main(String []args){
Scanner ob=new Scanner(System.in);
int count=0,i,j;
String s=ob.nextLine();
s=s.substring(0,s.length()-1);
String arr[]=s.split(" ");

for(i=0;i<arr.length;i++){
for(j=i+1;j<arr.length;j++){
if(Anagram(arr[i],arr[j]))
{
arr[j]="";

}
}
if(arr[i]!="")
count++;
}
System.out.println(count);
}
}

******************Assignment2**********************

******************Test4**********************

/*Program1*/
/*Longest Voweless Substring*/

import java.util.*;
public class test{
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String temp="";
String s1= sc.nextLine();
StringTokenizer st=new StringTokenizer(s1, "aeiouAEIOU");
while(st.hasMoreTokens()){
String s2= st.nextToken();
if(s2.length()>temp.length());
temp=s2;
}
System.out.println(arr[ind]);
}
}

/*Program2*/
/*Saddle Point */

import java.util.*;
class Test{
static boolean findSaddle(int a[][], int n){
for(int i=0;i<n;i++){
int min = a[i][0], col_ind = 0;
for(int j=0;j<n;j++){
if(min > a[i][j]){
min = a[i][j];
col_ind = j;
}
}
int k;
for(k=0;k<n;k++)
if(min < a[k][col_ind])
break;
if(k == n){
System.out.println(min);
return true;
}
}
return false;
}
public static void main(String[] args){
int m,n;
Scanner sc = new Scanner(System.in);
m=sc.nextInt();
n=sc.nextInt();
int a[][]=new int [m][n];
for(int i=0;i<m;i++){
for(int j=0;j<n;j++){
a[i][j]=sc.nextInt();
}
}
if(findSaddle(a,m)==false)
System.out.println("-1");
}
}

******************Test4**********************

******************Assignment3**********************

/*Program1*/
/*Switch Array*/

import java.util.*;
public class SwitchArrMain{
public static boolean isPrime(int n){
for(int i=2;i<=n/2;i++){
if(n%i==0){
return false;
}
}
return true;
}
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int count = 0;
int[] arr = new int[n];
for(int i=0;i<n;i++){
arr[i] = sc.nextInt();
}
for(int j=2;j<n;j++){
if(isPrime(j)==true){
if(arr[j]%j==0){
arr[j]=j;
count++;
}
}
}
System.out.println(count);
for(int k=0;k<n;k++){
System.out.print(arr[k]+" ");
}
}
}

/*Program2*/
/*SPiglatin encoding*/

import java.util.Scanner;
import java.lang.String;
public class LatinWord{
static boolean isVowel(char ch){
return (ch == 'A' || ch == 'a' || ch == 'E' || ch == 'e' || ch == 'I' || ch
=='i' || ch == 'O' || ch == 'o' || ch == 'U' || ch == 'u');
}
static String pigLatinWord(String string){
int stringlength = string.length();
int index = -1;
for(int i=0;i<stringlength;i++){
if(isVowel(string.charAt(i))){
index = i;
break;
}
}
if(index == -1)
return string+" ";
return (string.substring(index)+string.substring(0, index)+"ay ");
}
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
String str = sc.nextLine();
String [] str1 = str.split(" ");
for(String word : str1){
System.out.print(pigLatinWord(word));
}
}
}

/*Program3*/
/*Roman number conversion*/

import java.util.Scanner;
public class Main{
private static String romanLitrals ="MDCLXVI";
private static int[] values = {1000, 500, 100, 50, 10, 5, 1};
private static int romanToDecimal(String romanLitral){
int len = romanLitral.length();
int result= 0, prev=0, count=1;
for(int i=len; i>0; i--){
String ch = romanLitral.substring(i-1,i);
int index = romanLitrals.indexOf(ch);
if(index == -1) return -1;
int value = values[index];
if(value<prev) result-= value;
else result += value;
if(prev==value) {
if(prev == 5 || prev == 50 || prev == 500)
return -1;
count++;
}
else count=1;
if(count>3) return -1;
prev = value;
}
return result;
}
public static void main(String[]a){
Scanner sc = new Scanner(System.in);
String romanLitral = sc.nextLine();
int decimalNumber = romanToDecimal(romanLitral.toUpperCase());
if(decimalNumber == -1)
System.out.println(romanLitral+" is an individual input");
else
System.out.println(decimalNumber);
}
}

******************Assignment3**********************

******************Test5**********************

/*Program1*/
/*Make it Anagram*/

import java.util.*;
class Anagram
{
public static void main(String arg[])
{
Scanner sc=new Scanner(System.in);
String str1=sc.next();
String str2=sc.next();
String str3=new String();
String str4=new String();
str3=str1;
str4=str2;
int count=0;
for(int i=0;i<str1.length();i++)
{
if(str2.contains(str1.charAt(i)+""))
{
continue;
}
else
{
System.out.print(str1.charAt(i));
count++;
if(i!=str3.length())
str3=str3.substring(0,i)+str3.substring(i+1,str3.length());
else
str3=str3.substring(0,i-1);
}
}
System.out.println("");
for(int i=0;i<str2.length();i++)
{
if(str3.contains(str2.charAt(i)+""))
{
continue;
}
else
{
System.out.print(str2.charAt(i));
count++;
if(i!=str4.length())
str4=str4.substring(0,i)+str4.substring(i+1,str4.length());
else
str4=str4.substring(0,i-1);
}
}
System.out.println("");
System.out.println(count);
}

/*Program2*/
/*Longest sorted Sequence*/

import java.util.*;

public class LSS{


public static void main(String[] args){
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
int[] arr=new int[n];
if(n>20)
{
System.out.print("Array size of n is Invalid");
return;
}
for(int i=0;i<n;i++)
arr[i]=sc.nextInt();
int c=0,start=0,last=0;
for(int i=0;i<n-1;i++)
{
if(arr[i]<arr[i+1])
{
c++;
for(int j=i;j<(n-1);j++)
{
if(arr[j]>arr[j+1])
break;
c++;
}
if(last<c)
{
last=c;
start=i;
}
}
c=0;
}
System.out.print(last+" ");
for(int i=start;i<start+last;i++)
System.out.print(arr[i]+" ");
}
}

******************Test5**********************
******************Assignment4**********************

/*Program1*/
/*Make it Anagram*/

import java.util.*;
public class Main{
public static void main(String[]a){
Scanner sc= new Scanner(System.in);
String s1=sc.next();
String s2=sc.next();
if(s1.length()<1 && s2.length()<1) return;
for(int i=0; i<s1.length();i++)
for(int j=0; j<s2.length(); j++)
if(s1.charAt(i) == s2.charAt(j)){
s1 = s1.substring(0,i)+s1.substring(i+1);
s2 = s2.substring(0,j)+s2.substring(j+1);
i--;
j--;
break;
}
System.out.println(s1);
System.out.println(s2);
System.out.println(s1.length()+s2.length());
}
}

/*Program2*/
/*Longest sorted Sequence*/

import java.util.Scanner;

public class Main{


public static void main(String[]a){
Scanner sc = new Scanner(System.in);
int len = sc.nextInt();
if(len>20)
System.out.print("Array size of "+len+" is Invalid");
else{
int[] arr = new int[len];
for(int i=0; i<len; i++)
arr[i] = sc.nextInt();
int start=0, longest=0, temp =1;
for(int i=1; i<len; i++){
int curr = arr[i], prev = arr[i-1];
if(prev<=curr) temp++;
else{
if(longest<temp){
longest = temp;
start = i-temp;
}
temp=1;
}
}
if(temp>longest) longest = temp;
System.out.print(longest);
for(int i=start; i<longest+start; i++)
System.out.print(" "+arr[i]);
}
}
}

******************Assignment4**********************

******************Test6**********************

/*Program1*/
/*Time Interpreter*/

import java.util.Scanner;
public class Main{
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
int test = sc.nextInt();
for(int i=0;i<test;i++)
{
String t = sc.next();
int len = t.length();
String min = t.substring(3,5);
int h = Integer.parseInt(t.substring(0,2));
int m = Integer.parseInt(t.substring(3));
String word[] = {
"Zero", "one", "two", "three", "four",
"five", "six", "seven", "eight", "nine",
"ten", "eleven", "twelve", "thirteen",
"fourteen", "fifteen", "sixteen", "seventeen",
"eighteen", "nineteen", "twenty", "twenty one",
"twenty two", "twenty three", "twenty four",
"twenty five", "twenty six", "twenty seven",
"twenty eight", "twenty nine"
};

String time = "";

if(m==0){
time = word[h] + " o' clock";
} else if(m==15) {
time = "quarter past " + word[h];
} else if(m==30) {
time = "half past " + word[h];
} else if(m==45) {
time = "quarter to " + word[h +1];
} else if(m==1) {
time = word[m] + " minute past " + word[h];
} else if(m<30) {
time = word[m] + " minutes past " + word[h];
} else {
time = word[60-m] + " minutes to " + word[h+1];
}
System.out.println(time);
}
}
}

/*Program2*/
/*Quiz Show*/
import java.util.*;
class Quiz
{
public static void main(String arg[])
{
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
if(n<=3 || n>11)
{

}
char answers[][]=new char[n][5];
char key[]=new char[5];
for(int i=0;i<n;i++)
{
for(int j=0;j<5;j++)
{
answers[i][j]=sc.next().charAt(0);
}
}
for(int i=0;i<5;i++)
{
key[i]=sc.next().charAt(0);
}
int high=0;
int score[]=new int[n];
for(int i=0;i<n;i++)
{
for(int j=0;j<5;j++)
{
if(answers[i][j]==key[j])
{
score[i]++;
}
}
if(score[i]>high)
{
high=score[i];
}
}
for(int i=0;i<n;i++)
{
if(score[i]==high)
{
System.out.println(i+1);
}
}
}
}

******************Test6**********************

******************Test7**********************

/*Program1*/
/*Number of Voweless Substrings*/

/*Write your code here */


import java.util.*;
class Main
{
public static void main(String arg[])
{
Scanner s=new Scanner(System.in);
String vow=s.nextLine();
StringTokenizer vw=new StringTokenizer(vow, "aeiouAEIOU");
System.out.println(vw.countTokens());
}
}

/*Program2*/
/*Robot Game*/

******************Test7**********************

******************Test8**********************

/*Program1*/
/*Encryption*/

import java.util.*;
public class Main{
public static void main(String[] args)
{
Scanner in=new Scanner(System.in);
int col=in.nextInt();
in.nextLine();
String a= in.nextLine();
if(a.length()%col!=0)
{
int x=a.length()%col;
for(int u=1;u<=col-x;u++)
{
a=a+'.';
}
}
String x=a.replace(' ','*');
int n=x.length()/col;
char[][] e=new char[n][col];
int i=0,k=0;
while(k!=x.length())
{
for(int j=0;j<col;j++)
{
e[i][j]=x.charAt(k);
k++;
}
i++;
}
System.out.print(col);
for(int j=0;j<col;j++)
{
for(int s=0;s<i;s++)
{
System.out.print(e[s][j]);
}
}
System.out.println();
System.out.println();
System.out.println();

}
}

/*Program2*/
/*(Pendulam - Min2Max*/

import java.util.*;
class Main
{
static void arrangePendulum(int arr[], int n)
{
Arrays.sort(arr);
int op[] = new int[n];
int mid = (n-1)/2;
int j = 1, i =1;
op[mid] = arr[0];
for(i=1; i<=mid; i++)
{
op[mid+i] = arr[j++];
op[mid-i] = arr[j++];
}
if(n%2 == 0)
op[mid+i] = arr[j];
for(i=0;i<n;i++)
System.out.print(op[i] + " ");
System.out.println();
}
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
int p=sc.nextInt();
int[] arr=new int[p];
for(int i=0;i<p;i++)
{
arr[i]=sc.nextInt();
}
arrangePendulum(arr, p);
}
}

******************Test8**********************

******************Test9**********************

/*Program2*/
/*(Poker (REPTC)*/
/*Program2*/
/*(Samudra Chakravarthi Sequence*/

import java.util.Scanner;
class test{
int persistance(int num){
int count=0, sum=1,i;
Boolean r=true;
String string=null;
if(num<10){
return 0;
}
else{
string=Integer.toString(num);
while(r)
{
for(i=0;i<string.length()-1;i++)
{
sum=sum*Integer.parseInt(string.substring(i,i+1));
}
sum=sum*Integer.parseInt(string.substring(i,string.length()));
if(sum<10)
{
r=false;
}
else{
string=Integer.toString(sum);
sum=1;
}
count++;
}
}
return count;
}
public static void main(String [] args)
{
Scanner sc= new Scanner(System.in);
test c=new test();
System.out.print(c.persistance(sc.nextInt()));
}
}

******************Test9**********************

You might also like