diff --git a/Chapter 1/2_Check Permutation/CheckPermutation.py b/Chapter 1/2_Check Permutation/CheckPermutation.py deleted file mode 100644 index 811dbd8c..00000000 --- a/Chapter 1/2_Check Permutation/CheckPermutation.py +++ /dev/null @@ -1,35 +0,0 @@ -# O(N) -import unittest -from collections import Counter - -def check_permutation(string): - str1 = string[0] - str2 = string[1] - if len(str1) != len(str2): - return False - counter = Counter() - for c in str1: - counter[c] += 1 - for c in str2: - if counter[c] == 0: - return False - counter[c] -= 1 - return True - -class Test(unittest.TestCase): - dataT = [(['abcd', 'bacd']), (['3563476', '7334566']), - (['wef34f', 'wffe34'])] - dataF = [(['abcd', 'd2cba']), (['2354', '1234']), (['dcw4f', 'dcw5f'])] - - def test_cp(self): - # true check - for test_string in self.dataT: - actual = check_permutation(test_string) - self.assertTrue(actual) - # false check - for test_string in self.dataF: - actual = check_permutation(test_string) - self.assertFalse(actual) - -if __name__ == "__main__": - unittest.main() diff --git a/Chapter 1/1_Is Unique/IsUnique.py b/Chapter1/1_Is Unique/IsUnique.py similarity index 100% rename from Chapter 1/1_Is Unique/IsUnique.py rename to Chapter1/1_Is Unique/IsUnique.py diff --git a/Chapter1/2_Check Permutation/CheckPermutation.py b/Chapter1/2_Check Permutation/CheckPermutation.py new file mode 100644 index 00000000..49046309 --- /dev/null +++ b/Chapter1/2_Check Permutation/CheckPermutation.py @@ -0,0 +1,43 @@ +# O(N) +import unittest +from collections import Counter + + +def check_permutation(str1, str2): + if len(str1) != len(str2): + return False + counter = Counter() + for c in str1: + counter[c] += 1 + for c in str2: + if counter[c] == 0: + return False + counter[c] -= 1 + return True + + +class Test(unittest.TestCase): + dataT = ( + ('abcd', 'bacd'), + ('3563476', '7334566'), + ('wef34f', 'wffe34'), + ) + dataF = ( + ('abcd', 'd2cba'), + ('2354', '1234'), + ('dcw4f', 'dcw5f'), + ) + + def test_cp(self): + # true check + for test_strings in self.dataT: + result = check_permutation(*test_strings) + self.assertTrue(result) + # false check + for test_strings in self.dataF: + result = check_permutation(*test_strings) + self.assertFalse(result) + + +if __name__ == "__main__": + unittest.main() diff --git a/Chapter 1/3_URLify/URLify.py b/Chapter1/3_URLify/URLify.py similarity index 100% rename from Chapter 1/3_URLify/URLify.py rename to Chapter1/3_URLify/URLify.py diff --git a/Chapter 1/4_Palindrome Permutation/PalindromePermutation.py b/Chapter1/4_Palindrome Permutation/PalindromePermutation.py similarity index 100% rename from Chapter 1/4_Palindrome Permutation/PalindromePermutation.py rename to Chapter1/4_Palindrome Permutation/PalindromePermutation.py diff --git a/Chapter 1/5_One Away/OneAway.py b/Chapter1/5_One Away/OneAway.py similarity index 100% rename from Chapter 1/5_One Away/OneAway.py rename to Chapter1/5_One Away/OneAway.py diff --git a/Chapter 1/6_String Compression/StringCompression.py b/Chapter1/6_String Compression/StringCompression.py similarity index 100% rename from Chapter 1/6_String Compression/StringCompression.py rename to Chapter1/6_String Compression/StringCompression.py diff --git a/Chapter 1/7_Rotate Matrix/RotateMatrix.py b/Chapter1/7_Rotate Matrix/RotateMatrix.py similarity index 100% rename from Chapter 1/7_Rotate Matrix/RotateMatrix.py rename to Chapter1/7_Rotate Matrix/RotateMatrix.py diff --git a/Chapter 1/8_Zero Matrix/ZeroMatrix.py b/Chapter1/8_Zero Matrix/ZeroMatrix.py similarity index 100% rename from Chapter 1/8_Zero Matrix/ZeroMatrix.py rename to Chapter1/8_Zero Matrix/ZeroMatrix.py diff --git a/Chapter 1/9_String Rotation/StringRotation.py b/Chapter1/9_String Rotation/StringRotation.py similarity index 100% rename from Chapter 1/9_String Rotation/StringRotation.py rename to Chapter1/9_String Rotation/StringRotation.py diff --git a/Chapter 2/1_Remove_Dups.py b/Chapter2/1_Remove_Dups.py similarity index 100% rename from Chapter 2/1_Remove_Dups.py rename to Chapter2/1_Remove_Dups.py diff --git a/Chapter 2/2_Return_Kth_To_Last.py b/Chapter2/2_Return_Kth_To_Last.py similarity index 100% rename from Chapter 2/2_Return_Kth_To_Last.py rename to Chapter2/2_Return_Kth_To_Last.py diff --git a/Chapter 2/3_Delete_Middle_Node.py b/Chapter2/3_Delete_Middle_Node.py similarity index 100% rename from Chapter 2/3_Delete_Middle_Node.py rename to Chapter2/3_Delete_Middle_Node.py diff --git a/Chapter 2/4_Partition.py b/Chapter2/4_Partition.py similarity index 100% rename from Chapter 2/4_Partition.py rename to Chapter2/4_Partition.py diff --git a/Chapter 2/5_Sum_Lists.py b/Chapter2/5_Sum_Lists.py similarity index 100% rename from Chapter 2/5_Sum_Lists.py rename to Chapter2/5_Sum_Lists.py diff --git a/Chapter 2/6_Palindrome.py b/Chapter2/6_Palindrome.py similarity index 100% rename from Chapter 2/6_Palindrome.py rename to Chapter2/6_Palindrome.py diff --git a/Chapter 2/7_Intersection.py b/Chapter2/7_Intersection.py similarity index 100% rename from Chapter 2/7_Intersection.py rename to Chapter2/7_Intersection.py diff --git a/Chapter 2/8_Loop_Detection.py b/Chapter2/8_Loop_Detection.py similarity index 100% rename from Chapter 2/8_Loop_Detection.py rename to Chapter2/8_Loop_Detection.py diff --git a/Chapter 2/LinkedList.py b/Chapter2/LinkedList.py similarity index 100% rename from Chapter 2/LinkedList.py rename to Chapter2/LinkedList.py diff --git a/Chapter4/README.md b/Chapter4/README.md new file mode 100644 index 00000000..e69539d3 --- /dev/null +++ b/Chapter4/README.md @@ -0,0 +1 @@ +# Trees and graphs diff --git a/README.md b/README.md new file mode 100644 index 00000000..8ba92013 --- /dev/null +++ b/README.md @@ -0,0 +1,9 @@ +# Python Solutions to *Cracking the Coding Interview, 6th Edition* + +This is a **Python** solutions for the book [Cracking the Coding Interview, 6th Edition](https://www.careercup.com/book) by *Gayle Laakmann McDowell*. + +## How to use? + +To run the programs, just use the `python ChapterX/filename.py` command. + +The test cases are included in the solution files. diff --git a/readme.md b/readme.md deleted file mode 100644 index 7f913ae8..00000000 --- a/readme.md +++ /dev/null @@ -1,5 +0,0 @@ -##Python Solutions to Cracking the Coding Interview 6th Edition - -To run the programs, just use the `python filename.py` command. - -The test cases are included in the solution files. \ No newline at end of file