Accept a sentence terminated by ‘.’, ‘?’ or ‘!’ only. Sentence may contain any of ()[]{} plus letters/spaces. It must contain no digits and no other symbols besides the final terminator. For any violation, display: INVALID INPUT. Perform the following tasks: (a) Check if the brackets are balanced and properly nested. (b) Print the maximum nesting depth of brackets. (c) If unbalanced, print the index (1-based indexing) of the first error. • A mismatched/extra closing bracket is an error at that bracket’s index. • If brackets are incomplete at the terminator, the first error is the index of the first bracket that cannot be matched (typically where the mismatch first occurs). Test your program for the following data and some random data: Example 1 INPUT: This (is [very {deep}]) indeed! OUTPUT: BALANCED: YES MAX DEPTH: 3 Example 2 INPUT: Simple () test. OUTPUT: BALANCED: YES MAX DEPTH: 1 Example 3 INPUT: Hello world. OUTPUT: BALANCED: YES MAX DEPTH: 0 Example 4 INPUT: We love (balanced [brackets)! OUTPUT: BALANCED: NO ERROR AT INDEX: 22 Example 5 INPUT: Hi ! OUTPUT: INVALID INPUT
SOLUTION:
import java.util.Scanner;
import java.util.Stack;
public class BracketSentenceCheck {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String s = sc.nextLine();
sc.close();
if (s == null || s.length() == 0) {
System.out.println("INVALID INPUT");
return;
}
// Check terminator
char term = s.charAt(s.length() - 1);
if (term != '.' && term != '?' && term != '!') {
System.out.println("INVALID INPUT");
return;
}
String body = s.substring(0, s.length() - 1); // exclude terminator
// Validate allowed characters: letters, spaces, and brackets ()[]{}
for (int i = 0; i < body.length(); i++) {
char c = body.charAt(i);
if (Character.isLetter(c) || c == ' '
|| c == '(' || c == ')' || c == '[' || c == ']' || c == '{' || c == '}') {
continue;
} else {
System.out.println("INVALID INPUT");
return;
}
}
// Stack for brackets: store pair (char bracket, index 1-based in original sentence)
class Pair {
char ch;
int idx;
Pair(char ch, int idx) { this.ch = ch; this.idx = idx; }
}
Stack st = new Stack();
int maxDepth = 0;
// iterate through original string to get 1-based indices including terminator position
// but only examine positions upto length-1 (body)
for (int i = 0; i < body.length(); i++) {
char c = body.charAt(i);
int pos = i + 1; // 1-based indexing as required
if (c == '(' || c == '[' || c == '{') {
st.push(new Pair(c, pos));
if (st.size() > maxDepth) maxDepth = st.size();
} else if (c == ')' || c == ']' || c == '}') {
if (st.isEmpty()) {
// extra closing bracket -> error at this bracket's index
System.out.println("BALANCED: NO");
System.out.println("ERROR AT INDEX: " + pos);
return;
} else {
Pair top = st.peek();
if (!matches(top.ch, c)) {
// mismatched closing bracket -> error at this bracket's index
System.out.println("BALANCED: NO");
System.out.println("ERROR AT INDEX: " + pos);
return;
} else {
st.pop();
}
}
} else {
// letters/spaces - ignore
}
}
// after processing body, check stack
if (!st.isEmpty()) {
// unmatched opening bracket(s). Error index is the index of the first bracket that cannot be matched.
// That is the position (1-based) of the earliest opening bracket in the stack that stays unmatched.
Pair firstUnmatched = st.firstElement(); // bottom-most unmatched opening bracket (earliest)
System.out.println("BALANCED: NO");
System.out.println("ERROR AT INDEX: " + firstUnmatched.idx);
return;
}
// balanced
System.out.println("BALANCED: YES");
System.out.println("MAX DEPTH: " + maxDepth);
}
private static boolean matches(char open, char close) {
return (open == '(' && close == ')')
|| (open == '[' && close == ']')
|| (open == '{' && close == '}');
}
}
ICSE And ISC Tutorials By Sandeep Goswami Sir
The purpose of creating this page is only to get Tuition and Coaching free environment. Make the students more confident and flexible.
A Digit Permutation Cipher is a simple form of number encryption where the digits of a number are rearranged based on a given key where (1
The 12-hour clock format (A.M./P.M.) is a time convention in which the 24 hours of the day are divided into two periods. The 24-hour clock format is the international standard. Example: If time = 9:35 PM, then output will be 21:35 Write a program to accept a time in 12-hour clock format (HH:MM AM/PM) and convert it to 24-hour clock format (HH:MM). Test your program for the following data and some random data: Example 1 INPUT: 11:30 PM OUTPUT: 23:30 Example 2 INPUT: 12:00 AM OUTPUT: 00:00 Example 3 INPUT: 1:45 AM OUTPUT: 01:45 Example 4 INPUT: 12:00 PM OUTPUT: 12:00 Example 5 INPUT: 13:30 AM OUTPUT: INVALID INPUT
Solution:
import java.util.Scanner;
class TimeConvert {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter time in 12-hour format (HH:MM AM/PM): ");
String input = sc.nextLine().trim();
String[] parts = input.split(" ");
if (parts.length != 2) {
System.out.println("INVALID INPUT");
return;
}
String timePart = parts[0];
String period = parts[1].toUpperCase();
if (!(period.equals("AM") || period.equals("PM"))) {
System.out.println("INVALID INPUT");
return;
}
String[] time = timePart.split(":");
if (time.length != 2) {
System.out.println("INVALID INPUT");
return;
}
int hour, minute;
try {
hour = Integer.parseInt(time[0]);
minute = Integer.parseInt(time[1]);
} catch (NumberFormatException e) {
System.out.println("INVALID INPUT");
return;
}
if (hour < 1 || hour > 12 || minute < 0 || minute > 59) {
System.out.println("INVALID INPUT");
return;
}
if (period.equals("AM")) {
if (hour == 12) {
hour = 0;
}
} else {
if (hour != 12) {
hour = hour + 12;
}
}
System.out.printf("%02d:%02d%n", hour, minute);
}
}
22/05/2024
26/09/2023
Scale your skills at lightning speed with Skill India Digital. Break geographical barriers and stay updated with real-time curriculum updates. Are you ready to skill at the speed of digital with us?
Narendra Modi Press Information Bureau - PIB, Government of India NITI Aayog Dharmendra Pradhan Rajeev Chandrasekhar Press Trust of India - PTI Doordarshan National (DD1) DDNewsLive
26/09/2023
26/09/2023
27 सितम्बर 2023 को ईपीएफओ 2.0 के तहत जिलों में आउटरीच कार्यक्रम आयोजित कर रहा है।
कैंप का स्थान जानने के लिए यहां 👇🏻 क्लिक करें
https://www.epfindia.gov.in/site_docs/PDFs/Updates/NAN_Camps_Venue_Details-September%202023%20-%20FINAL%201%20PM.pdf
#ईपीएफ #पीएफ #ईपीएफओ
PMO India Bhupender Yadav BJP Rameswar Teli Ministry of Labour and Employment, Government of India Ministry of Information & Broadcasting, Government of India Press Information Bureau - PIB, Government of India Amrit Mahotsav MyGovIndia
24/05/2023
Inspirational Story
A Prime-Adam integer is a positive integer (without leading zeros) which is a prime as well as an Adam number.
Prime number: A number which has only two factors, i.e. 1 and the number itself.
Example: 2, 3, 5, 7 ... etc.
Adam number: The square of a number and the square of its reverse are reverse to each other.
Example: If n = 13 and reverse of 'n' = 31, then,
(13)2 = 169
(31)2 = 961 which is reverse of 169
thus 13, is an Adam number.
Accept two positive integers m and n, where m is less than n as user input. Display all Prime-Adam integers that are in the range between m and n (both inclusive) and output them along with the frequency, in the format given below:
Test your program with the following data and some random data:
Example 1
INPUT:
m = 5
n = 100
OUTPUT:
THE PRIME-ADAM INTEGERS ARE:
11 13 31
FREQUENCY OF PRIME-ADAM INTEGERS IS: 3
Example 2
INPUT:
m = 100
n = 200
OUTPUT:
THE PRIME-ADAM INTEGERS ARE:
101 103 113
FREQUENCY OF PRIME-ADAM INTEGERS IS: 3
Example 3
INPUT:
m = 50
n = 70
OUTPUT:
THE PRIME-ADAM INTEGERS ARE:
NIL
FREQUENCY OF PRIME-ADAM INTEGERS IS: 0
Example 4
INPUT:
m = 700
n = 450
OUTPUT: INVALID INPUT
Solution
import java.util.Scanner;
public class PrimeAdam
{
public static int reverse(int num) {
int rev = 0;
while (num != 0) {
int d = num % 10;
rev = rev * 10 + d;
num /= 10;
}
return rev;
}
public static boolean isAdam(int num) {
int sqNum = num * num;
int revNum = reverse(num);
int sqRevNum = revNum * revNum;
int rev = reverse(sqNum);
return rev == sqRevNum;
}
public static boolean isPrime(int num) {
int c = 0;
for (int i = 1; i = n) {
System.out.println("INVALID INPUT");
return;
}
System.out.println("THE PRIME-ADAM INTEGERS ARE:");
for (int i = m; i
Click here to claim your Sponsored Listing.
Location
Category
Telephone
Website
Address
Vindhyachal Road
Mirzapur
231001