Skip to content

Commit

Permalink
Added 27 solutions
Browse files Browse the repository at this point in the history
  • Loading branch information
RodneyShag committed Mar 5, 2017
1 parent af8c296 commit 3a6797a
Show file tree
Hide file tree
Showing 43 changed files with 2,823 additions and 598 deletions.
52 changes: 26 additions & 26 deletions 10 Days of Statistics/README.md

Large diffs are not rendered by default.

15 changes: 5 additions & 10 deletions 30 Days of Code/Day 12 - Inheritance/Solution.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,20 +53,15 @@ public char calculate() {

if (average >= 90) {
return 'O';
}
else if (average >= 80) {
} else if (average >= 80) {
return 'E';
}
else if (average >= 70) {
} else if (average >= 70) {
return 'A';
}
else if (average >= 55) {
} else if (average >= 55) {
return 'P';
}
else if (average >= 40) {
} else if (average >= 40) {
return 'D';
}
else {
} else {
return 'T';
}
}
Expand Down
14 changes: 5 additions & 9 deletions 30 Days of Code/Day 14 - Scope/Solution.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,12 @@ class Difference {
this.elements = elements;
}

// O(n) runtime. O(1) space
// Runtime: O(n)
// Space Complexity: O(1)
void computeDifference() {
maximumDifference = 0;
int min = elements[0];
int max = elements[0];
for (int num : elements) {
min = Math.min(min, num);
max = Math.max(max, num);
}
maximumDifference = max - min;
int max = Arrays.stream(elements).max().getAsInt();
int min = Arrays.stream(elements).min().getAsInt();
maximumDifference = max - min ;
}
} // End of Difference class

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ public class Solution {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
String str = scan.next();
scan.close();
try {
int num = Integer.parseInt(str);
System.out.println(num);
} catch (NumberFormatException e) {
System.out.println("Bad String");
}
scan.close();
}
}
61 changes: 61 additions & 0 deletions 30 Days of Code/Day 23 - BST Level-Order Traversal/Solution.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
// Author: Rodney Shaghoulian
// Github: github.com/rshaghoulian
// HackerRank: hackerrank.com/rshaghoulian

import java.io.*;
import java.util.*;

class Node {
Node left,right;
int data;
Node(int data) {
this.data = data;
left = right = null;
}
}

class Solution {
static void levelOrder(Node root){
Queue<Node> queue = new LinkedList<>();
if (root != null) {
queue.add(root);
}
while ( ! queue.isEmpty()) {
Node n = queue.remove();
System.out.print(n.data + " ");
if (n.left != null) {
queue.add(n.left);
}
if (n.right != null) {
queue.add(n.right);
}
}
}

public static Node insert(Node root,int data){
if (root == null) {
return new Node(data);
} else {
Node cur;
if (data <= root.data) {
cur = insert(root.left, data);
root.left = cur;
}
else {
cur = insert(root.right, data);
root.right = cur;
}
return root;
}
}
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
int T = sc.nextInt();
Node root = null;
while (T-- > 0) {
int data = sc.nextInt();
root = insert(root, data);
}
levelOrder(root);
}
}
67 changes: 67 additions & 0 deletions 30 Days of Code/Day 24 - More Linked Lists/Solution.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
// Author: Rodney Shaghoulian
// Github: github.com/rshaghoulian
// HackerRank: hackerrank.com/rshaghoulian

import java.io.*;
import java.util.*;

class Node {
int data;
Node next;
Node(int d) {
data = d;
next = null;
}
}

class Solution {
public static Node removeDuplicates(Node head) {
if (head == null) {
return null;
}
Node n = head;
while (n.next != null) {
if (n.data == n.next.data) {
n.next = n.next.next;
} else {
n = n.next;
}
}
return head;
}

public static Node insert(Node head,int data) {
Node p = new Node(data);
if (head == null)
head = p;
else if (head.next == null)
head.next = p;
else {
Node start = head;
while (start.next != null)
start = start.next;
start.next = p;
}
return head;
}

public static void display(Node head) {
Node start = head;
while (start != null) {
System.out.print(start.data + " ");
start = start.next;
}
}

public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
Node head = null;
int T = sc.nextInt();
while (T-- > 0) {
int ele = sc.nextInt();
head = insert(head, ele);
}
head = removeDuplicates(head);
display(head);
}
}
31 changes: 31 additions & 0 deletions 30 Days of Code/Day 25 - Running Time and Complexity/Solution.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// Author: Rodney Shaghoulian
// Github: github.com/rshaghoulian
// HackerRank: hackerrank.com/rshaghoulian

import java.util.Scanner;

public class Solution {

public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int p = scan.nextInt();
for (int a0 = 0; a0 < p; a0++) {
int n = scan.nextInt();
System.out.println(isPrime(n) ? "Prime" : "Not prime");
}
scan.close();
}

public static boolean isPrime (int n) {
if (n < 2) {
return false;
}
int sqrt = (int) Math.sqrt(n);
for (int i = 2; i <= sqrt; i++) {
if (n % i == 0) {
return false;
}
}
return true;
}
}
36 changes: 36 additions & 0 deletions 30 Days of Code/Day 26 - Nested Logic/Solution.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// Author: Rodney Shaghoulian
// Github: github.com/rshaghoulian
// HackerRank: hackerrank.com/rshaghoulian

import java.util.Scanner;
import java.time.LocalDate;

public class Solution {
public static void main(String[] args) {
/* Read and save input as LocalDates */
Scanner scan = new Scanner(System.in);
LocalDate returnDate = readDate(scan);
LocalDate expectDate = readDate(scan);
scan.close();

/* Calculate fine */
int fine;
if (returnDate.isEqual(expectDate) || returnDate.isBefore(expectDate)) {
fine = 0;
} else if (returnDate.getMonth() == expectDate.getMonth() && returnDate.getYear() == expectDate.getYear()) {
fine = 15 * (returnDate.getDayOfMonth() - expectDate.getDayOfMonth());
} else if (returnDate.getYear() == expectDate.getYear()) {
fine = 500 * (returnDate.getMonthValue() - expectDate.getMonthValue());
} else {
fine = 10000;
}
System.out.println(fine);
}

private static LocalDate readDate(Scanner scan) {
int day = scan.nextInt();
int month = scan.nextInt();
int year = scan.nextInt();
return LocalDate.of(year, month, day);
}
}
24 changes: 24 additions & 0 deletions 30 Days of Code/Day 27 - Testing/Solution.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// Author: Rodney Shaghoulian
// Github: github.com/rshaghoulian
// HackerRank: hackerrank.com/rshaghoulian

public class Solution {
public static void main(String[] args) {
System.out.println(5);

System.out.println("4 3");
System.out.println("-1 0 1 2");

System.out.println("3 1");
System.out.println("-1 0 1");

System.out.println("5 4");
System.out.println("-1 0 1 2 3");

System.out.println("6 1");
System.out.println("-1 0 1 2 3 4");

System.out.println("7 6");
System.out.println("-1 0 1 2 3 4 5");
}
}
Loading

0 comments on commit 3a6797a

Please sign in to comment.