-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
f28589d
commit 7041cd7
Showing
11 changed files
with
348 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
import java.lang.annotation.*; | ||
import java.lang.reflect.*; | ||
import java.util.*; | ||
|
||
@Target(ElementType.METHOD) | ||
@Retention(RetentionPolicy.RUNTIME) | ||
@interface FamilyBudget { | ||
String userRole() default "GUEST"; | ||
int budgetLimit() default 100; | ||
} | ||
|
||
class FamilyMember { | ||
@FamilyBudget(userRole = "SENIOR", budgetLimit = 100) | ||
public void seniorMember(int budget, int moneySpend) { | ||
System.out.println("Senior Member"); | ||
System.out.println("Spend: " + moneySpend); | ||
System.out.println("Budget Left: " + (budget - moneySpend)); | ||
} | ||
|
||
@FamilyBudget(userRole = "JUNIOR", budgetLimit = 50) | ||
public void juniorUser(int budget, int moneySpend) { | ||
System.out.println("Junior Member"); | ||
System.out.println("Spend: " + moneySpend); | ||
System.out.println("Budget Left: " + (budget - moneySpend)); | ||
} | ||
} | ||
|
||
public class Annotations { | ||
public static void main(String[] args) { | ||
Scanner in = new Scanner(System.in); | ||
int testCases = Integer.parseInt(in.nextLine()); | ||
while (testCases > 0) { | ||
String role = in.next(); | ||
int spend = in.nextInt(); | ||
try { | ||
Class annotatedClass = FamilyMember.class; | ||
Method[] methods = annotatedClass.getMethods(); | ||
for (Method method : methods) { | ||
if (method.isAnnotationPresent(FamilyBudget.class)) { | ||
FamilyBudget family = method | ||
.getAnnotation(FamilyBudget.class); | ||
String userRole = family.userRole(); | ||
int budgetLimit = family.budgetLimit(); | ||
if (userRole.equals(role)) { | ||
if(spend <= budgetLimit){ | ||
method.invoke(FamilyMember.class.newInstance(), | ||
budgetLimit, spend); | ||
}else{ | ||
System.out.println("Budget Limit Over"); | ||
} | ||
} | ||
} | ||
} | ||
} catch (Exception e) { | ||
e.printStackTrace(); | ||
} | ||
testCases--; | ||
} | ||
} | ||
} | ||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import java.util.*; | ||
|
||
public class Dequeue { | ||
public static void main(String[] args) { | ||
Scanner in = new Scanner(System.in); | ||
Deque<Integer> deque = new ArrayDeque<>(); | ||
int n = in.nextInt(); | ||
int m = in.nextInt(); | ||
Set<Integer> set = new HashSet<>(); | ||
int unique = 0; | ||
for (int i = 0; i < n; i++) { | ||
int num = in.nextInt(); | ||
deque.offer(num); | ||
set.add(num); | ||
if(deque.size() == m) { | ||
unique = Math.max(unique, set.size()); | ||
if(unique == m) { | ||
break; | ||
} | ||
int removed = deque.poll(); | ||
if(!deque.contains(removed)) { | ||
set.remove(removed); | ||
} | ||
} | ||
} | ||
System.out.println(unique); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
import java.io.*; | ||
import java.util.*; | ||
interface PerformOperation { | ||
boolean check(int a); | ||
} | ||
class MyMath { | ||
public static boolean checker(PerformOperation p, int num) { | ||
return p.check(num); | ||
} | ||
|
||
// Write your code here | ||
public PerformOperation isOdd() { | ||
return num -> num % 2 != 0; | ||
} | ||
|
||
public PerformOperation isPalindrome() { | ||
return num -> { | ||
StringBuilder x = new StringBuilder(); | ||
x.append(String.valueOf(num)); | ||
return String.valueOf(num).equals(x.reverse().toString()); | ||
}; | ||
} | ||
|
||
public PerformOperation isPrime() { | ||
return num -> { | ||
int c = 0; | ||
for(int i = 2; i < Math.sqrt(num); i++){ | ||
if(num % i == 0){ | ||
c++; | ||
break; | ||
} | ||
} | ||
return c == 0; | ||
}; | ||
} | ||
} | ||
|
||
public class Lambda_Expressions { | ||
|
||
public static void main(String[] args) throws IOException { | ||
MyMath ob = new MyMath(); | ||
BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); | ||
int T = Integer.parseInt(br.readLine()); | ||
PerformOperation op; | ||
boolean ret = false; | ||
String ans = null; | ||
while (T--> 0) { | ||
String s = br.readLine().trim(); | ||
StringTokenizer st = new StringTokenizer(s); | ||
int ch = Integer.parseInt(st.nextToken()); | ||
int num = Integer.parseInt(st.nextToken()); | ||
if (ch == 1) { | ||
op = ob.isOdd(); | ||
ret = ob.checker(op, num); | ||
ans = (ret) ? "ODD" : "EVEN"; | ||
} else if (ch == 2) { | ||
op = ob.isPrime(); | ||
ret = ob.checker(op, num); | ||
ans = (ret) ? "PRIME" : "COMPOSITE"; | ||
} else if (ch == 3) { | ||
op = ob.isPalindrome(); | ||
ret = ob.checker(op, num); | ||
ans = (ret) ? "PALINDROME" : "NOT PALINDROME"; | ||
|
||
} | ||
System.out.println(ans); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
import java.io.*; | ||
import java.util.*; | ||
import java.text.*; | ||
import java.math.*; | ||
import java.util.regex.*; | ||
import java.lang.reflect.*; | ||
import static java.lang.System.in; | ||
|
||
class Prime { | ||
private boolean isPrime(int n) { | ||
if(n == 2) { | ||
return true; | ||
} | ||
if(n < 2 || n % 2 == 0) { | ||
return false; | ||
} | ||
for(int j = 3; j < Math.sqrt(n); j += 2){ | ||
if(n % j == 0){ | ||
return false; | ||
} | ||
} | ||
return true; | ||
} | ||
|
||
public void checkPrime(int... num){ | ||
StringBuilder str = new StringBuilder(); | ||
for(int i : num) { | ||
if(isPrime(i)) { | ||
str.append(i).append(" "); | ||
} | ||
} | ||
System.out.println(str); | ||
} | ||
} | ||
|
||
public class PrimeChecker { | ||
|
||
public static void main(String[] args) { | ||
try{ | ||
BufferedReader br=new BufferedReader(new InputStreamReader(in)); | ||
int n1=Integer.parseInt(br.readLine()); | ||
int n2=Integer.parseInt(br.readLine()); | ||
int n3=Integer.parseInt(br.readLine()); | ||
int n4=Integer.parseInt(br.readLine()); | ||
int n5=Integer.parseInt(br.readLine()); | ||
Prime ob=new Prime(); | ||
ob.checkPrime(n1); | ||
ob.checkPrime(n1,n2); | ||
ob.checkPrime(n1,n2,n3); | ||
ob.checkPrime(n1,n2,n3,n4,n5); | ||
Method[] methods=Prime.class.getDeclaredMethods(); | ||
Set<String> set=new HashSet<>(); | ||
boolean overload=false; | ||
for(int i=0;i<methods.length;i++) | ||
{ | ||
if(set.contains(methods[i].getName())) | ||
{ | ||
overload=true; | ||
break; | ||
} | ||
set.add(methods[i].getName()); | ||
} | ||
if(overload) | ||
{ | ||
throw new Exception("Overloading not allowed"); | ||
} | ||
} | ||
catch(Exception e) | ||
{ | ||
System.out.println(e); | ||
} | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.Scanner; | ||
/* | ||
* Create the Student and Priorities classes here. | ||
*/ | ||
import java.util.Optional; | ||
import java.util.PriorityQueue; | ||
import java.util.stream.Collectors; | ||
import static java.util.Comparator.comparing; | ||
import static java.util.Comparator.comparingInt; | ||
import static java.util.Comparator.comparingDouble; | ||
|
||
class Student { | ||
private int id; | ||
private String name; | ||
private double cgpa; | ||
|
||
public Student(int id, String name, double cgpa) { | ||
this.id = id; | ||
this.name = name; | ||
this.cgpa = cgpa; | ||
} | ||
|
||
public int getID() { | ||
return this.id; | ||
} | ||
|
||
public String getName() { | ||
return this.name; | ||
} | ||
|
||
public double getCGPA() { | ||
return this.cgpa; | ||
} | ||
} | ||
|
||
class Priorities { | ||
private List<Student> students = new ArrayList<>(); | ||
|
||
public List<Student> getStudents(List<String> events) { | ||
events.forEach(item -> { | ||
if(item.startsWith("ENTER")) { | ||
String[] attributes = item.split(" "); | ||
this.students.add(new Student(Integer.valueOf(attributes[3]), attributes[1], Double.valueOf(attributes[2]))); | ||
} else if(item.equals("SERVED") && !this.students.isEmpty()) { | ||
Optional<Student> max = this.students.stream() | ||
.sorted(comparingDouble(Student::getCGPA).reversed() | ||
.thenComparing(Student::getName) | ||
.thenComparingInt(Student::getID)) | ||
.findFirst(); | ||
if(max.isPresent()) { | ||
this.students.remove(max.get()); | ||
} | ||
} | ||
}); | ||
return this.students.stream().sorted(comparingDouble(Student::getCGPA).reversed() | ||
.thenComparing(Student::getName) | ||
.thenComparingInt(Student::getID)) | ||
.collect(Collectors.toList()); | ||
} | ||
} | ||
|
||
class PrioritiesOptimized { | ||
private PriorityQueue<Student> students = new PriorityQueue<>( | ||
comparingDouble(Student::getCGPA).reversed() | ||
.thenComparing(Student::getName) | ||
.thenComparingInt(Student::getID)); | ||
|
||
public List<Student> getStudents(List<String> events) { | ||
events.forEach(item -> { | ||
if(item.startsWith("ENTER")) { | ||
String[] attributes = item.split(" "); | ||
this.students.add(new Student(Integer.valueOf(attributes[3]), attributes[1], Double.valueOf(attributes[2]))); | ||
} else if(item.equals("SERVED") && !this.students.isEmpty()) { | ||
students.poll(); | ||
} | ||
}); | ||
return students.stream().sorted(comparingDouble(Student::getCGPA).reversed() | ||
.thenComparing(Student::getName) | ||
.thenComparingInt(Student::getID)).collect(Collectors.toList()); | ||
} | ||
} | ||
|
||
|
||
public class PrioritiesQueue { | ||
private final static Scanner scan = new Scanner(System.in); | ||
private final static Priorities priorities = new Priorities(); | ||
|
||
public static void main(String[] args) { | ||
int totalEvents = Integer.parseInt(scan.nextLine()); | ||
List<String> events = new ArrayList<>(); | ||
|
||
while (totalEvents-- != 0) { | ||
String event = scan.nextLine(); | ||
events.add(event); | ||
} | ||
|
||
List<Student> students = priorities.getStudents(events); | ||
|
||
if (students.isEmpty()) { | ||
System.out.println("EMPTY"); | ||
} else { | ||
for (Student st: students) { | ||
System.out.println(st.getName()); | ||
} | ||
} | ||
} | ||
} |
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.