From b3cd8681647260b7c0be21445b5a4669158cd862 Mon Sep 17 00:00:00 2001 From: Fabio Scotto di Santolo Date: Mon, 8 Jun 2020 20:18:35 +0200 Subject: [PATCH] Test stringhe ed array --- pom.xml | 34 +++++++++ .../interview/demo/InterviewQuestionsC1.java | 71 +++++++++++++++++++ .../demo/InterviewQuestionsC1Test.java | 47 ++++++++++++ 3 files changed, 152 insertions(+) create mode 100644 pom.xml create mode 100644 src/main/java/org/fscotto/interview/demo/InterviewQuestionsC1.java create mode 100644 src/test/java/org/fscotto/interview/demo/InterviewQuestionsC1Test.java diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..7b60e62 --- /dev/null +++ b/pom.xml @@ -0,0 +1,34 @@ + + + 4.0.0 + + org.fscotto + interviewdemo + 1.0-SNAPSHOT + + + + org.junit.jupiter + junit-jupiter-api + 5.6.1 + test + + + + + + + org.apache.maven.plugins + maven-compiler-plugin + 3.8.1 + + 14 + 14 + + + + + + diff --git a/src/main/java/org/fscotto/interview/demo/InterviewQuestionsC1.java b/src/main/java/org/fscotto/interview/demo/InterviewQuestionsC1.java new file mode 100644 index 0000000..512293e --- /dev/null +++ b/src/main/java/org/fscotto/interview/demo/InterviewQuestionsC1.java @@ -0,0 +1,71 @@ +package org.fscotto.interview.demo; + +import java.util.*; + +public class InterviewQuestionsC1 { + + public static boolean isUniqueChars(final String str) { + if (str == null || "".equals(str)) + return false; + + if (str.length() > 128) + return false; + + final boolean[] charSet = new boolean[128]; + for (int i = 0; i < str.length(); ++i) { + int val = str.charAt(i); + if (charSet[val]) + return false; + charSet[val] = true; + } + return true; + } + + public static boolean permutation(final String s, final String t) { + if (s == null || t == null || s.length() != t.length()) + return false; + + final int[] letters = new int[128]; + final char[] sArray = s.toCharArray(); + for (final char c : sArray) + letters[c]++; + + for (int i = 0; i < t.length(); i++) { + final int c = t.charAt(i); + letters[c]--; + if (letters[c] < 0) + return false; + } + return true; + } + + public static void replaceSpaces(char[] str, int trueLength) { + int spaceCount = 0; + for (int i = 0; i < trueLength; i++) { + if (str[i] == ' ') { + spaceCount++; + } + } + + int index = trueLength + spaceCount * 2; + if (trueLength < str.length) + str[trueLength] = '\0'; + for (int i = trueLength - 1; i >= 0; --i) { + if (str[i] == ' ') { + str[index - 1] = '0'; + str[index - 2] = '2'; + str[index - 3] = '%'; + index -= 3; + } else { + str[index - 1] = str[i]; + index--; + } + } + } + + public static boolean palindrome(String str) { + + return false; + } + +} diff --git a/src/test/java/org/fscotto/interview/demo/InterviewQuestionsC1Test.java b/src/test/java/org/fscotto/interview/demo/InterviewQuestionsC1Test.java new file mode 100644 index 0000000..245913f --- /dev/null +++ b/src/test/java/org/fscotto/interview/demo/InterviewQuestionsC1Test.java @@ -0,0 +1,47 @@ +package org.fscotto.interview.demo; + +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.*; + +class InterviewQuestionsC1Test { + + @Test + void isUniqueCharsTests() { + assertFalse(InterviewQuestionsC1.isUniqueChars(null)); + assertFalse(InterviewQuestionsC1.isUniqueChars("")); + assertTrue(InterviewQuestionsC1.isUniqueChars("a")); + assertTrue(InterviewQuestionsC1.isUniqueChars("abc")); + assertFalse(InterviewQuestionsC1.isUniqueChars("anna")); + assertFalse(InterviewQuestionsC1.isUniqueChars("birbante")); + assertFalse(InterviewQuestionsC1.isUniqueChars(getStringAllASCII())); + } + + private String getStringAllASCII() { + StringBuilder sb = new StringBuilder(130); + for (int i = 0; i < 129; ++i) + sb.append((char) i); + return sb.toString(); + } + + @Test + void isPermutationOfTests() { + assertFalse(InterviewQuestionsC1.permutation(null, "BAC")); + assertFalse(InterviewQuestionsC1.permutation("BAC", null)); + assertFalse(InterviewQuestionsC1.permutation(null, null)); + assertTrue(InterviewQuestionsC1.permutation("", "")); + assertTrue(InterviewQuestionsC1.permutation("ABC", "BAC")); + assertTrue(InterviewQuestionsC1.permutation("BAC", "ABC")); + assertTrue(InterviewQuestionsC1.permutation("BAC", "CAB")); + } + + @Test + void replaceSpacesTests() { + final char[] str = "Mr John Smith ".toCharArray(); + InterviewQuestionsC1.replaceSpaces(str, 13); + assertEquals("Mr%20John%20Smith", new String(str)); + } + + + +}