Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# IntelliJ IDEA files
.idea/
*.iml

# Compiled/Output
out/
*.class

# Crash logs
hs_err_pid*

# Windows
Thumbs.db
Binary file added Algorithms/lib/junit-jupiter-5.4.2.jar
Binary file not shown.
Binary file added Algorithms/lib/junit-jupiter-api-5.4.2.jar
Binary file not shown.
Binary file added Algorithms/lib/junit-jupiter-engine-5.4.2.jar
Binary file not shown.
Binary file added Algorithms/lib/junit-jupiter-params-5.4.2.jar
Binary file not shown.
Binary file added Algorithms/lib/junit-platform-commons-1.4.2.jar
Binary file not shown.
Binary file added Algorithms/lib/junit-platform-engine-1.4.2.jar
Binary file not shown.
Binary file added Algorithms/lib/opentest4j-1.1.1.jar
Binary file not shown.
80 changes: 80 additions & 0 deletions Algorithms/src/main/xyz/jdtec/algorithms/Algorithms.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
package xyz.jdtec.algorithms;

import java.util.*;

public class Algorithms {
/**
* Question 1: Find all pairs for a given sum
* Prints any pairs found with format: "(number, number)", separated by new lines.
*
* This implementation has an O(n) time complexity (linear time) as it only traverses the input array once (rather
* than nested loops). As far as I'm aware, this is the fastest possible implementation. For context, brute-force
* takes O(n^2) (polynomial time), which is horrible. Alternatively, the sort + pointers method is bit better, but
* it requires sorting, which I believe is O(n log n) best-case.
*
* @param testArray integer array to check for pairs
* @param targetSum integer sum to check against
*/
public static void findPairs(int[] testArray, int targetSum) {
// If array length < 2, we can't do anything
if (testArray.length < 2) {
return;
}

Set<List<Integer>> pairs = new HashSet<>(); // Store the found pairs in a Set to prevent duplicates
ArrayList<Integer> workingList = new ArrayList<>(); // Working list, see below for usage

// Loop through all the elements of input array
for (int num : testArray) {
// Instead of checking a + b = c, check c - a = b; ...where: a = num, b = checkNum, c = targetSum
int checkNum = targetSum - num;

if (!workingList.contains(checkNum)) {
// Add current number (a) to "working list" if it doesn't contain complement of current number (b)
workingList.add(num);
}
else {
// Complement of current number (b) was found! Therefore, we must've found a pair; add to pairs Set
pairs.add(Arrays.asList(num, checkNum));
}
}

// Run through the Set of pairs and print to stdout according to requested format
for (List<Integer> pair : pairs) {
// Note: I went through the trouble of storing the pairs as a List rather than as a pre-formatted String
// because later on, if we need to use the data in some other way, it's easy to do so.
System.out.println(String.format("(%d, %d)", pair.get(0), pair.get(1)));
}
}

/**
* Question 2: Is Palindrome
* Tells you whether a given String is a palindrome!
*
* This implementation uses the existing (and well designed, I assume) StringBuilder.reverse() method provided by
* Java standard library. It has a time complexity of O(n), should be fastest possible.
*
* @param testString String to check for palindromic status
* @return whether String is a palindrome
*/
public static boolean isPalindrome(String testString) {
// Make sure string is lowercase as "Radar" != "radar"!
testString = testString.toLowerCase();

// Use the StringBuilder built-in class to reverse the string
StringBuilder str = new StringBuilder(testString);
String reverse = str.reverse().toString();

// By definition: string is palindrome if string reads the same way normally and in reverse!
return testString.equals(reverse);
}

// Main method, for Question 1 example.
public static void main(String[] args) {
// --- Given Example for findPairs --- //
int[] testArray = {2, 4, 5, 1, 3, 5, 4};
int targetSum = 6;

findPairs(testArray, targetSum);
}
}
83 changes: 83 additions & 0 deletions Algorithms/src/test/xyz/jdtec/algorithms/AlgorithmsTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
package xyz.jdtec.algorithms;

import org.junit.jupiter.api.*;
import static org.junit.jupiter.api.Assertions.*;

import java.io.ByteArrayOutputStream;
import java.io.PrintStream;


class AlgorithmsTest {
// Instantiate our own output stream so we can test methods that print to stdout
ByteArrayOutputStream outStream = new ByteArrayOutputStream();

// Different OSes have different "line separators" to check for: CRLF or LF
String lf = System.lineSeparator();

// --- Setup and Teardown --- //

@BeforeEach
void setUp() {
// Reset our own output stream and redirect stdout into it
outStream.reset();
System.setOut(new PrintStream(outStream));
}

@AfterEach
void tearDown() {
// Redirect stdout back to System stream
System.setOut(System.out);
}

// --- findPairs() Tests --- //

@Test
void findPairsTestInputTooShort() {
// Check for array length < 2 case
Algorithms.findPairs(new int[] {1}, 6);
assertEquals("", outStream.toString());
}

@Test
void findPairs() {
// Check for example case
Algorithms.findPairs(new int[] {2, 4, 5, 1, 3, 5, 4}, 6);
assertEquals("(1, 5)" + lf + "(4, 2)" + lf, outStream.toString());
}

@Test
void findPairsTestNegativesAndZeroes() {
// Check for negatives and zeroes
Algorithms.findPairs(new int[] {-2, 8, 5, 6, 0, -9, 15}, 6);
assertEquals("(8, -2)" + lf + "(0, 6)" + lf + "(15, -9)" + lf, outStream.toString());
}

@Test
void findPairsTestRepeats() {
// Check for repeats
Algorithms.findPairs(new int[] {3, 3, 3, 3, 3, 3}, 6);
assertEquals("(3, 3)" + lf, outStream.toString());
}

@Test
void findPairsTestNoPairs() {
// Check for none found
Algorithms.findPairs(new int[] {1, 0}, 6);
assertEquals("", outStream.toString());
}

// --- isPalindrome() Tests --- //

@Test
void isPalindromeTestExamples() {
assertTrue(Algorithms.isPalindrome("radar")); // example case
assertTrue(Algorithms.isPalindrome("BOb")); // example case + random capitalization
assertTrue(Algorithms.isPalindrome("asdfdsa")); // example case
}

@Test
void isPalindromeTestNonPalindromes() {
assertFalse(Algorithms.isPalindrome("awesome")); // non-palindrome case
assertFalse(Algorithms.isPalindrome("Game of Apps")); // non-palindrome case + random capitalization
}
}
14 changes: 14 additions & 0 deletions Android App/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
*.iml
.gradle
/local.properties
/.idea/caches
/.idea/libraries
/.idea/modules.xml
/.idea/workspace.xml
/.idea/navEditor.xml
/.idea/assetWizardSettings.xml
.DS_Store
/build
/captures
.externalNativeBuild
.cxx
1 change: 1 addition & 0 deletions Android App/app/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/build
35 changes: 35 additions & 0 deletions Android App/app/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
apply plugin: 'com.android.application'

android {
compileSdkVersion 29
buildToolsVersion "29.0.3"

defaultConfig {
applicationId "xyz.jdtec.imageview"
minSdkVersion 28
targetSdkVersion 29
versionCode 1
versionName "1.0"

testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}

buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}

}

dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])

implementation 'androidx.appcompat:appcompat:1.1.0'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test.ext:junit:1.1.1'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
implementation 'androidx.recyclerview:recyclerview:1.0.0'
}
21 changes: 21 additions & 0 deletions Android App/app/proguard-rules.pro
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Add project specific ProGuard rules here.
# You can control the set of applied configuration files using the
# proguardFiles setting in build.gradle.
#
# For more details, see
# http://developer.android.com/guide/developing/tools/proguard.html

# If your project uses WebView with JS, uncomment the following
# and specify the fully qualified class name to the JavaScript interface
# class:
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
# public *;
#}

# Uncomment this to preserve the line number information for
# debugging stack traces.
#-keepattributes SourceFile,LineNumberTable

# If you keep the line number information, uncomment this to
# hide the original source file name.
#-renamesourcefileattribute SourceFile
29 changes: 29 additions & 0 deletions Android App/app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="xyz.jdtec.imageview">

<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">

<!-- Main activity -->
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>

<!-- Image Detail activity — Child of Main activity -->
<activity
android:name=".ImageDetailActivity"
android:label="@string/detail_activity_name"
android:parentActivityName=".MainActivity" />

</application>

</manifest>
63 changes: 63 additions & 0 deletions Android App/app/src/main/java/xyz/jdtec/imageview/Image.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package xyz.jdtec.imageview;

import java.util.ArrayList;

public class Image {
private int id;
private String caption;
private String description;
private String infoUrl;

/**
* Image class constructor
*
* @param id Android resource ID for the image
* @param caption caption text for the image
*/
Image(int id, String caption, String description, String infoUrl) {
this.id = id;
this.caption = caption;
this.description = description;
this.infoUrl = infoUrl;
}

/**
* Method for creating "dummy" images
*
* @return ArrayList of Image objects containing some images and their captions
*/
public static ArrayList<Image> createDummyImages() {
ArrayList<Image> images = new ArrayList<>();

// Returns some images from drawable as well their respective captions
images.add(new Image(R.drawable.sun, "Sun", "The Sun is a main-sequence star in our solar system. As with other stars, the Sun is composed mainly from hydrogen and helium gas.", "https://solarsystem.nasa.gov/solar-system/sun/overview/"));
images.add(new Image(R.drawable.mercury, "Mercury", "Mercury is the 1st planet in our solar system, with an average orbital distance of 0.39 AU away from the Sun. Mercury is mainly composed of rock and metallic elements, and as such is one of the terrestrial planets.", "https://solarsystem.nasa.gov/planets/mercury/overview/"));
images.add(new Image(R.drawable.venus, "Venus", "Venus is the 2nd planet in our solar system, with an average orbital distance of 0.72 AU away from the Sun. Venus is mainly composed of rock and metallic elements, and as such is one of the terrestrial planets.", "https://solarsystem.nasa.gov/planets/venus/overview/"));
images.add(new Image(R.drawable.earth, "Earth", "Earth is the 3rd planet in our solar system, with an average orbital distance of 1 AU away from the Sun. Earth is mainly composed of rock and metallic elements, and as such is one of the terrestrial planets.", "https://solarsystem.nasa.gov/planets/earth/overview/"));
images.add(new Image(R.drawable.mars, "Mars", "Mars is the 4th planet in our solar system, with an average orbital distance of 1.5 AU away from the Sun. Mars is mainly composed of rock and metallic elements, and as such is one of the terrestrial planets.", "https://solarsystem.nasa.gov/planets/mars/overview/"));
images.add(new Image(R.drawable.jupiter, "Jupiter", "Jupiter is the 5th planet in our solar system, with an average orbital distance of 5.2 AU away from the Sun. Jupiter is mainly composed of gaseous elements, and as such is one of the gas giants.", "https://solarsystem.nasa.gov/planets/jupiter/overview/"));
images.add(new Image(R.drawable.saturn, "Saturn", "Saturn is the 6th planet in our solar system, with an average orbital distance of 9.5 AU away from the Sun. Saturn is mainly composed of gaseous elements, and as such is one of the gas giants.", "https://solarsystem.nasa.gov/planets/saturn/overview/"));
images.add(new Image(R.drawable.uranus, "Uranus", "Uranus is the 7th planet in our solar system, with an average orbital distance of 19.2 AU away from the Sun. Uranus is mainly composed of gaseous elements, and as such is one of the gas giants.", "https://solarsystem.nasa.gov/planets/uranus/overview/"));
images.add(new Image(R.drawable.neptune, "Neptune", "Neptune is the 8th planet in our solar system, with an average orbital distance of 30 AU away from the Sun. Neptune is mainly composed of gaseous elements, and as such is one of the gas giants.", "https://solarsystem.nasa.gov/planets/neptune/overview/"));

return images;
}

// --- Getters ---

public int getId() {
return id;
}

public String getCaption() {
return caption;
}

public String getDescription() {
return description;
}

public String getInfoUrl() {
return infoUrl;
}
}
Loading