Puzzles With Java

Study

Please don’t forget to visit my Amazon Kindle & Pothi stores or contact us for the books.

While no one can disagree that Python is growing so quickly. Apart from easy and human-readable syntax format, Python’s popularity in data science and artificial intelligence is probably the another driver of its fast growth. But even after more than 25 years after ‘Java’ was launched, it is still among the top three programming languages in the use across the world. Java programming language provides platform independence. Due to this it is commonly used in various segments of software development, including web and mobile development, and such trending techs as big data, IoT (Internet of Things), blockchain and artificial intelligence.

Playing with Java

Let’s take 2 puzzles with coding solution in Java.

1 MAGIC GROUP

Let’s start with a magic group.

QWERTY, NASDAQ & QING are falling in the same group. How?

Can you write a code where you can check if the given word belongs to this group?

2 ISN’T IT, MARVEL?

Namor is Marvel’s first superhero and also the first anti-hero in comics. The character’s first appearance to a general audience was in Marvel Comics No. 1. Below sentence is very unique in the Marvel Universe. If you look at it carefully, you will find out.

“Namor sees roman”

Write a program to validate this type of statement.

.

.

.

.

.

B

Y

T

E

M

A

N

.

.

.

.

.

  1. All given words QWERTY, NASDAQ, QING belong to the unique group where ‘Q’ character is not directly followed by ‘U’ character.
import java.util.Scanner;

public class Solution {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		
		Scanner sc = new Scanner(System.in);
		System.out.println("Please enter your word:");
		
		String word= sc.next().toUpperCase();
		String guess = "Q";
		
		System.out.println("Given word:"+word);
		
		int index = word.indexOf(guess);

		while(index >= 0) {
			if(index != word.length()-1) {
				if(word.charAt(index+1) != 'U') {
					System.out.println("Given word belongs to the magic group");
					System.exit(0);
				}
			} else {
				System.out.println("Given word belongs to the magic group");
				System.exit(0);
			}

			index = word.indexOf(guess, index+1);
	
		}
		System.out.println("Given word does not belong to the magic group");
	}
}

2. “Namor sees roman” – is a palindrome. A palindrome is a word, number, or sentence which reads the same backward as forward. So basically, it’s solution is a palindrome program.

import java.util.Scanner;

public class Solution {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		
		Scanner sc = new Scanner(System.in);
		System.out.println("Please enter your word:");
		
		String word= sc.nextLine().toUpperCase();
		
		StringBuilder reqStr = new StringBuilder(word);
		reqStr.reverse();
	
		System.out.println(reqStr.toString());
		if (word.equals(reqStr.toString())) {
			System.out.println("Is a palindrome. Welcome it to the group!");
		} else {
			System.out.println("Is not a palindrome. Bye bye it!");
		}
	}
}