-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHelloCoding.java
More file actions
117 lines (101 loc) · 2.99 KB
/
Copy pathHelloCoding.java
File metadata and controls
117 lines (101 loc) · 2.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
import java.security.KeyStore.Entry;
import java.util.Arrays;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Scanner;
/**
*
*/
/**
* @author Abhishek Miahra
*
*/
public class HelloCoding {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
// 1.
System.out.println("Hello, World.");
System.out.println("Hello, Java.");
//2.
Scanner scan = new Scanner(System.in);
int a = scan.nextInt();
System.out.println(a);
//3.
String ans = null;
if(a%2==1 && a>10){
ans = "Weird";
}
else{
ans = "Not Weird";
}
System.out.println(ans);
//4. Is number Prime
System.out.println("Is number "+a+ " Prime ? "+isNumPrime(a));
//5.
System.out.println("First non repeated character: "+getFirstNonRepeatedChar("ahmednagar"));
}
public static boolean isNumPrime(int num){
for(int i=2; i<num; i++){
if(num%i == 0){
return false; //number is divisible so its not prime
}
}
return true; //number is prime now
}
private static boolean isArmStrong(int num) {
// 153 = 1^3 + 5^3 + 3^3
int result = 0;
int orig = num;
while(num != 0){
int remainder = num%10;
result = result + remainder*remainder*remainder;
num = num/10;
}
//number is Armstrong return true
if(orig == result){
return true;
}
return false; }
public static int factorial(int num){
//base case
if(num == 0){
return 1;
}
return num*factorial(num -1); //is this tail-recursion?
}
/* * Using LinkedHashMap to find first non repeated character of String
* * Algorithm :
* * Step 1: get character array and loop through it to build a
* * hash table with char and their count.
* * Step 2: loop through LinkedHashMap to find an entry with
* * value 1, that's your first non-repeated character,
* * as LinkedHashMap maintains insertion order. */
public static char getFirstNonRepeatedChar(String str) {
Map<Character,Integer> counts = new LinkedHashMap<>(str.length());
for (char c : str.toCharArray()) {
counts.put(c, counts.containsKey(c) ? counts.get(c) + 1 : 1);
}
for (java.util.Map.Entry<Character, Integer> entry : counts.entrySet()) {
if (entry.getValue() == 1) {
return entry.getKey();
}
} throw new RuntimeException("didn't find any non repeated Character");
}
public static boolean iAnagram(String word, String anagram){
char[] charFromWord = word.toCharArray();
char[] charFromAnagram = anagram.toCharArray();
Arrays.sort(charFromWord);
Arrays.sort(charFromAnagram);
return Arrays.equals(charFromWord, charFromAnagram);
}
//Select D.NAME, count(*) as count
//from Employee E right join Department D
//on E.DEPT_ID = D.DEPT_ID
//group by D.NAME,E.NAME
//order by count desc, D.NAME asc
//select E.ID from (Select E.ID, count(*)
//from Employee group by ID order by ID desc limit 0,1) as Temp
}