diff --git a/technical-fundamentals/.gitignore b/technical-fundamentals/.gitignore index 726ecd9b..0a25ecd1 100644 --- a/technical-fundamentals/.gitignore +++ b/technical-fundamentals/.gitignore @@ -1,2 +1,3 @@ node_modules -*copy* \ No newline at end of file +*copy* +venv \ No newline at end of file diff --git a/technical-fundamentals/coding/problems/01_isUnique.ts b/technical-fundamentals/coding/problems/01_isUnique.ts index 07f432b1..44652b2a 100644 --- a/technical-fundamentals/coding/problems/01_isUnique.ts +++ b/technical-fundamentals/coding/problems/01_isUnique.ts @@ -3,4 +3,13 @@ // Implement an algorithm to determine if a string has all unique characters. // What if you cannot use additional data structures? -export default function isUnique(str: string): boolean {} +export default function isUnique(str: string): boolean { + let nonRpeatChar = new Set() + for (let i = 0; i < str.length; i++) { + if (nonRpeatChar.has(str[i])){ + return false + } + nonRpeatChar.add(str[i]) + } + return true +} diff --git a/technical-fundamentals/coding/problems/02_checkPermutations.ts b/technical-fundamentals/coding/problems/02_checkPermutations.ts index 56deb0f0..d45063c5 100644 --- a/technical-fundamentals/coding/problems/02_checkPermutations.ts +++ b/technical-fundamentals/coding/problems/02_checkPermutations.ts @@ -2,4 +2,19 @@ // Given two strings, write a method to decide if one is a permutation of the other. -export default function checkPermutations(s1: string, s2: string): boolean {} +export default function checkPermutations(s1: string, s2: string): boolean { + if (s1.length != s2.length) { + return false; + } + const count: Record = {}; + for (const char of s1) { + count[char] = (count[char] || 0) + 1; + } + for (const char of s2) { + if (!count[char]) { + return false; + } + count[char] --; + } + return true; +} diff --git a/technical-fundamentals/coding/problems/03_urlify.ts b/technical-fundamentals/coding/problems/03_urlify.ts index ca989b83..a43b1e4e 100644 --- a/technical-fundamentals/coding/problems/03_urlify.ts +++ b/technical-fundamentals/coding/problems/03_urlify.ts @@ -5,5 +5,13 @@ // and that you are given the "true" length of the string. export default function URLify (s1 : string): string { - + let res: string = "" + for (let i = 0; i < s1.length; i++) { + if (s1[i] == " ") { + res += "%20"; + } else { + res += s1[i]; + } + } + return res } \ No newline at end of file diff --git a/technical-fundamentals/coding/problems/04_palindromePermutation.ts b/technical-fundamentals/coding/problems/04_palindromePermutation.ts index d80c9217..521c3a9a 100644 --- a/technical-fundamentals/coding/problems/04_palindromePermutation.ts +++ b/technical-fundamentals/coding/problems/04_palindromePermutation.ts @@ -10,5 +10,33 @@ // ``` export default function palindromePermutation (str: string): boolean { + let normalized: string = ""; + for (let i = 0; i < str.length; i++) { + if (/[a-zA-Z0-9]/.test(str[i])) { + normalized += str[i].toLowerCase(); + } + } + + const charCounts: { [key: string]: number} = {}; + for (let i = 0; i < normalized.length; i++) { + const char = normalized[i]; + if (charCounts[char]) { + charCounts[char] += 1; + }else { + charCounts[char] = 1; + } + } + + let oddCount = 0; + for (const char in charCounts) { + if (charCounts[char] % 2 !== 0) { + oddCount += 1; + if (oddCount > 1) { + return false; + } + } + } + + return true } \ No newline at end of file diff --git a/technical-fundamentals/coding/problems/05_oneAway.ts b/technical-fundamentals/coding/problems/05_oneAway.ts index 79c9a12d..8f026b2e 100644 --- a/technical-fundamentals/coding/problems/05_oneAway.ts +++ b/technical-fundamentals/coding/problems/05_oneAway.ts @@ -5,5 +5,38 @@ // Given two strings, write a function to check if they are one edit (or zero edits) away. export default function isOneAway(str1: string, str2: string): boolean { - + if (Math.abs(str1.length - str2.length) > 1) { + return false; + } + + const s1 = str1.length < str2.length ? str1 : str2; + const s2 = str1.length < str2.length ? str2 : str1; + + let index1 = 0; + let index2 = 0; + let foundDifference = false; + + while (index1 < s1.length && index2 < s2.length) { + if (s1[index1] !== s2[index2]) { + if (foundDifference) { + return false; + } + foundDifference = true; + + if (s1.length === s2.length) { + // replace: move both pointers + index1 += 1; + index2 += 1; + } else { + // insert/remove: skip char in longer string + index2 += 1; + } + } else { + index1 += 1; + index2 += 1; + } + } + + return (s2.length - index2) <= 1; } + diff --git a/technical-fundamentals/coding/problems/06_stringCompression.ts b/technical-fundamentals/coding/problems/06_stringCompression.ts index 525ce40d..c022784d 100644 --- a/technical-fundamentals/coding/problems/06_stringCompression.ts +++ b/technical-fundamentals/coding/problems/06_stringCompression.ts @@ -7,5 +7,22 @@ // You can assume the string has only uppercase and lowercase letters (a - z). export default function stringCompression (str: string) : string { - + if (!str) { + return ""; + } + + let count = 1; + let res_compress = ""; + + for (let i = 1; i < str.length ; i++) { + if (str[i] === str[i -1]) { + count ++; + } else { + res_compress += str[i -1] + count.toString(); + count = 1; + } + } + res_compress += str[str.length - 1] + count.toString(); + + return res_compress.length < str.length ? res_compress : str; } \ No newline at end of file diff --git a/technical-fundamentals/coding/problems/07_rotateMatrix.ts b/technical-fundamentals/coding/problems/07_rotateMatrix.ts index 875d0841..80c5e046 100644 --- a/technical-fundamentals/coding/problems/07_rotateMatrix.ts +++ b/technical-fundamentals/coding/problems/07_rotateMatrix.ts @@ -6,5 +6,23 @@ type Matrix = number[][] export default function rotateMatrix (matrix: Matrix) { + let n = matrix.length + for(let layer = 0; layer < Math.floor(n / 2); layer++) { + const first = layer + const last = n - 1 - first + for(let i = first; i < last; i++) { + const offset = i - first + const top = matrix[first][i] + + // left - top + matrix[first][i] = matrix[last - offset][first] + // bottom - left + matrix[last - offset][first] = matrix[last][last - offset] + // right - bottom + matrix[last][last - offset] = matrix[i][last] + // top - right + matrix[i][last] = top + } + } } \ No newline at end of file diff --git a/technical-fundamentals/coding/problems/08_zeroMatrix.ts b/technical-fundamentals/coding/problems/08_zeroMatrix.ts index 5bf6941d..8d0a3022 100644 --- a/technical-fundamentals/coding/problems/08_zeroMatrix.ts +++ b/technical-fundamentals/coding/problems/08_zeroMatrix.ts @@ -5,5 +5,35 @@ type Matrix = number[][] export default function zeroMatrix (matrix: Matrix) { + if (!matrix || matrix.length === 0 || matrix[0].length === 0) { + return + } + const rows = matrix.length + const columns = matrix[0].length + const zero_rows = new Set() + const zero_columns = new Set() + + for (let r = 0; r < rows; r ++) { + for (let c = 0; c < columns; c ++) { + if (matrix[r][c] === 0) { + zero_rows.add(r) + zero_columns.add(c) + } + } + } + + // Zero out rows + for (const r of zero_rows) { + for (let c = 0; c < columns; c++) { + matrix[r][c] = 0; + } + } + + // Zero out columns + for (const c of zero_columns) { + for (let r = 0; r < rows; r++) { + matrix[r][c] = 0; + } + } } \ No newline at end of file diff --git a/technical-fundamentals/coding/problems/09_stringRotation.ts b/technical-fundamentals/coding/problems/09_stringRotation.ts index afc900e0..d67990e8 100644 --- a/technical-fundamentals/coding/problems/09_stringRotation.ts +++ b/technical-fundamentals/coding/problems/09_stringRotation.ts @@ -6,4 +6,10 @@ import { isSubstring } from "./__utils__/strings" // Given two strings, s1 and s2, write code to check if s2 is a rotation of s1 using only one call to isSubstring. // [e.g., "waterbottle" is a rotation of 'erbottlewat") -export default function stringRotation(s1: string, s2: string): boolean {} +export default function stringRotation(s1: string, s2: string): boolean { + if (s1.length !== s2.length) { + return false + } + + return (s1 + s1).includes(s2) +} diff --git a/technical-fundamentals/coding/problems/__pycache__/is_unique.cpython-311.pyc b/technical-fundamentals/coding/problems/__pycache__/is_unique.cpython-311.pyc new file mode 100644 index 00000000..a8aaafe7 Binary files /dev/null and b/technical-fundamentals/coding/problems/__pycache__/is_unique.cpython-311.pyc differ diff --git a/technical-fundamentals/coding/problems/__tests__/strings/01_is_unique_test.py b/technical-fundamentals/coding/problems/__tests__/strings/01_is_unique_test.py new file mode 100644 index 00000000..0265b71a --- /dev/null +++ b/technical-fundamentals/coding/problems/__tests__/strings/01_is_unique_test.py @@ -0,0 +1,36 @@ +import unittest +from coding.problems.python_solutions.is_unique import is_unique_chars + +class TestIsUnique(unittest.TestCase): + + def test_unique_characters(self): + self.assertTrue(is_unique_chars("abc")) + self.assertTrue(is_unique_chars("abcdefg")) + self.assertTrue(is_unique_chars("123456")) + self.assertTrue(is_unique_chars("!@#$%^")) + + def test_non_unique_characters(self): + self.assertFalse(is_unique_chars("aab")) + self.assertFalse(is_unique_chars("hello")) + self.assertFalse(is_unique_chars("testing")) + self.assertFalse(is_unique_chars("1234456")) + self.assertFalse(is_unique_chars("abccdef")) + + def test_empty_string(self): + self.assertTrue(is_unique_chars("")) + + def test_whitespace_handling(self): + self.assertFalse(is_unique_chars("a b c")) + self.assertTrue(is_unique_chars("ab c")) + + def test_special_characters(self): + self.assertTrue(is_unique_chars("!@#$%^&*")) + self.assertFalse(is_unique_chars("!@#$%^&*!")) + + def test_mixed_case(self): + self.assertTrue(is_unique_chars("aA")) + self.assertTrue(is_unique_chars("Aa")) + self.assertFalse(is_unique_chars("Hello")) + +if __name__ == '__main__': + unittest.main() diff --git a/technical-fundamentals/coding/problems/__tests__/strings/02_check_permutations_test.py b/technical-fundamentals/coding/problems/__tests__/strings/02_check_permutations_test.py new file mode 100644 index 00000000..b145a97a --- /dev/null +++ b/technical-fundamentals/coding/problems/__tests__/strings/02_check_permutations_test.py @@ -0,0 +1,28 @@ +import unittest +from coding.problems.python_solutions.check_permutations import check_permutations + +class TestCheckPermutations(unittest.TestCase): + + def test_permutations_same_length(self): + self.assertTrue(check_permutations("abc", "cba")) + + def test_different_lengths(self): + self.assertFalse(check_permutations("abc", "cbad")) + + def test_permutations_with_special_characters(self): + self.assertTrue(check_permutations("abc!", "!bac")) + + def test_non_permutations_with_special_characters(self): + self.assertFalse(check_permutations("abc!", "!bcd")) + + def test_empty_strings(self): + self.assertTrue(check_permutations("", "")) + + def test_long_identical_strings(self): + self.assertTrue(check_permutations("a" * 1000, "a" * 1000)) + + def test_long_different_strings(self): + self.assertFalse(check_permutations("a" * 1000, "b" * 1000)) + +if __name__ == '__main__': + unittest.main() diff --git a/technical-fundamentals/coding/problems/__tests__/strings/03_urlify_test.py b/technical-fundamentals/coding/problems/__tests__/strings/03_urlify_test.py new file mode 100644 index 00000000..d52102a9 --- /dev/null +++ b/technical-fundamentals/coding/problems/__tests__/strings/03_urlify_test.py @@ -0,0 +1,28 @@ +import unittest +from coding.problems.python_solutions.urlify import urlify # Asegúrate de que la función esté definida con este nombre + +class TestURLify(unittest.TestCase): + + def test_replaces_spaces(self): + self.assertEqual(urlify('ab c'), 'ab%20c') + + def test_leading_trailing_spaces(self): + self.assertEqual(urlify(' ab c '), '%20%20ab%20c%20%20') + + def test_empty_string(self): + self.assertEqual(urlify(''), '') + + def test_no_spaces(self): + self.assertEqual(urlify('abc'), 'abc') + + def test_multiple_consecutive_spaces(self): + self.assertEqual(urlify('a b c'), 'a%20%20b%20%20%20c') + + def test_special_characters(self): + self.assertEqual(urlify('a b!c'), 'a%20b!c') + + def test_mr_john_smith(self): + self.assertEqual(urlify('Mr 3ohn Smith'), 'Mr%203ohn%20Smith') + +if __name__ == '__main__': + unittest.main() diff --git a/technical-fundamentals/coding/problems/__tests__/strings/04_palindrome_permutation_test.py b/technical-fundamentals/coding/problems/__tests__/strings/04_palindrome_permutation_test.py new file mode 100644 index 00000000..234d5822 --- /dev/null +++ b/technical-fundamentals/coding/problems/__tests__/strings/04_palindrome_permutation_test.py @@ -0,0 +1,34 @@ +import unittest +from coding.problems.python_solutions.palindrome_permutation import palindrome_permutation + +class TestPalindromePermutation(unittest.TestCase): + + def test_empty_string(self): + self.assertTrue(palindrome_permutation("")) + + def test_single_character(self): + self.assertTrue(palindrome_permutation("a")) + + def test_palindrome_odd_length(self): + self.assertTrue(palindrome_permutation("taco cat")) + + def test_palindrome_even_length(self): + self.assertTrue(palindrome_permutation("rdeder")) + + def test_non_palindrome_odd_length(self): + self.assertFalse(palindrome_permutation("hello")) + + def test_non_palindrome_even_length(self): + self.assertFalse(palindrome_permutation("world")) + + def test_mixed_case(self): + self.assertTrue(palindrome_permutation("RaceCar")) + + def test_numeric_palindrome(self): + self.assertTrue(palindrome_permutation("12321")) + + def test_impossible_permutation(self): + self.assertFalse(palindrome_permutation("abcdefg")) + +if __name__ == '__main__': + unittest.main() diff --git a/technical-fundamentals/coding/problems/__tests__/strings/05_oneAway.test.ts b/technical-fundamentals/coding/problems/__tests__/strings/05_oneAway.test.ts index 4b55f20b..524b6af0 100644 --- a/technical-fundamentals/coding/problems/__tests__/strings/05_oneAway.test.ts +++ b/technical-fundamentals/coding/problems/__tests__/strings/05_oneAway.test.ts @@ -41,4 +41,5 @@ describe("05 - oneAway", () => { test("Empty and Non-Empty String", () => { expect(oneAway("", "a")).toEqual(true); // Empty string and a non-empty string }); + }); diff --git a/technical-fundamentals/coding/problems/__tests__/strings/05_one_away_test.py b/technical-fundamentals/coding/problems/__tests__/strings/05_one_away_test.py new file mode 100644 index 00000000..2ad16409 --- /dev/null +++ b/technical-fundamentals/coding/problems/__tests__/strings/05_one_away_test.py @@ -0,0 +1,31 @@ +import unittest +from coding.problems.python_solutions.one_away import one_away # Replace with your actual function import + +class TestOneAway(unittest.TestCase): + + def test_replace(self): + self.assertTrue(one_away("pale", "bale")) + self.assertFalse(one_away("bbaa", "bcca")) + + def test_insert(self): + self.assertTrue(one_away("pale", "ple")) + + def test_remove(self): + self.assertTrue(one_away("pale", "pales")) + + def test_same_strings(self): + self.assertTrue(one_away("abc", "abc")) + + def test_more_than_one_edit_away(self): + self.assertFalse(one_away("abcd", "efgh")) + self.assertFalse(one_away("palesa", "pale")) + + def test_empty_strings(self): + self.assertTrue(one_away("", "")) + + def test_one_char_difference(self): + self.assertTrue(one_away("a", "ab")) + self.assertTrue(one_away("", "a")) + +if __name__ == "__main__": + unittest.main() diff --git a/technical-fundamentals/coding/problems/__tests__/strings/06_string_compression_test.py b/technical-fundamentals/coding/problems/__tests__/strings/06_string_compression_test.py new file mode 100644 index 00000000..143ee98a --- /dev/null +++ b/technical-fundamentals/coding/problems/__tests__/strings/06_string_compression_test.py @@ -0,0 +1,26 @@ +import unittest +from coding.problems.python_solutions.string_compression import string_compression + +class TestStringCompression(unittest.TestCase): + + def test_compresses_repeated_characters(self): + self.assertEqual(string_compression('aabcccccaaa'), 'a2b1c5a3') + + def test_returns_original_if_not_shorter(self): + self.assertEqual(string_compression('abcde'), 'abcde') + + def test_empty_string(self): + self.assertEqual(string_compression(''), '') + + def test_single_character(self): + self.assertEqual(string_compression('a'), 'a') + + def test_upper_and_lower_case(self): + self.assertEqual(string_compression('AAAbbbCCCddd'), 'A3b3C3d3') + + def test_no_repeated_characters(self): + self.assertEqual(string_compression('abcdef'), 'abcdef') + + +if __name__ == '__main__': + unittest.main() diff --git a/technical-fundamentals/coding/problems/__tests__/strings/07_rotate_matrix_test.py b/technical-fundamentals/coding/problems/__tests__/strings/07_rotate_matrix_test.py new file mode 100644 index 00000000..ad515d4e --- /dev/null +++ b/technical-fundamentals/coding/problems/__tests__/strings/07_rotate_matrix_test.py @@ -0,0 +1,70 @@ +import unittest +from typing import List +from copy import deepcopy + +from coding.problems.python_solutions.rotate_matrix import rotate_matrix + +class TestRotateMatrix(unittest.TestCase): + + def test_rotates_2x2(self): + matrix = [ + [1, 2], + [3, 4] + ] + expected = [ + [3, 1], + [4, 2] + ] + rotate_matrix(matrix) + self.assertEqual(matrix, expected) + + def test_rotates_3x3(self): + matrix = [ + [1, 2, 3], + [4, 5, 6], + [7, 8, 9] + ] + expected = [ + [7, 4, 1], + [8, 5, 2], + [9, 6, 3] + ] + rotate_matrix(matrix) + self.assertEqual(matrix, expected) + + def test_rotates_4x4(self): + matrix = [ + [1, 2, 3, 4], + [5, 6, 7, 8], + [9, 10, 11, 12], + [13, 14, 15, 16] + ] + expected = [ + [13, 9, 5, 1], + [14, 10, 6, 2], + [15, 11, 7, 3], + [16, 12, 8, 4] + ] + rotate_matrix(matrix) + self.assertEqual(matrix, expected) + + def test_rotates_5x5(self): + matrix = [ + [1, 2, 3, 4, 5], + [6, 7, 8, 9, 10], + [11, 12, 13, 14, 15], + [16, 17, 18, 19, 20], + [21, 22, 23, 24, 25] + ] + expected = [ + [21, 16, 11, 6, 1], + [22, 17, 12, 7, 2], + [23, 18, 13, 8, 3], + [24, 19, 14, 9, 4], + [25, 20, 15, 10, 5] + ] + rotate_matrix(matrix) + self.assertEqual(matrix, expected) + +if __name__ == '__main__': + unittest.main() diff --git a/technical-fundamentals/coding/problems/__tests__/strings/08_zero_matrix_test.py b/technical-fundamentals/coding/problems/__tests__/strings/08_zero_matrix_test.py new file mode 100644 index 00000000..a82182d4 --- /dev/null +++ b/technical-fundamentals/coding/problems/__tests__/strings/08_zero_matrix_test.py @@ -0,0 +1,66 @@ +import unittest +from typing import List +from coding.problems.python_solutions.zero_matrix import zero_matrix + +class TestZeroMatrix(unittest.TestCase): + + def test_zeroes_2x2_matrix(self): + matrix = [ + [0, 2], + [3, 4], + ] + expected = [ + [0, 0], + [0, 4], + ] + zero_matrix(matrix) + self.assertEqual(matrix, expected) + + def test_zeroes_3x3_matrix(self): + matrix = [ + [1, 2, 3], + [4, 5, 6], + [7, 0, 9], + ] + expected = [ + [1, 0, 3], + [4, 0, 6], + [0, 0, 0], + ] + zero_matrix(matrix) + self.assertEqual(matrix, expected) + + def test_zeroes_4x4_matrix(self): + matrix = [ + [1, 2, 3, 4], + [5, 6, 0, 8], + [9, 10, 11, 12], + [13, 14, 15, 16], + ] + expected = [ + [1, 2, 0, 4], + [0, 0, 0, 0], + [9, 10, 0, 12], + [13, 14, 0, 16], + ] + zero_matrix(matrix) + self.assertEqual(matrix, expected) + + def test_two_zeroes_4x4_matrix(self): + matrix = [ + [0, 2, 3, 4], + [5, 6, 0, 8], + [9, 10, 11, 12], + [13, 14, 15, 16], + ] + expected = [ + [0, 0, 0, 0], + [0, 0, 0, 0], + [0, 10, 0, 12], + [0, 14, 0, 16], + ] + zero_matrix(matrix) + self.assertEqual(matrix, expected) + +if __name__ == '__main__': + unittest.main() diff --git a/technical-fundamentals/coding/problems/__tests__/strings/09_string_rotation_test.py b/technical-fundamentals/coding/problems/__tests__/strings/09_string_rotation_test.py new file mode 100644 index 00000000..d5a2a85d --- /dev/null +++ b/technical-fundamentals/coding/problems/__tests__/strings/09_string_rotation_test.py @@ -0,0 +1,19 @@ +import unittest +from coding.problems.python_solutions.string_rotation import string_rotation + +class TestStringRotation(unittest.TestCase): + + def test_rotates_string(self): + str1 = "Hello" + str2 = "oHell" + result = string_rotation(str1, str2) + self.assertEqual(result, True) + + def test_rotates_another_string(self): + str1 = "waterbottle" + str2 = "erbottlewat" + result = string_rotation(str1, str2) + self.assertEqual(result, True) + +if __name__ == '__main__': + unittest.main() diff --git a/technical-fundamentals/coding/problems/__tests__/strings/__pycache__/01_is_unique_test.cpython-311.pyc b/technical-fundamentals/coding/problems/__tests__/strings/__pycache__/01_is_unique_test.cpython-311.pyc new file mode 100644 index 00000000..79fac107 Binary files /dev/null and b/technical-fundamentals/coding/problems/__tests__/strings/__pycache__/01_is_unique_test.cpython-311.pyc differ diff --git a/technical-fundamentals/coding/problems/__tests__/strings/__pycache__/02_check_permutations_test.cpython-311.pyc b/technical-fundamentals/coding/problems/__tests__/strings/__pycache__/02_check_permutations_test.cpython-311.pyc new file mode 100644 index 00000000..54805518 Binary files /dev/null and b/technical-fundamentals/coding/problems/__tests__/strings/__pycache__/02_check_permutations_test.cpython-311.pyc differ diff --git a/technical-fundamentals/coding/problems/__tests__/strings/__pycache__/03_urlify_test.cpython-311.pyc b/technical-fundamentals/coding/problems/__tests__/strings/__pycache__/03_urlify_test.cpython-311.pyc new file mode 100644 index 00000000..c6929ae3 Binary files /dev/null and b/technical-fundamentals/coding/problems/__tests__/strings/__pycache__/03_urlify_test.cpython-311.pyc differ diff --git a/technical-fundamentals/coding/problems/__tests__/strings/__pycache__/04_palindrome_permutation.cpython-311.pyc b/technical-fundamentals/coding/problems/__tests__/strings/__pycache__/04_palindrome_permutation.cpython-311.pyc new file mode 100644 index 00000000..6aa972f6 Binary files /dev/null and b/technical-fundamentals/coding/problems/__tests__/strings/__pycache__/04_palindrome_permutation.cpython-311.pyc differ diff --git a/technical-fundamentals/coding/problems/__tests__/strings/__pycache__/04_palindrome_permutation_test.cpython-311.pyc b/technical-fundamentals/coding/problems/__tests__/strings/__pycache__/04_palindrome_permutation_test.cpython-311.pyc new file mode 100644 index 00000000..3262f61e Binary files /dev/null and b/technical-fundamentals/coding/problems/__tests__/strings/__pycache__/04_palindrome_permutation_test.cpython-311.pyc differ diff --git a/technical-fundamentals/coding/problems/__tests__/strings/__pycache__/05_one_away_test.cpython-311.pyc b/technical-fundamentals/coding/problems/__tests__/strings/__pycache__/05_one_away_test.cpython-311.pyc new file mode 100644 index 00000000..8358f6dd Binary files /dev/null and b/technical-fundamentals/coding/problems/__tests__/strings/__pycache__/05_one_away_test.cpython-311.pyc differ diff --git a/technical-fundamentals/coding/problems/__tests__/strings/__pycache__/06_string_compression_test.cpython-311.pyc b/technical-fundamentals/coding/problems/__tests__/strings/__pycache__/06_string_compression_test.cpython-311.pyc new file mode 100644 index 00000000..8c50368d Binary files /dev/null and b/technical-fundamentals/coding/problems/__tests__/strings/__pycache__/06_string_compression_test.cpython-311.pyc differ diff --git a/technical-fundamentals/coding/problems/__tests__/strings/__pycache__/07_rotate_matrix_test.cpython-311.pyc b/technical-fundamentals/coding/problems/__tests__/strings/__pycache__/07_rotate_matrix_test.cpython-311.pyc new file mode 100644 index 00000000..9fd9b5fc Binary files /dev/null and b/technical-fundamentals/coding/problems/__tests__/strings/__pycache__/07_rotate_matrix_test.cpython-311.pyc differ diff --git a/technical-fundamentals/coding/problems/__tests__/strings/__pycache__/08_zero_matrix_test.cpython-311.pyc b/technical-fundamentals/coding/problems/__tests__/strings/__pycache__/08_zero_matrix_test.cpython-311.pyc new file mode 100644 index 00000000..51512d5b Binary files /dev/null and b/technical-fundamentals/coding/problems/__tests__/strings/__pycache__/08_zero_matrix_test.cpython-311.pyc differ diff --git a/technical-fundamentals/coding/problems/__tests__/strings/__pycache__/09_string_rotation_test.cpython-311.pyc b/technical-fundamentals/coding/problems/__tests__/strings/__pycache__/09_string_rotation_test.cpython-311.pyc new file mode 100644 index 00000000..176cf13f Binary files /dev/null and b/technical-fundamentals/coding/problems/__tests__/strings/__pycache__/09_string_rotation_test.cpython-311.pyc differ diff --git a/technical-fundamentals/coding/problems/python_solutions/__init__.py b/technical-fundamentals/coding/problems/python_solutions/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/technical-fundamentals/coding/problems/python_solutions/__pycache__/__init__.cpython-311.pyc b/technical-fundamentals/coding/problems/python_solutions/__pycache__/__init__.cpython-311.pyc new file mode 100644 index 00000000..1e7b0768 Binary files /dev/null and b/technical-fundamentals/coding/problems/python_solutions/__pycache__/__init__.cpython-311.pyc differ diff --git a/technical-fundamentals/coding/problems/python_solutions/__pycache__/check_permutations.cpython-311.pyc b/technical-fundamentals/coding/problems/python_solutions/__pycache__/check_permutations.cpython-311.pyc new file mode 100644 index 00000000..02a79a5f Binary files /dev/null and b/technical-fundamentals/coding/problems/python_solutions/__pycache__/check_permutations.cpython-311.pyc differ diff --git a/technical-fundamentals/coding/problems/python_solutions/__pycache__/is_unique.cpython-311.pyc b/technical-fundamentals/coding/problems/python_solutions/__pycache__/is_unique.cpython-311.pyc new file mode 100644 index 00000000..e0a6b060 Binary files /dev/null and b/technical-fundamentals/coding/problems/python_solutions/__pycache__/is_unique.cpython-311.pyc differ diff --git a/technical-fundamentals/coding/problems/python_solutions/__pycache__/one_away.cpython-311.pyc b/technical-fundamentals/coding/problems/python_solutions/__pycache__/one_away.cpython-311.pyc new file mode 100644 index 00000000..74cb6579 Binary files /dev/null and b/technical-fundamentals/coding/problems/python_solutions/__pycache__/one_away.cpython-311.pyc differ diff --git a/technical-fundamentals/coding/problems/python_solutions/__pycache__/palindrome_permutation.cpython-311.pyc b/technical-fundamentals/coding/problems/python_solutions/__pycache__/palindrome_permutation.cpython-311.pyc new file mode 100644 index 00000000..e6d5919f Binary files /dev/null and b/technical-fundamentals/coding/problems/python_solutions/__pycache__/palindrome_permutation.cpython-311.pyc differ diff --git a/technical-fundamentals/coding/problems/python_solutions/__pycache__/rotate_matrix.cpython-311.pyc b/technical-fundamentals/coding/problems/python_solutions/__pycache__/rotate_matrix.cpython-311.pyc new file mode 100644 index 00000000..94117538 Binary files /dev/null and b/technical-fundamentals/coding/problems/python_solutions/__pycache__/rotate_matrix.cpython-311.pyc differ diff --git a/technical-fundamentals/coding/problems/python_solutions/__pycache__/string_compression.cpython-311.pyc b/technical-fundamentals/coding/problems/python_solutions/__pycache__/string_compression.cpython-311.pyc new file mode 100644 index 00000000..fd0f172c Binary files /dev/null and b/technical-fundamentals/coding/problems/python_solutions/__pycache__/string_compression.cpython-311.pyc differ diff --git a/technical-fundamentals/coding/problems/python_solutions/__pycache__/string_rotation.cpython-311.pyc b/technical-fundamentals/coding/problems/python_solutions/__pycache__/string_rotation.cpython-311.pyc new file mode 100644 index 00000000..8dd8cfd3 Binary files /dev/null and b/technical-fundamentals/coding/problems/python_solutions/__pycache__/string_rotation.cpython-311.pyc differ diff --git a/technical-fundamentals/coding/problems/python_solutions/__pycache__/urlify.cpython-311.pyc b/technical-fundamentals/coding/problems/python_solutions/__pycache__/urlify.cpython-311.pyc new file mode 100644 index 00000000..5a820cad Binary files /dev/null and b/technical-fundamentals/coding/problems/python_solutions/__pycache__/urlify.cpython-311.pyc differ diff --git a/technical-fundamentals/coding/problems/python_solutions/__pycache__/zero_matrix.cpython-311.pyc b/technical-fundamentals/coding/problems/python_solutions/__pycache__/zero_matrix.cpython-311.pyc new file mode 100644 index 00000000..c285afc5 Binary files /dev/null and b/technical-fundamentals/coding/problems/python_solutions/__pycache__/zero_matrix.cpython-311.pyc differ diff --git a/technical-fundamentals/coding/problems/python_solutions/check_permutations.py b/technical-fundamentals/coding/problems/python_solutions/check_permutations.py new file mode 100644 index 00000000..e76e32c0 --- /dev/null +++ b/technical-fundamentals/coding/problems/python_solutions/check_permutations.py @@ -0,0 +1,4 @@ +def check_permutations(s1: str, s2: str) -> bool: + if len(s1) != len(s2): + return False + return sorted(s1) == sorted(s2) \ No newline at end of file diff --git a/technical-fundamentals/coding/problems/python_solutions/is_unique.py b/technical-fundamentals/coding/problems/python_solutions/is_unique.py new file mode 100644 index 00000000..ef85273c --- /dev/null +++ b/technical-fundamentals/coding/problems/python_solutions/is_unique.py @@ -0,0 +1,8 @@ +def is_unique_chars(s:str) -> bool: + non_repeat_char = set() + for char in s: + if char in non_repeat_char: + return False + else: + non_repeat_char.add(char) + return True diff --git a/technical-fundamentals/coding/problems/python_solutions/one_away.py b/technical-fundamentals/coding/problems/python_solutions/one_away.py new file mode 100644 index 00000000..507d56a8 --- /dev/null +++ b/technical-fundamentals/coding/problems/python_solutions/one_away.py @@ -0,0 +1,26 @@ +def one_away(str1: str, str2: str) -> bool: + index1 = 0 + index2 = 0 + found_difference = False + + if abs(len(str1) - len(str2)) > 1: + return False + + s1 = str1 if len(str1) < len(str2) else str2 + s2 = str2 if len(str1) < len(str2) else str1 + + while index2 < len(s2) and index1 < len(s1): + if s1[index1] != s2[index2]: + if found_difference: + return False + + found_difference = True + + if len(s1) == len(s2): + index1 += 1 + else: + index1 += 1 + + index2 += 1 + + return True \ No newline at end of file diff --git a/technical-fundamentals/coding/problems/python_solutions/palindrome_permutation.py b/technical-fundamentals/coding/problems/python_solutions/palindrome_permutation.py new file mode 100644 index 00000000..7a182231 --- /dev/null +++ b/technical-fundamentals/coding/problems/python_solutions/palindrome_permutation.py @@ -0,0 +1,20 @@ +def palindrome_permutation(s: str) -> bool: + normalized:str = "" + for char in s: + if char.isalnum(): + normalized += char.lower() + + char_counts = {} + for char in normalized: + if char in char_counts: + char_counts[char] += 1 + else: + char_counts[char] = 1 + + odd_counts = 0 + for char in char_counts: + if char_counts[char] % 2 != 0: + odd_counts += 1 + if odd_counts > 1: + return False + return True \ No newline at end of file diff --git a/technical-fundamentals/coding/problems/python_solutions/rotate_matrix.py b/technical-fundamentals/coding/problems/python_solutions/rotate_matrix.py new file mode 100644 index 00000000..ed5c86fd --- /dev/null +++ b/technical-fundamentals/coding/problems/python_solutions/rotate_matrix.py @@ -0,0 +1,21 @@ +from typing import List + + +def rotate_matrix(matrix: List[List[int]] ) -> None: + n = len(matrix) + for layer in range(n // 2): + first = layer + last = n - 1 - layer + + for i in range(first, last): + offset = i - first + top = matrix[first][i] + + # left - top + matrix[first][i] = matrix[last - offset][first] + # bottom - left + matrix[last - offset][first] = matrix[last][last - offset] + # right - bottom + matrix[last][last - offset] = matrix[i][last] + # top - right + matrix[i][last] = top \ No newline at end of file diff --git a/technical-fundamentals/coding/problems/python_solutions/string_compression.py b/technical-fundamentals/coding/problems/python_solutions/string_compression.py new file mode 100644 index 00000000..5a552c5d --- /dev/null +++ b/technical-fundamentals/coding/problems/python_solutions/string_compression.py @@ -0,0 +1,17 @@ +def string_compression(str1: str) -> str: + if not str1: + return "" + + count = 1 + res_compress = "" + + for i in range(1, len(str1)): + if str1[i] == str1[i -1]: + count += 1 + else: + res_compress += str1[i-1] + str(count) + count = 1 + + res_compress += str1[-1] + str(count) + + return res_compress if len(res_compress) < len(str1) else str1 \ No newline at end of file diff --git a/technical-fundamentals/coding/problems/python_solutions/string_rotation.py b/technical-fundamentals/coding/problems/python_solutions/string_rotation.py new file mode 100644 index 00000000..7040e751 --- /dev/null +++ b/technical-fundamentals/coding/problems/python_solutions/string_rotation.py @@ -0,0 +1,4 @@ +def string_rotation(s1: str, s2: str) -> bool: + if len(s1) != len(s2): + return False + return s2 in (s1 + s1) \ No newline at end of file diff --git a/technical-fundamentals/coding/problems/python_solutions/urlify.py b/technical-fundamentals/coding/problems/python_solutions/urlify.py new file mode 100644 index 00000000..79cd7b62 --- /dev/null +++ b/technical-fundamentals/coding/problems/python_solutions/urlify.py @@ -0,0 +1,8 @@ +def urlify(s:str) -> str: + res = "" + for i in range(len(s)): + if s[i] == " ": + res += "%20" + else: + res += s[i] + return res diff --git a/technical-fundamentals/coding/problems/python_solutions/zero_matrix.py b/technical-fundamentals/coding/problems/python_solutions/zero_matrix.py new file mode 100644 index 00000000..e2fd6891 --- /dev/null +++ b/technical-fundamentals/coding/problems/python_solutions/zero_matrix.py @@ -0,0 +1,28 @@ +from typing import List + + +def zero_matrix(matrix: List[List[int]]) -> None: + if not matrix: + return + + rows = len(matrix) + columns = len(matrix[0]) + + zero_rows = set() + zero_columns = set() + + for r in range(rows): + for c in range(columns): + if matrix[r][c] == 0: + zero_rows.add(r) + zero_columns.add(c) + + # Zero rows + for r in zero_rows: + for c in range(columns): + matrix[r][c] = 0 + + # Zero columns + for c in zero_columns: + for r in range(rows): + matrix[r][c] = 0 \ No newline at end of file diff --git a/technical-fundamentals/makefile b/technical-fundamentals/makefile new file mode 100644 index 00000000..314d978a --- /dev/null +++ b/technical-fundamentals/makefile @@ -0,0 +1,30 @@ +VENV_NAME = venv +PYTHON = python3 +REQUIREMENTS = requirements.txt + +.PHONY: all venv install test clean + +all: venv install test + +venv: + @if [ ! -d "$(VENV_NAME)" ]; then \ + $(PYTHON) -m venv $(VENV_NAME); \ + echo "Virtual environment created."; \ + else \ + echo "Virtual environment already exists."; \ + fi + +install: venv + @if [ -f "$(REQUIREMENTS)" ]; then \ + $(VENV_NAME)/bin/pip install -r $(REQUIREMENTS); \ + echo "Requirements installed."; \ + else \ + echo "No requirements.txt found. Skipping install."; \ + fi + +test: venv + $(VENV_NAME)/bin/python -m unittest $(PWD)/coding/problems/__tests__/strings/*.py -v + +clean: + rm -rf $(VENV_NAME) + echo "Virtual environment removed."