Showing posts with label functional programming. Show all posts
Showing posts with label functional programming. Show all posts

Friday, January 27, 2023

My GitHub Repo #25 : Tokyo

Code Samples for [Algos & DS, OOPs, Lambdas]
MIT License, Copyright (c) 2018-19, Sumith Kumar Puri
https://github.com/sumithpuri


[Java] Problem : Changes in Usernames (HackerRank)
[Java] Problem : Active Traders (HackerRank)
[Java] [ FP ] : Functional Programming & Lambdas (TechGig)
[Java] [OOPs] : Object Oriented Programming (TechGig)
[Java] Problem : Monkeys in the Garden (TechGig)
[Java] Problem : AutoComplete Using Trie Data Structure
[Java] Problem : Longest Palindrome in String









Project Codename

Tokyo

Blog Post URL

http://www.techilashots.blog/2015/09/introduction-to-complex-event.html

Blog Short URL

Package Prefix

me.sumithpuri.github.tokyo

GitHub URL

https://github.com/sumithpuri/skp-winter-code-nights-tokyo

Contact E-Mail

code@sumithpuri.xyz

Contact Number

+91 9591497974 (WhatsApp, Viber, Telegram)

Historical

 Started this Movement of 1000s of Lines of Java / J2EE* Code to GitHub

 Was a Senior Software Architect (Java/J2EE) in Manila*, 2018 (At Start) 

 Named this Initial Code Journey as [ Manila Code Marathon - 2018 ]

 Code Is Non-Proprietary and Non-Copyright from my Work Experience.

 Was Back to Bangalore, Named as [ Bangalore Code Nights - 2019. ]

 Added More Code under [ -20 Days of Code in Benglauru- ] in 2020

 Celebration of Java/Java EE Code as Java Turned 25 in the Year ~ 2020!

  

Tuesday, February 2, 2021

SKP's Java Problem Solving Series : Refresh Java Lambdas (FP)

[Question/Problem Statement is the Property of Techgig]

Java Advanced - Lambda Expressions [www.techgig.com] 
Write the Following Methods that Return a Lambda Expression Performing a Specified Action: PerformOperation isOdd(): The Lambda Expression must return  if a Number is Odd or  If it is Even. PerformOperation isPrime(): The lambda expression must return  if a number is prime or  if it is composite. PerformOperation isPalindrome(): The Lambda Expression must return  if a number is a Palindrome or if it is not.

Input Format
Input is as Show in the Format Below (Deduce Unknowns!)

Input
3
1 3
2 7
3 7777

Constraints
NA

Output Format
Output is as Show in the Format Below (Deduce Unknowns!)

Output
ODD
PRIME
PALINDROME
______________ 
 
 
[Explanation of the Solution]
This is a Good Question to Refresh Java 8 Lambdas. In my Solution, I Implemented the Functional Interfaces within my main() Method and assigned it to Local Reference Variables.
 
________________  
 

[Source Code, Sumith Puri (c) 2021 - Free to Use & Distribute]
 import java.util.Scanner;

/*    
 * Techgig Core Java Basics Problem - Knock Off Java Lambdas!   
 * Author: Sumith Puri [I Bleed Java!] // GitHub: @sumithpuri   
 */
interface LambdaYogi {
	public boolean opYog(int x);
}

public class CandidateCode {
	public static void main(String args[]) throws Exception {

                // you may choose to refactor, as this method
                // becomes really long and unmanageable #TODO
		LambdaYogi isOdd = a -> {
			boolean retFlag = false;
			if (a % 2 != 0)
				retFlag = true;
			return retFlag;
		};
		LambdaYogi isPrime = a -> {
			boolean retFlag = true;
			for (int i = 2; i < a - 1; i++) {
				if (a % i == 0) {
					retFlag = false;
					break;
				}
			}
			return retFlag;
		};
		LambdaYogi isPalindrome = a -> {
			boolean retFlag = false;
			String actStr = String.valueOf(a).trim();
			String revStr = new StringBuffer(actStr).reverse().toString();

			// using string basis, not mathematical
			if (actStr.equals(revStr))
				retFlag = true;
			return retFlag;
		};

		Scanner scanner = new Scanner(System.in);
		int val = scanner.nextInt();

		for (int i = 0; i < val; i++) {
			int op = scanner.nextInt();
			int no = scanner.nextInt();
			switch (op) {
			case 1: {
				if (isOdd.opYog(no))
					System.out.println("ODD");
				else
					System.out.println("EVEN");
				break;
			}
			case 2: {
				if (isPrime.opYog(no))
					System.out.println("PRIME");
				else
					System.out.println("COMPOSITE");
				break;
			}
			case 3: {
				if (isPalindrome.opYog(no))
					System.out.println("PALINDROME");
				else
					System.out.println("NONPALIN");
			}
			}
		}
	}
}

Happy Problem Solving using Java!