diff --git a/Java/Introduction/Java Currency Formatter/Solution.java b/Java/Introduction/Java Currency Formatter/Solution.java new file mode 100644 index 0000000..1feef67 --- /dev/null +++ b/Java/Introduction/Java Currency Formatter/Solution.java @@ -0,0 +1,29 @@ +import java.util.Scanner; +import java.text.NumberFormat; +import java.util.Locale; + +public class Solution { + + public static void main(String[] args) { + /* Read input */ + Scanner scanner = new Scanner(System.in); + double payment = scanner.nextDouble(); + scanner.close(); + + /* Create custom Locale for India. + I used the "IANA Language Subtag Registry" to find India's country code */ + Locale indiaLocale = new Locale("en", "IN"); + + /* Create NumberFormats using Locales */ + NumberFormat us = NumberFormat.getCurrencyInstance(Locale.US); + NumberFormat india = NumberFormat.getCurrencyInstance(indiaLocale); + NumberFormat china = NumberFormat.getCurrencyInstance(Locale.CHINA); + NumberFormat france = NumberFormat.getCurrencyInstance(Locale.FRANCE); + + /* Print output */ + System.out.println("US: " + us.format(payment)); + System.out.println("India: " + india.format(payment)); + System.out.println("China: " + china.format(payment)); + System.out.println("France: " + france.format(payment)); + } +} diff --git a/Java/Introduction/Java Datatypes/Solution.java b/Java/Introduction/Java Datatypes/Solution.java new file mode 100644 index 0000000..3ad45ce --- /dev/null +++ b/Java/Introduction/Java Datatypes/Solution.java @@ -0,0 +1,30 @@ +import java.util.Scanner; + +class Solution{ + public static void main(String []args) { + + Scanner scan = new Scanner(System.in); + int t = scan.nextInt(); + + for (int i = 0; i < t; i++) { + try { + long x = scan.nextLong(); + System.out.println(x + " can be fitted in:"); + if (x >= Byte.MIN_VALUE && x <= Byte.MAX_VALUE) { + System.out.println("* byte"); + } + if (x >= Short.MIN_VALUE && x <= Short.MAX_VALUE) { + System.out.println("* short"); + } + if (x >= Integer.MIN_VALUE && x <= Integer.MAX_VALUE) { + System.out.println("* int"); + } + if (x >= Long.MIN_VALUE && x <= Long.MAX_VALUE) { + System.out.println("* long"); + } + } catch(Exception e) { + System.out.println(scan.next() +" can't be fitted anywhere."); + } + } + } +} diff --git a/Java/Introduction/Java Date and Time/Solution.java b/Java/Introduction/Java Date and Time/Solution.java new file mode 100644 index 0000000..ff21468 --- /dev/null +++ b/Java/Introduction/Java Date and Time/Solution.java @@ -0,0 +1,18 @@ +import java.util.Scanner; +import java.time.LocalDate; + +public class Solution { + + public static void main(String[] args) { + /* Read input */ + Scanner scan = new Scanner(System.in); + int month = scan.nextInt(); + int day = scan.nextInt(); + int year = scan.nextInt(); + scan.close(); + + /* Create LocalDate instance */ + LocalDate date = LocalDate.of(year, month, day); + System.out.println(date.getDayOfWeek()); + } +} diff --git a/Java/Introduction/Java End-of-file/Solution.java b/Java/Introduction/Java End-of-file/Solution.java new file mode 100644 index 0000000..ca0bdde --- /dev/null +++ b/Java/Introduction/Java End-of-file/Solution.java @@ -0,0 +1,13 @@ +import java.util.Scanner; + +public class Solution { + + public static void main(String[] args) { + Scanner scan = new Scanner(System.in); + int i = 1; + while (scan.hasNextLine()) { + System.out.println(i + " " + scan.nextLine()); + i++; + } + } +} diff --git a/Java/Introduction/Java Int to String/Solution.java b/Java/Introduction/Java Int to String/Solution.java new file mode 100644 index 0000000..b18b828 --- /dev/null +++ b/Java/Introduction/Java Int to String/Solution.java @@ -0,0 +1,49 @@ +import java.util.*; +import java.security.*; +public class Solution { + public static void main(String[] args) { + + DoNotTerminate.forbidExit(); + + try { + Scanner in = new Scanner(System.in); + int n = in .nextInt(); + in.close(); + //String s=???; Complete this line below + + /* Here are 3 ways to convert an int to a String */ + // String s = String.valueOf(n); + // String s = Integer.toString(n); + String s = "" + n; + + if (n == Integer.parseInt(s)) { + System.out.println("Good job"); + } else { + System.out.println("Wrong answer."); + } + } catch (DoNotTerminate.ExitTrappedException e) { + System.out.println("Unsuccessful Termination!!"); + } + } +} + +//The following class will prevent you from terminating the code using exit(0)! +class DoNotTerminate { + + public static class ExitTrappedException extends SecurityException { + + private static final long serialVersionUID = 1; + } + + public static void forbidExit() { + final SecurityManager securityManager = new SecurityManager() { + @Override + public void checkPermission(Permission permission) { + if (permission.getName().contains("exitVM")) { + throw new ExitTrappedException(); + } + } + }; + System.setSecurityManager(securityManager); + } +} diff --git a/Java/Introduction/Java Loops I/Solution.java b/Java/Introduction/Java Loops I/Solution.java new file mode 100644 index 0000000..184aa41 --- /dev/null +++ b/Java/Introduction/Java Loops I/Solution.java @@ -0,0 +1,12 @@ +import java.util.Scanner; + +public class Solution { + + public static void main(String[] args) { + Scanner scan = new Scanner(System.in); + int multiplier = scan.nextInt(); + for (int i = 1; i <= 10; i++) { + System.out.format("%d x %d = %d%n", multiplier, i , i * multiplier); + } + } +} diff --git a/Java/Introduction/Java Loops II/Solution.java b/Java/Introduction/Java Loops II/Solution.java new file mode 100644 index 0000000..9924727 --- /dev/null +++ b/Java/Introduction/Java Loops II/Solution.java @@ -0,0 +1,20 @@ +import java.util.Scanner; + +class Solution{ + public static void main(String []args) { + Scanner scan = new Scanner(System.in); + int t = scan.nextInt(); + for (int i = 0; i < t; i++) { + int a = scan.nextInt(); + int b = scan.nextInt(); + int n = scan.nextInt(); + + for (int j = 0; j < n; j++) { + a += b * (int) Math.pow(2, j); + System.out.print(a + " "); + } + System.out.println(); + } + scan.close(); + } +} diff --git a/Java/Introduction/Java Output Formatting/Solution.java b/Java/Introduction/Java Output Formatting/Solution.java new file mode 100644 index 0000000..7e9f0b6 --- /dev/null +++ b/Java/Introduction/Java Output Formatting/Solution.java @@ -0,0 +1,15 @@ +import java.util.Scanner; + +public class Solution { + + public static void main(String[] args) { + Scanner sc = new Scanner(System.in); + System.out.println("================================"); + for (int i = 0; i < 3; i++) { + String s1 = sc.next(); + int x = sc.nextInt(); + System.out.format("%-15s%03d%n", s1, x); + } + System.out.println("================================"); + } +} diff --git a/Java/Introduction/Java Static Initializer Block/Solution.java b/Java/Introduction/Java Static Initializer Block/Solution.java new file mode 100644 index 0000000..8eb3e54 --- /dev/null +++ b/Java/Introduction/Java Static Initializer Block/Solution.java @@ -0,0 +1,33 @@ +import java.io.*; +import java.util.*; +import java.text.*; +import java.math.*; +import java.util.regex.*; + +public class Solution { + private static int B; + private static int H; + private static boolean flag; + + static { + Scanner scan = new Scanner(System.in); + B = scan.nextInt(); + H = scan.nextInt(); + scan.close(); + if (B <= 0 || H <= 0) { + System.out.println("java.lang.Exception: Breadth and height must be positive"); + flag = false; + } else { + flag = true; + } + } + + public static void main(String[] args){ + if(flag){ + int area=B*H; + System.out.print(area); + } + + }//end of main + +}//end of class diff --git a/Java/Strings/Java Anagrams/Solution.java b/Java/Strings/Java Anagrams/Solution.java new file mode 100644 index 0000000..86529c5 --- /dev/null +++ b/Java/Strings/Java Anagrams/Solution.java @@ -0,0 +1,47 @@ +import java.io.*; +import java.util.*; + +public class Solution { + /* Runtime: O(n) using a HashMap */ + static boolean isAnagram(String a, String b) { + if (a == null || b == null || a.length() != b.length()) { + return false; + } + + a = a.toLowerCase(); + b = b.toLowerCase(); + + HashMap map = new HashMap<>(); + + /* Fill HashMap with 1st String */ + for (int i = 0; i < a.length(); i++) { + char ch = a.charAt(i); + if (map.containsKey(ch)) { + map.put(ch, map.get(ch) + 1); + } else { + map.put(ch, 1); + } + } + + /* Compare 2nd String to 1st String's HashMap */ + for (int i = 0; i < b.length(); i++) { + char ch = b.charAt(i); + if (map.containsKey(ch) && map.get(ch) > 0) { + map.put(ch, map.get(ch) - 1); + } else { + return false; + } + } + return true; + } + + public static void main(String[] args) { + + Scanner scan = new Scanner(System.in); + String a = scan.next(); + String b = scan.next(); + scan.close(); + boolean ret = isAnagram(a, b); + System.out.println( (ret) ? "Anagrams" : "Not Anagrams" ); + } +} diff --git a/Java/Strings/Java String Compare/Solution.java b/Java/Strings/Java String Compare/Solution.java new file mode 100644 index 0000000..1fbd1e9 --- /dev/null +++ b/Java/Strings/Java String Compare/Solution.java @@ -0,0 +1,30 @@ +import java.util.Scanner; + +public class Solution { + + public static void main(String[] args) { + /* Read input */ + Scanner scan = new Scanner(System.in); + String s = scan.nextLine(); + int k = scan.nextInt(); + scan.close(); + + /* Create smallest and largest strings and initialize them */ + String smallest = s.substring(0, k); + String largest = s.substring(0, k); + + for (int i = 0; i <= s.length() - k; i++) { + String curr = s.substring(i, i + k); + if (smallest.compareTo(curr) > 0){ + smallest = curr; + } + if (largest.compareTo(curr) < 0) { + largest = curr; + } + } + + /* Print results */ + System.out.println(smallest); + System.out.println(largest); + } +} diff --git a/Java/Strings/Java String Reverse/Solution.java b/Java/Strings/Java String Reverse/Solution.java new file mode 100644 index 0000000..372f8a0 --- /dev/null +++ b/Java/Strings/Java String Reverse/Solution.java @@ -0,0 +1,16 @@ +import java.util.Scanner; + +/* If a String is equivalent to itself when reversed, it's a palindrome */ +public class Solution { + + public static void main(String[] args) { + /* Read input */ + Scanner scan = new Scanner(System.in); + String str = scan.nextLine(); + scan.close(); + + /* Reverse string and compare to original */ + String reversed = new StringBuilder(str).reverse().toString(); + System.out.println(str.equals(reversed) ? "Yes" : "No"); + } +} diff --git a/Java/Strings/Java Strings Introduction/Solution.java b/Java/Strings/Java Strings Introduction/Solution.java new file mode 100644 index 0000000..6a85320 --- /dev/null +++ b/Java/Strings/Java Strings Introduction/Solution.java @@ -0,0 +1,29 @@ +import java.util.Scanner; + +public class Solution { + + public static void main(String[] args) { + /* Read input */ + Scanner scan = new Scanner(System.in); + String A = scan.next(); + String B = scan.next(); + scan.close(); + + /* Sum lengths */ + System.out.println(A.length() + B.length()); + + /* Compare lexicographical ordering */ + System.out.println(A.compareTo(B) > 0 ? "Yes" : "No"); + + /* Print the Strings in desired format */ + System.out.println(capFirstLetter(A) + " " + capFirstLetter(B)); + } + + private static String capFirstLetter(String str) { + if (str == null || str.length() == 0) { + return ""; + } else { + return str.substring(0,1).toUpperCase() + str.substring(1); + } + } +} diff --git a/Java/Strings/Java Substring/Solution.java b/Java/Strings/Java Substring/Solution.java new file mode 100644 index 0000000..26abca8 --- /dev/null +++ b/Java/Strings/Java Substring/Solution.java @@ -0,0 +1,15 @@ +import java.util.Scanner; + +public class Solution { + + public static void main(String[] args) { + /* Read input */ + Scanner scan = new Scanner(System.in); + String s = scan.next(); + int start = scan.nextInt(); + int end = scan.nextInt(); + scan.close(); + + System.out.println(s.substring(start, end)); + } +} diff --git a/Tutorials/10 Days of Statistics/Day 1 - Interquartile Range/Solution.java b/Tutorials/10 Days of Statistics/Day 1 - Interquartile Range/Solution.java new file mode 100644 index 0000000..36bad6e --- /dev/null +++ b/Tutorials/10 Days of Statistics/Day 1 - Interquartile Range/Solution.java @@ -0,0 +1,50 @@ +import java.util.Scanner; +import java.util.Arrays; + +public class Solution { + + public static void main(String[] args) { + /* Read and save input */ + Scanner scan = new Scanner(System.in); + int size = scan.nextInt(); + int [] element = new int[size]; + int [] frequency = new int[size]; + for (int i = 0; i < size; i++) { + element[i] = scan.nextInt(); + } + + int numElements = 0; + for (int i = 0; i < size; i++) { + frequency[i] = scan.nextInt(); + numElements += frequency[i]; + } + + /* Create and sort our data set */ + int [] data = new int[numElements]; + int dataIndex = 0; + for (int i = 0; i < size; i++) { + for (int j = 0; j < frequency[i]; j++) { + data[dataIndex] = element[i]; + dataIndex++; + } + } + Arrays.sort(data); + + /* Works with both even and odd length arrays */ + double q1 = findMedian(data, 0, data.length / 2 - 1); + double q3 = findMedian(data, (data.length + 1) / 2, data.length - 1); + + System.out.println(q3 - q1); + } + + /* Treats elements from "start" to "end" as an array and calculates its median */ + private static double findMedian(int [] array, int start, int end) { + if ((end - start) % 2 == 0) { // odd number of elements + return (array[(end + start) / 2]); + } else { // even number of elements + int value1 = array[(end + start) / 2]; + int value2 = array[(end + start) / 2 + 1]; + return (value1 + value2) / 2.0; + } + } +}