started with the TicTacToe_MinMax Project. Finished the rendering of the game.
TODO: Make a playable version and add MinMax
This commit is contained in:
0
CodingChallanges/.attach_pid12360
Normal file
0
CodingChallanges/.attach_pid12360
Normal file
7
CodingChallanges/.classpath
Normal file
7
CodingChallanges/.classpath
Normal file
@@ -0,0 +1,7 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<classpath>
|
||||
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
|
||||
<classpathentry kind="src" path="src"/>
|
||||
<classpathentry kind="var" path="MAVEN_REPOSITORY/junit/junit/4.12/junit-4.12.jar"/>
|
||||
<classpathentry kind="output" path="bin"/>
|
||||
</classpath>
|
||||
15
CodingChallanges/.project
Normal file
15
CodingChallanges/.project
Normal file
@@ -0,0 +1,15 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<projectDescription>
|
||||
<name>CodingChallanges</name>
|
||||
<comment/>
|
||||
<projects/>
|
||||
<buildSpec>
|
||||
<buildCommand>
|
||||
<name>org.eclipse.jdt.core.javabuilder</name>
|
||||
<arguments/>
|
||||
</buildCommand>
|
||||
</buildSpec>
|
||||
<natures>
|
||||
<nature>org.eclipse.jdt.core.javanature</nature>
|
||||
</natures>
|
||||
</projectDescription>
|
||||
6
CodingChallanges/CodingChallanges.eml
Normal file
6
CodingChallanges/CodingChallanges.eml
Normal file
@@ -0,0 +1,6 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<component inherit-compiler-output="true" inheritJdk="true">
|
||||
<output-test url="file://$USER_HOME$/Documents/real-java/out/test/CodingChallanges"/>
|
||||
<exclude-output/>
|
||||
<contentEntry url="file://$MODULE_DIR$"/>
|
||||
</component>
|
||||
21
CodingChallanges/CodingChallanges.iml
Normal file
21
CodingChallanges/CodingChallanges.iml
Normal file
@@ -0,0 +1,21 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<module type="JAVA_MODULE" version="4">
|
||||
<component name="NewModuleRootManager" inherit-compiler-output="true">
|
||||
<exclude-output />
|
||||
<content url="file://$MODULE_DIR$">
|
||||
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
|
||||
</content>
|
||||
<orderEntry type="inheritedJdk" />
|
||||
<orderEntry type="sourceFolder" forTests="false" />
|
||||
<orderEntry type="module-library">
|
||||
<library name="JUnit4">
|
||||
<CLASSES>
|
||||
<root url="jar://$MAVEN_REPOSITORY$/junit/junit/4.12/junit-4.12.jar!/" />
|
||||
<root url="jar://$MAVEN_REPOSITORY$/org/hamcrest/hamcrest-core/1.3/hamcrest-core-1.3.jar!/" />
|
||||
</CLASSES>
|
||||
<JAVADOC />
|
||||
<SOURCES />
|
||||
</library>
|
||||
</orderEntry>
|
||||
</component>
|
||||
</module>
|
||||
41
CodingChallanges/CodingChallanges1.iml
Normal file
41
CodingChallanges/CodingChallanges1.iml
Normal file
@@ -0,0 +1,41 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<module type="JAVA_MODULE" version="4">
|
||||
<component name="NewModuleRootManager" inherit-compiler-output="true">
|
||||
<exclude-output />
|
||||
<content url="file://$MODULE_DIR$">
|
||||
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
|
||||
</content>
|
||||
<orderEntry type="inheritedJdk" />
|
||||
<orderEntry type="sourceFolder" forTests="false" />
|
||||
<orderEntry type="module-library">
|
||||
<library name="JUnit4">
|
||||
<CLASSES>
|
||||
<root url="jar://$MAVEN_REPOSITORY$/junit/junit/4.12/junit-4.12.jar!/" />
|
||||
<root url="jar://$MAVEN_REPOSITORY$/org/hamcrest/hamcrest-core/1.3/hamcrest-core-1.3.jar!/" />
|
||||
</CLASSES>
|
||||
<JAVADOC />
|
||||
<SOURCES />
|
||||
</library>
|
||||
</orderEntry>
|
||||
<orderEntry type="module-library">
|
||||
<library name="JUnit4">
|
||||
<CLASSES>
|
||||
<root url="jar://$MAVEN_REPOSITORY$/junit/junit/4.12/junit-4.12.jar!/" />
|
||||
<root url="jar://$MAVEN_REPOSITORY$/org/hamcrest/hamcrest-core/1.3/hamcrest-core-1.3.jar!/" />
|
||||
</CLASSES>
|
||||
<JAVADOC />
|
||||
<SOURCES />
|
||||
</library>
|
||||
</orderEntry>
|
||||
<orderEntry type="module-library">
|
||||
<library name="JUnit4">
|
||||
<CLASSES>
|
||||
<root url="jar://$MAVEN_REPOSITORY$/junit/junit/4.12/junit-4.12.jar!/" />
|
||||
<root url="jar://$MAVEN_REPOSITORY$/org/hamcrest/hamcrest-core/1.3/hamcrest-core-1.3.jar!/" />
|
||||
</CLASSES>
|
||||
<JAVADOC />
|
||||
<SOURCES />
|
||||
</library>
|
||||
</orderEntry>
|
||||
</component>
|
||||
</module>
|
||||
11
CodingChallanges/links.iml
Normal file
11
CodingChallanges/links.iml
Normal file
@@ -0,0 +1,11 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<module type="JAVA_MODULE" version="4">
|
||||
<component name="NewModuleRootManager" inherit-compiler-output="true">
|
||||
<exclude-output />
|
||||
<content url="file://$MODULE_DIR$">
|
||||
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
|
||||
</content>
|
||||
<orderEntry type="inheritedJdk" />
|
||||
<orderEntry type="sourceFolder" forTests="false" />
|
||||
</component>
|
||||
</module>
|
||||
51
CodingChallanges/src/arrays/PoppingBlocks.java
Normal file
51
CodingChallanges/src/arrays/PoppingBlocks.java
Normal file
@@ -0,0 +1,51 @@
|
||||
package arrays;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
public class PoppingBlocks {
|
||||
|
||||
//Edabit-Challange
|
||||
//https://edabit.com/challenge/p38KD5Far2g72fv3d
|
||||
|
||||
public static char[] finalResult(char[] letters) {
|
||||
int firstIndex = -1;
|
||||
int lastIndex = -1;
|
||||
char[] newLetters = new char[0];
|
||||
for (int i = 0; i < letters.length - 1; i++) {
|
||||
if (letters[i] == letters[i + 1]) {
|
||||
firstIndex = i;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (firstIndex != -1) {
|
||||
for (int j = firstIndex; j < letters.length; j++) {
|
||||
if (letters[j] != letters[firstIndex]) {
|
||||
lastIndex = j - 1;
|
||||
break;
|
||||
}
|
||||
if (j == letters.length - 1) {
|
||||
lastIndex = j;
|
||||
break;
|
||||
}
|
||||
}
|
||||
newLetters = new char[letters.length - (lastIndex - firstIndex) - 1];
|
||||
int j = 0;
|
||||
for (int i = 0; i < letters.length; i++) {
|
||||
if (i < firstIndex || i > lastIndex) {
|
||||
newLetters[j] = letters[i];
|
||||
j++;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (firstIndex == -1) {
|
||||
return letters;
|
||||
}
|
||||
return finalResult(newLetters);
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
System.out.println(finalResult(new char[]{'B', 'B', 'C', 'C', 'A', 'A', 'A'}));
|
||||
}
|
||||
|
||||
}
|
||||
19
CodingChallanges/src/geometry/PentagonalNumber.java
Normal file
19
CodingChallanges/src/geometry/PentagonalNumber.java
Normal file
@@ -0,0 +1,19 @@
|
||||
package geometry;
|
||||
|
||||
public class PentagonalNumber {
|
||||
|
||||
//Edabit-Challange
|
||||
//https://edabit.com/challenge/H6eTNH6NW36MHqkjb
|
||||
|
||||
public static int pentagonal(int num) {
|
||||
int outerRings = 0;
|
||||
for(int i = num-1; i > 0; i--){
|
||||
outerRings += i * 5;
|
||||
}
|
||||
return outerRings + 1;
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
System.out.println(pentagonal(10));
|
||||
}
|
||||
}
|
||||
26
CodingChallanges/src/geometry/makeGrid.java
Normal file
26
CodingChallanges/src/geometry/makeGrid.java
Normal file
@@ -0,0 +1,26 @@
|
||||
package geometry;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
public class makeGrid {
|
||||
|
||||
//Edabit-Challange
|
||||
//https://edabit.com/challenge/7Tb7qMDQHtz3xpydd
|
||||
|
||||
public static int[][] squarePatch( int n ){
|
||||
int[][] array = new int[n][n];
|
||||
int i = 0;
|
||||
while (i < n){
|
||||
for (int j = 0; j < n; j++){
|
||||
array[i][j] = n;
|
||||
}
|
||||
i++;
|
||||
}
|
||||
return array;
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
System.out.println(Arrays.toString(squarePatch(4)));
|
||||
}
|
||||
|
||||
}
|
||||
48
CodingChallanges/src/numbers/CarryingTheDigits.java
Normal file
48
CodingChallanges/src/numbers/CarryingTheDigits.java
Normal file
@@ -0,0 +1,48 @@
|
||||
package numbers;
|
||||
|
||||
public class CarryingTheDigits {
|
||||
|
||||
//Edabit-Challange:
|
||||
//https://edabit.com/challenge/5snfPLPbvjAsZ5kjo
|
||||
|
||||
public static int carryDigits(int n1, int n2){
|
||||
String firstNum = String.valueOf(n1);
|
||||
String secondNum = String.valueOf(n2);
|
||||
String first = new String();
|
||||
String second = new String();
|
||||
if (firstNum.length() > secondNum.length()){
|
||||
String filler = new String();
|
||||
for (int i = 0; i < firstNum.length()-secondNum.length();i++){
|
||||
filler += "0";
|
||||
}
|
||||
first = firstNum;
|
||||
second = filler + secondNum;
|
||||
} else {
|
||||
String filler = new String();
|
||||
for (int i = 0; i < secondNum.length()-firstNum.length();i++){
|
||||
filler += "0";
|
||||
}
|
||||
first = filler + firstNum;
|
||||
second = secondNum;
|
||||
}
|
||||
System.out.println(first+ ", "+ second);
|
||||
int tempCarry = 0;
|
||||
int carry = 0;
|
||||
for (int i = first.length()-1; i >= 0; i--){
|
||||
int num = Integer.parseInt(Character.toString(first.charAt(i))) + Integer.parseInt(Character.toString(second.charAt(i))) + tempCarry;
|
||||
if (num > 9){
|
||||
tempCarry = num - 9;
|
||||
if (tempCarry <= 0) {
|
||||
tempCarry = 0;
|
||||
}
|
||||
carry++;
|
||||
}
|
||||
}
|
||||
return carry;
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
System.out.println( carryDigits(671, 329));
|
||||
}
|
||||
|
||||
}
|
||||
21
CodingChallanges/src/numbers/FindPrimorial.java
Normal file
21
CodingChallanges/src/numbers/FindPrimorial.java
Normal file
@@ -0,0 +1,21 @@
|
||||
package numbers;
|
||||
|
||||
public class FindPrimorial {
|
||||
|
||||
//Edabit-Link
|
||||
//https://edabit.com/challenge/Tyzp6S67dtXPAAQan
|
||||
static int[] primes = {2, 3, 5, 7, 11, 13, 17, 19};
|
||||
public static int primorial(int n) {
|
||||
if (n > 0) {
|
||||
int primorial = primes[n-1] * primorial(n - 1);
|
||||
return primorial;
|
||||
} else {
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
System.out.println(primorial(1));
|
||||
}
|
||||
|
||||
}
|
||||
27
CodingChallanges/src/numbers/HowManyPrimes_NOT_SOLVED.java
Normal file
27
CodingChallanges/src/numbers/HowManyPrimes_NOT_SOLVED.java
Normal file
@@ -0,0 +1,27 @@
|
||||
package numbers;
|
||||
|
||||
public class HowManyPrimes_NOT_SOLVED {
|
||||
|
||||
//Edabit link
|
||||
//https://edabit.com/challenge/z8vvSdWjAPu5ufBuR
|
||||
|
||||
public static int findPrimes(int boundary){
|
||||
int primes = 0;
|
||||
for (int i = 0; i < boundary; i++){
|
||||
if (i % 2 != 0 && i!= 0){
|
||||
for (int j = 1; j < 10; j = j+2){
|
||||
if (i/j == 1){
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return primes;
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
int boundary = 100;
|
||||
System.out.printf("There are %d primes within the range of 0-%d",findPrimes(boundary),boundary);
|
||||
}
|
||||
|
||||
}
|
||||
27
CodingChallanges/src/numbers/MaxFirstNum.java
Normal file
27
CodingChallanges/src/numbers/MaxFirstNum.java
Normal file
@@ -0,0 +1,27 @@
|
||||
package numbers;
|
||||
|
||||
public class MaxFirstNum {
|
||||
|
||||
public static int maxPossible(int n1, int n2) {
|
||||
String firstNum = n1 + "";
|
||||
String secondNum = n2 + "";
|
||||
String maxNum = null;
|
||||
for (int i = 0; i < firstNum.length(); i++){
|
||||
|
||||
int num1 = Integer.parseInt(Character.toString(firstNum.charAt(i)));
|
||||
boolean found = false;
|
||||
|
||||
for (int j = 0; j < secondNum.length(); j++){
|
||||
if (num1 < Integer.parseInt(Character.toString(secondNum.charAt(i)))){
|
||||
firstNum.replace(Character.toString(firstNum.charAt(i)), Character.toString(secondNum.charAt(j)));
|
||||
secondNum.replace(Character.toString(secondNum.charAt(j)),"");
|
||||
}
|
||||
}
|
||||
}
|
||||
return Integer.parseInt(firstNum);
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
System.out.println(maxPossible(9132, 5564));
|
||||
}
|
||||
}
|
||||
29
CodingChallanges/src/numbers/SimplifyFractions.java
Normal file
29
CodingChallanges/src/numbers/SimplifyFractions.java
Normal file
@@ -0,0 +1,29 @@
|
||||
package numbers;
|
||||
|
||||
public class SimplifyFractions {
|
||||
|
||||
//Edabit-Challange
|
||||
//https://edabit.com/challenge/bmwpoeCybNWnBxn7M
|
||||
|
||||
public static String simplify(String str) {
|
||||
String[] numbers = str.split("/");
|
||||
int firstNum = Integer.parseInt(numbers[0]);
|
||||
int secondNum = Integer.parseInt(numbers[1]);
|
||||
int lowNum = Math.min(firstNum, secondNum);
|
||||
for (int i = lowNum; i > 0; i--){
|
||||
if (firstNum % i == 0 && secondNum % i == 0){
|
||||
firstNum = firstNum/i;
|
||||
secondNum = secondNum/i;
|
||||
}
|
||||
}
|
||||
if (firstNum % secondNum == 0){
|
||||
return firstNum/secondNum+"";
|
||||
} else{
|
||||
return firstNum+"/"+secondNum;
|
||||
}
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
System.out.println(simplify("100/400"));
|
||||
}
|
||||
}
|
||||
38
CodingChallanges/src/numbers/ValidateCreditCard.java
Normal file
38
CodingChallanges/src/numbers/ValidateCreditCard.java
Normal file
@@ -0,0 +1,38 @@
|
||||
package numbers;
|
||||
|
||||
// Link: https://edabit.com/challenge/RvxGLMzoPoBgXhcaC
|
||||
|
||||
public class ValidateCreditCard {
|
||||
|
||||
public static boolean validateCard(long num) {
|
||||
|
||||
if ((num+"").length()<14 || (num+"".length()<20)){
|
||||
return false;
|
||||
}
|
||||
|
||||
int checkDigit = Integer.parseInt((num+"").substring((num+"").length()-1));
|
||||
|
||||
int[] numInArray = new int [(num+"").length()-1];
|
||||
|
||||
for (int i = 1; i < numInArray.length+1; i++){
|
||||
numInArray[i-1] = Integer.parseInt((num+"").substring((num+"").length()-i-1, (num+"").length()-i));
|
||||
if (i % 2 == 1){
|
||||
numInArray[i-1] *= 2;
|
||||
if (numInArray[i-1]>9){
|
||||
numInArray[i-1] = (numInArray[i-1]/10) + (numInArray[i-1]-10);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int sum = 0;
|
||||
for (int i = 0; i < numInArray.length; i++){
|
||||
sum += numInArray[i];
|
||||
}
|
||||
return 10 - (sum-((sum/10)*10)) == checkDigit;
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
System.out.println(validateCard(1234567890123456L));
|
||||
}
|
||||
|
||||
}
|
||||
26
CodingChallanges/src/strings/CharacterMapping.java
Normal file
26
CodingChallanges/src/strings/CharacterMapping.java
Normal file
@@ -0,0 +1,26 @@
|
||||
package strings;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
public class CharacterMapping {
|
||||
|
||||
//Edabit-Challange
|
||||
//https://edabit.com/challenge/zcobRojF5p8DWkfiC
|
||||
|
||||
public static int[] characterMapping(String str) {
|
||||
int[] mappedArr = new int[str.length()];
|
||||
String chars = new String();
|
||||
for (int i = 0; i < str.length(); i++){
|
||||
if (chars.indexOf(str.charAt(i)) == -1){
|
||||
chars += str.charAt(i);
|
||||
}
|
||||
mappedArr[i] = chars.indexOf(str.charAt(i));
|
||||
}
|
||||
return mappedArr;
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
System.out.println(Arrays.toString(characterMapping("hmmmmm")));
|
||||
}
|
||||
|
||||
}
|
||||
54
CodingChallanges/src/strings/DistanceToNearestVowel.java
Normal file
54
CodingChallanges/src/strings/DistanceToNearestVowel.java
Normal file
@@ -0,0 +1,54 @@
|
||||
package strings;
|
||||
|
||||
import java.util.LinkedList;
|
||||
|
||||
public class DistanceToNearestVowel {
|
||||
|
||||
//Edabit-Challange
|
||||
//https://edabit.com/challenge/Ls6pbLkC9wfYuaDgm
|
||||
|
||||
public static int[] distanceToNearestVowel(String str) {
|
||||
LinkedList<Character> vowels= new LinkedList<Character>();
|
||||
char[] vowelsArr = new char[]{'a', 'e', 'i', 'o', 'u'};
|
||||
for (int i = 0; i < vowelsArr.length; i++) {
|
||||
vowels.add(vowelsArr[i]);
|
||||
}
|
||||
LinkedList<Integer> values = new LinkedList<>();
|
||||
for (int i = 0; i < str.length();i++){
|
||||
if (vowels.contains(str.charAt(i))){
|
||||
values.addLast(1);
|
||||
} else{
|
||||
values.addLast(0);
|
||||
}
|
||||
}
|
||||
LinkedList<Integer> indexesVowels = new LinkedList<>();
|
||||
for (int i = 0 ; i < values.size(); i++){
|
||||
if (values.get(i) == 1){
|
||||
indexesVowels.addLast(i);
|
||||
}
|
||||
}
|
||||
int[] distances = new int[values.size()];
|
||||
for (int i = 0; i < distances.length; i++){
|
||||
distances[i] = 1000;
|
||||
}
|
||||
for (int i = 0; i < values.size(); i++){
|
||||
if (values.get(i) == 1){
|
||||
distances[i] = 0;
|
||||
}
|
||||
}
|
||||
for (int i = 0; i < indexesVowels.size(); i++){
|
||||
for (int j = 0; j < distances.length; j++){
|
||||
if (distances[j] != 0){
|
||||
if (distances[j] > Math.abs(j - indexesVowels.get(i))){
|
||||
distances[j] = Math.abs(j - indexesVowels.get(i));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return distances;
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
distanceToNearestVowel("shopper");
|
||||
}
|
||||
}
|
||||
33
CodingChallanges/src/strings/FilterStringArray.java
Normal file
33
CodingChallanges/src/strings/FilterStringArray.java
Normal file
@@ -0,0 +1,33 @@
|
||||
package strings;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Stack;
|
||||
|
||||
public class FilterStringArray {
|
||||
|
||||
//Edabit-Link
|
||||
//https://edabit.com/challenge/vNszi3mbJA5BhgGFX
|
||||
|
||||
public static String[] filterArray(String[] str) {
|
||||
Stack<String> stack = new Stack<>();
|
||||
int stackSize = 0;
|
||||
for (int i = 0; i < str.length; i++) {
|
||||
try {
|
||||
int possibleNum = Integer.parseInt(str[i]);
|
||||
if (stack.search(possibleNum) < 0) {
|
||||
stack.push(str[i]);
|
||||
stackSize++;
|
||||
}
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
String[] strArray = new String[stackSize];
|
||||
return stack.toArray(strArray);
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
System.out.println(Arrays.toString(filterArray(new String[]{"1", "2", "aasf", "1", "123", "123"})));
|
||||
}
|
||||
|
||||
}
|
||||
64
CodingChallanges/src/strings/FiscalCode.java
Normal file
64
CodingChallanges/src/strings/FiscalCode.java
Normal file
@@ -0,0 +1,64 @@
|
||||
package strings;
|
||||
|
||||
//Link: https://edabit.com/challenge/FToTLuiB7bmFngkjr
|
||||
|
||||
public class FiscalCode {
|
||||
|
||||
static String[] months = {"A", "B", "C", "D", "E", "H", "L", "M", "P", "R", "S", "T"};
|
||||
static String vowels = "AEIOU";
|
||||
|
||||
public static String fiscalCode(String name, String surName, String gender, String dob) {
|
||||
String returnString = "";
|
||||
returnString += generateLetters(surName, 's');
|
||||
returnString += generateLetters(name, 'n');
|
||||
returnString += generateNumbers(dob, gender);
|
||||
return returnString;
|
||||
}
|
||||
|
||||
private static String generateLetters(String string, char mode) {
|
||||
String returnString = "";
|
||||
string = string.toUpperCase();
|
||||
for (int i = 0; i < string.length(); i++) {
|
||||
if (vowels.indexOf(string.charAt(i)) == -1) {
|
||||
returnString += string.charAt(i);
|
||||
}
|
||||
}
|
||||
|
||||
if (returnString.length() == 3) {
|
||||
return returnString;
|
||||
} else if (returnString.length() > 3) {
|
||||
if (mode != 'n' && mode != 's') {
|
||||
System.err.println("Not a valid value");
|
||||
System.exit(0);
|
||||
}
|
||||
return mode == 'n' ? "" + returnString.charAt(0) + returnString.charAt(2) + returnString.charAt(3) : returnString.substring(0, 3);
|
||||
} else {
|
||||
int index = 0;
|
||||
while (returnString.length() < 3 && string.length() > index) {
|
||||
if (vowels.indexOf(string.charAt(index)) > -1) {
|
||||
returnString += string.charAt(index);
|
||||
}
|
||||
index++;
|
||||
}
|
||||
return returnString.length() == 3 ? returnString : returnString + "X";
|
||||
}
|
||||
}
|
||||
|
||||
private static String generateNumbers(String dob, String gender) {
|
||||
String returnString = "";
|
||||
String date = dob.substring(dob.length() - 2);
|
||||
int month = Integer.parseInt(dob.substring(dob.indexOf("/") + 1, dob.lastIndexOf("/")))-1;
|
||||
if (gender.equals("M")) {
|
||||
return date + months[month] + (dob.substring(0, dob.indexOf("/")).length() == 2 ? dob.substring(0, dob.indexOf("/")) :
|
||||
0 + dob.substring(0, dob.indexOf("/")));
|
||||
} else {
|
||||
int day = Integer.parseInt(dob.substring(0, dob.indexOf("/")))+40;
|
||||
return date + months[month] + day;
|
||||
}
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
System.out.println(fiscalCode("Mickey", "Mouse", "M", "6/1/1928"));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
package strings;
|
||||
|
||||
import java.util.LinkedList;
|
||||
|
||||
public class LongestAlternatingSubstring {
|
||||
|
||||
//Edabit-Challange
|
||||
//https://edabit.com/challenge/De5e8kdsCFmKvc6w8
|
||||
|
||||
public static String longestSubstring(String digits) {
|
||||
char[] digitsArr = digits.toCharArray();
|
||||
LinkedList<Integer> indexes = new LinkedList<>();
|
||||
indexes.addLast(0);
|
||||
for (int i = 0; i < digitsArr.length - 1; i++) {
|
||||
if ((digitsArr[i] % 2 == 0 && digitsArr[i + 1] % 2 == 0) || (digitsArr[i] % 2 != 0 && digitsArr[i + 1] % 2 != 0)) {
|
||||
indexes.addLast(i+1 );
|
||||
}
|
||||
}
|
||||
indexes.addLast(digits.length());
|
||||
LinkedList<String> substrings = new LinkedList<String>();
|
||||
LinkedList<Integer> indexDump = (LinkedList<Integer>) indexes.clone();
|
||||
for (int i = 0; i < indexes.size()-1; i++) {
|
||||
if (indexDump.get(i+1)-indexDump.get(i)>1) {
|
||||
String sub = digits.substring(indexDump.get(i), indexDump.get(i + 1));
|
||||
substrings.addLast(sub);
|
||||
}
|
||||
}
|
||||
String longest = new String();
|
||||
for (int i = 0;i < substrings.size();i++){
|
||||
if (substrings.get(i).length() > longest.length()){
|
||||
longest = substrings.get(i);
|
||||
}
|
||||
}
|
||||
return "lll";
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
longestSubstring("594127169973391692147228678476");
|
||||
}
|
||||
}
|
||||
23
CodingChallanges/src/strings/SmoothSentences.java
Normal file
23
CodingChallanges/src/strings/SmoothSentences.java
Normal file
@@ -0,0 +1,23 @@
|
||||
package strings;
|
||||
|
||||
public class SmoothSentences {
|
||||
|
||||
//Edabit-Challange
|
||||
//https://edabit.com/challenge/FtoNXq7DTCAiZ3z9T
|
||||
|
||||
public static boolean isSmooth(String sentence) {
|
||||
String[] words = sentence.toLowerCase().split("\\s+");
|
||||
for (int i = 0; i < words.length-1; i++){
|
||||
char lastChar = words[i].charAt(words[i].length()-1);
|
||||
char firstChar = words[i+1].charAt(0);
|
||||
if (lastChar != firstChar){
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
System.out.println(isSmooth("Marigold daffodils streaming happily."));
|
||||
}
|
||||
}
|
||||
43
CodingChallanges/src/strings/WordChain.java
Normal file
43
CodingChallanges/src/strings/WordChain.java
Normal file
@@ -0,0 +1,43 @@
|
||||
package strings;
|
||||
|
||||
public class WordChain {
|
||||
|
||||
String link = "https://edabit.com/challenge/hbHJdMv4HubYs5TRv";
|
||||
|
||||
public static boolean isWordChain(String[] words){
|
||||
String firstWord, secondWord;
|
||||
for (int i = 0; i < words.length-1; i++){
|
||||
int differences = 0;
|
||||
firstWord = words[i];
|
||||
secondWord = words[i+1];
|
||||
|
||||
if (firstWord.length() == secondWord.length()){
|
||||
for (int j = 0; j < firstWord.length(); j++){
|
||||
if (firstWord.charAt(j) != secondWord.charAt(j)){
|
||||
differences++;
|
||||
}
|
||||
}
|
||||
} else if(firstWord.length() > secondWord.length()){
|
||||
differences = firstWord.length()-secondWord.length();
|
||||
for (int j = 0; j < secondWord.length(); j++){
|
||||
if (firstWord.charAt(j) != secondWord.charAt(j)){
|
||||
differences++;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
differences = secondWord.length()-firstWord.length();
|
||||
for (int j = 0; j < firstWord.length(); j++){
|
||||
if (firstWord.charAt(j) != secondWord.charAt(j)){
|
||||
differences++;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (differences > 1){
|
||||
break;
|
||||
}
|
||||
differences = 0;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
}
|
||||
35
CodingChallanges/src/tests/FiscalCodeTest.java
Normal file
35
CodingChallanges/src/tests/FiscalCodeTest.java
Normal file
@@ -0,0 +1,35 @@
|
||||
package tests;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import org.junit.Test;
|
||||
import strings.FiscalCode;
|
||||
|
||||
/**
|
||||
* @author edwardclark
|
||||
**/
|
||||
|
||||
public class FiscalCodeTest {
|
||||
@Test
|
||||
public void test1() throws Exception {
|
||||
assertEquals("CHEBND61T01", FiscalCode.fiscalCode("Brendan", "Eich", "M", "1/12/1961"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test2() throws Exception {
|
||||
assertEquals("YUXHLN50T41", FiscalCode.fiscalCode("Helen", "Yu", "F", "1/12/1950"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test3() throws Exception {
|
||||
assertEquals("CPNLAX99A17", FiscalCode.fiscalCode("Al", "Capone", "M", "17/1/1899"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test4() throws Exception {
|
||||
assertEquals("MSOMKY28A16", FiscalCode.fiscalCode("Mickey", "Mouse", "M", "16/1/1928"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test5() throws Exception {
|
||||
assertEquals("CRUMRA67S47", FiscalCode.fiscalCode("Marie", "Curie", "F", "7/11/1867"));
|
||||
}
|
||||
}
|
||||
34
CodingChallanges/src/tests/PigLatinTranslationTest.java
Normal file
34
CodingChallanges/src/tests/PigLatinTranslationTest.java
Normal file
@@ -0,0 +1,34 @@
|
||||
package tests;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
import translation_encryption.PigLatinTranslation;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
public class PigLatinTranslationTest {
|
||||
@Test
|
||||
public void test1() {
|
||||
Assert.assertEquals("Atscay areway reatgay etspay.", PigLatinTranslation.pigLatin("Cats are great pets."));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test2() {
|
||||
assertEquals("Omtay otgay away mallsay iecepay ofway iepay.", PigLatinTranslation.pigLatin("Tom got a small piece of pie."));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test3() {
|
||||
assertEquals("Ehay oldtay usway away eryvay excitingway aletay.", PigLatinTranslation.pigLatin("He told us a very exciting tale."));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test4() {
|
||||
assertEquals("Away iamondday isway otnay enoughway.", PigLatinTranslation.pigLatin("A diamond is not enough."));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test5() {
|
||||
assertEquals("Urryhay!", PigLatinTranslation.pigLatin("Hurry!"));
|
||||
}
|
||||
}
|
||||
53
CodingChallanges/src/tests/ValidateCreditCardTest.java
Normal file
53
CodingChallanges/src/tests/ValidateCreditCardTest.java
Normal file
@@ -0,0 +1,53 @@
|
||||
package tests;
|
||||
|
||||
import org.junit.Test;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import numbers.ValidateCreditCard;
|
||||
|
||||
public class ValidateCreditCardTest {
|
||||
@Test
|
||||
public void test1() {
|
||||
assertEquals(false, ValidateCreditCard.validateCard(79927398714L));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test2() {
|
||||
System.out.println("Passes Luhn test, but too short.");
|
||||
assertEquals(false, ValidateCreditCard.validateCard(79927398713L));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test3() {
|
||||
assertEquals(true, ValidateCreditCard.validateCard(709092739800713L));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test4() {
|
||||
assertEquals(false, ValidateCreditCard.validateCard(1234567890123456L));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test5() {
|
||||
assertEquals(true, ValidateCreditCard.validateCard(12345678901237L));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test6() {
|
||||
assertEquals(true, ValidateCreditCard.validateCard(5496683867445267L));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test7() {
|
||||
assertEquals(false, ValidateCreditCard.validateCard(4508793361140566L));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test8() {
|
||||
assertEquals(true, ValidateCreditCard.validateCard(376785877526048L));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test9() {
|
||||
assertEquals(false, ValidateCreditCard.validateCard(36717601781975L));
|
||||
}
|
||||
}
|
||||
51
CodingChallanges/src/tests/WordChainTest.java
Normal file
51
CodingChallanges/src/tests/WordChainTest.java
Normal file
@@ -0,0 +1,51 @@
|
||||
package tests;
|
||||
import org.junit.Test;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import strings.WordChain;
|
||||
|
||||
public class WordChainTest {
|
||||
@Test
|
||||
public void test1() {
|
||||
assertEquals(true, WordChain.isWordChain(new String[]{"row", "crow", "crown", "brown", "brawn"}));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test2() {
|
||||
assertEquals(true, WordChain.isWordChain(new String[]{"flew", "flaw", "flan", "flat", "fat", "rat", "rot", "tot"}));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test3() {
|
||||
assertEquals(false, WordChain.isWordChain(new String[]{"meek", "meet", "meat", "teal"}));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test4() {
|
||||
assertEquals(false, WordChain.isWordChain(new String[]{"run", "runny", "bunny"}));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test5() {
|
||||
assertEquals(true, WordChain.isWordChain(new String[]{"fun", "fund", "find", "bind", "wind", "wild", "wile", "wiles"}));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test6() {
|
||||
assertEquals(true, WordChain.isWordChain(new String[]{"nut", "but", "bot", "boot", "loot", "look", "book", "brook"}));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test7() {
|
||||
assertEquals(true, WordChain.isWordChain(new String[]{"some", "tome", "tame", "lame", "flame", "flamer", "blamer", "blamers"}));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test8() {
|
||||
assertEquals(false, WordChain.isWordChain(new String[]{"a", "at", "hat", "that", "what", "flat"}));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test9() {
|
||||
assertEquals(false, WordChain.isWordChain(new String[]{"link", "blink", "wink", "sink"}));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,69 @@
|
||||
package translation_encryption;
|
||||
|
||||
public class EdabitEncription {
|
||||
|
||||
//Edabit Link
|
||||
//https://edabit.com/challenge/jfCsugyp9BSLYEtwb
|
||||
|
||||
public EdabitEncription(){ }
|
||||
|
||||
public String encrypt(String clearText){
|
||||
String editedString = new String();
|
||||
editedString = clearText.replaceAll("\\s","");
|
||||
int length = editedString.length();
|
||||
double sqrtOfLength = Math.sqrt(length);
|
||||
int columns = (int) sqrtOfLength;
|
||||
int rows = columns + 1;
|
||||
|
||||
char[][] wordtable = new char[columns][rows];
|
||||
|
||||
fillWordtable(wordtable, editedString);
|
||||
|
||||
String word = new String();
|
||||
String encrypted = new String();
|
||||
for (int i = 0; i < wordtable[0].length; i++){
|
||||
for (int j = 0; j < wordtable.length; j++){
|
||||
word += wordtable[j][i];
|
||||
}
|
||||
encrypted += word + " ";
|
||||
word = "";
|
||||
}
|
||||
|
||||
return encrypted;
|
||||
}
|
||||
|
||||
private void fillWordtable(char[][] wordtable, String text) {
|
||||
int position = 0;
|
||||
for (int i = 0; i < wordtable.length; i++){
|
||||
for (int j = 0; j < wordtable[0].length; j++){
|
||||
if (position < text.length()) {
|
||||
wordtable[i][j] = text.charAt(position);
|
||||
position++;
|
||||
}
|
||||
else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public String decrypt(String cryptedText){
|
||||
String decrypted = new String();
|
||||
decrypted = "LOL";
|
||||
return decrypted;
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
EdabitEncription encryption = new EdabitEncription();
|
||||
String clearText = "Info ist ein wenig langweilig";
|
||||
String encrypted = encryption.encrypt(clearText);
|
||||
System.out.println(encrypted);
|
||||
String decrypted = encryption.decrypt(encrypted);
|
||||
if (decrypted.equals(clearText)){
|
||||
System.out.println("The Encryption was successful!");
|
||||
} else {
|
||||
System.out.println("You made a mistake.");
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,57 @@
|
||||
package translation_encryption;
|
||||
|
||||
public class KaracasEncryption {
|
||||
|
||||
//Edabit-Challange
|
||||
//https://edabit.com/challenge/SmL32HnRnmsCqGC5g
|
||||
|
||||
public KaracasEncryption(){
|
||||
|
||||
}
|
||||
|
||||
public String replaceVowles(String reversedText){
|
||||
String tempText = new String();
|
||||
for (int i = 0; i < reversedText.length(); i++){
|
||||
char c = reversedText.charAt(i);
|
||||
switch (c){
|
||||
case 'a':
|
||||
tempText += 0;
|
||||
break;
|
||||
case 'e':
|
||||
tempText+= 1;
|
||||
break;
|
||||
case 'o':
|
||||
tempText += 2;
|
||||
break;
|
||||
case 'u':
|
||||
tempText += 3;
|
||||
break;
|
||||
default:
|
||||
tempText += reversedText.charAt(i);
|
||||
break;
|
||||
}
|
||||
}
|
||||
tempText += "aca";
|
||||
return tempText;
|
||||
}
|
||||
|
||||
public String reverseString(String clearText){
|
||||
String tempText = new String();
|
||||
for (int i = clearText.length()-1; i >= 0; i--){
|
||||
tempText += clearText.charAt(i);
|
||||
}
|
||||
return tempText;
|
||||
}
|
||||
|
||||
public String encrypt(String clearText){
|
||||
String reversed = reverseString(clearText);
|
||||
String fullencrypted = replaceVowles(reversed);
|
||||
return fullencrypted;
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
KaracasEncryption karacas = new KaracasEncryption();
|
||||
karacas.encrypt("alpaca");
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,50 @@
|
||||
package translation_encryption;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
public class PigLatinTranslation {
|
||||
|
||||
//Edabit-Challange
|
||||
//https://edabit.com/challenge/2aajqTB69y7ZSSo9v
|
||||
|
||||
public static String pigLatin(String str) {
|
||||
String[] words = str.split("\\s|(?=[!\\.?])");
|
||||
String returnString = "";
|
||||
for (int i = 0; i < words.length-1; i++) {
|
||||
String s = "";
|
||||
|
||||
if ("AEIOUaeiou".indexOf(words[i].charAt(0)) == -1) {
|
||||
if (i == 0) {
|
||||
if (words[i].length() > 1) {
|
||||
s = words[i].toUpperCase().charAt(1) + words[i].substring(2) + words[i].toLowerCase().charAt(0);
|
||||
} else {
|
||||
s = words[i].toUpperCase();
|
||||
}
|
||||
} else {
|
||||
if (words[i].length() > 2) {
|
||||
s = words[i].substring(1) + words[i].toLowerCase().charAt(0);
|
||||
} else if (words[i].length() == 2) {
|
||||
s = words[i].toLowerCase().charAt(1) + String.valueOf(words[i].toLowerCase().charAt(0));
|
||||
} else {
|
||||
s = words[i].toLowerCase();
|
||||
}
|
||||
}
|
||||
s += "ay";
|
||||
} else {
|
||||
s += words[i] + "way";
|
||||
}
|
||||
|
||||
if (i < words.length-2) {
|
||||
returnString += s + " ";
|
||||
} else{
|
||||
returnString += s + words[i+1];
|
||||
}
|
||||
}
|
||||
return returnString;
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
System.out.println(pigLatin("Tom got a small piece of pie."));
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user