From fbd29773cb7814a0a0f34206efff5f21b51132f7 Mon Sep 17 00:00:00 2001 From: Vijayalaxmi Wakode Date: Fri, 4 Oct 2024 03:26:22 +0530 Subject: [PATCH 01/10] The string manipulation - replace() --- strings/replace.py | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 strings/replace.py diff --git a/strings/replace.py b/strings/replace.py new file mode 100644 index 000000000000..a22d70e76b97 --- /dev/null +++ b/strings/replace.py @@ -0,0 +1,21 @@ +def string_replace(text: str, input_string: str, replace_with_string: str, occurrence: int) -> str: + """ + The replace() method replaces a specified string with another specified string. + The occurence parameter can be skipped in order to consider all text. + Note: input and replace_with strings are case-sensitive. + >>> text = "One Two Two Three Four Five" + >>> string_val = string_replace(text, "Two", "Seven", 1) + >>> print(string_val) + One Seven Two Three Four Five + + >>> text = "In the morning, the cat is running behind the mouse." + >>> string_val = string_replace(text, "the", "a", 10) + >>> print(string_val) + In a morning, a cat is running behind a mouse. + """ + return text.replace(input_string, replace_with_string, occurrence) + + +if __name__ == "__main__": + from doctest import testmod + testmod() From 19db3e369ffd0c4c8a1420f6f7fb9895b5ed6a0f Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Thu, 3 Oct 2024 21:57:56 +0000 Subject: [PATCH 02/10] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- strings/replace.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/strings/replace.py b/strings/replace.py index a22d70e76b97..9b2494c3a81b 100644 --- a/strings/replace.py +++ b/strings/replace.py @@ -1,4 +1,6 @@ -def string_replace(text: str, input_string: str, replace_with_string: str, occurrence: int) -> str: +def string_replace( + text: str, input_string: str, replace_with_string: str, occurrence: int +) -> str: """ The replace() method replaces a specified string with another specified string. The occurence parameter can be skipped in order to consider all text. @@ -18,4 +20,5 @@ def string_replace(text: str, input_string: str, replace_with_string: str, occur if __name__ == "__main__": from doctest import testmod + testmod() From ce03ae68226579d676cbbe71d4f7d5104f7b94d9 Mon Sep 17 00:00:00 2001 From: Vijayalaxmi Wakode Date: Fri, 4 Oct 2024 03:35:06 +0530 Subject: [PATCH 03/10] Update replace.py --- strings/replace.py | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/strings/replace.py b/strings/replace.py index 9b2494c3a81b..967e251f2c69 100644 --- a/strings/replace.py +++ b/strings/replace.py @@ -1,9 +1,8 @@ -def string_replace( - text: str, input_string: str, replace_with_string: str, occurrence: int -) -> str: +def string_replace(text: str, input_string: str, replace_with_string: str, occurrence: int) -> str: """ + https://docs.python.org/3/library/stdtypes.html#str.replace The replace() method replaces a specified string with another specified string. - The occurence parameter can be skipped in order to consider all text. + The occurrence parameter can be skipped in order to consider all text. Note: input and replace_with strings are case-sensitive. >>> text = "One Two Two Three Four Five" >>> string_val = string_replace(text, "Two", "Seven", 1) @@ -20,5 +19,4 @@ def string_replace( if __name__ == "__main__": from doctest import testmod - testmod() From b4ec916773448bbd4cde08a6caa54d119984eed4 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Thu, 3 Oct 2024 22:05:57 +0000 Subject: [PATCH 04/10] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- strings/replace.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/strings/replace.py b/strings/replace.py index 967e251f2c69..16ef7cbba420 100644 --- a/strings/replace.py +++ b/strings/replace.py @@ -1,4 +1,6 @@ -def string_replace(text: str, input_string: str, replace_with_string: str, occurrence: int) -> str: +def string_replace( + text: str, input_string: str, replace_with_string: str, occurrence: int +) -> str: """ https://docs.python.org/3/library/stdtypes.html#str.replace The replace() method replaces a specified string with another specified string. @@ -19,4 +21,5 @@ def string_replace(text: str, input_string: str, replace_with_string: str, occur if __name__ == "__main__": from doctest import testmod + testmod() From 9f6633c9b951bc36bf0ba68b495c18ab4c2925bf Mon Sep 17 00:00:00 2001 From: vijayalaxmi777 Date: Mon, 14 Oct 2024 18:39:06 +0000 Subject: [PATCH 05/10] updating DIRECTORY.md --- DIRECTORY.md | 1 + 1 file changed, 1 insertion(+) diff --git a/DIRECTORY.md b/DIRECTORY.md index f0a34a553946..b3891a3cbe8c 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -1314,6 +1314,7 @@ * [Prefix Function](strings/prefix_function.py) * [Rabin Karp](strings/rabin_karp.py) * [Remove Duplicate](strings/remove_duplicate.py) + * [Replace](strings/replace.py) * [Reverse Letters](strings/reverse_letters.py) * [Reverse Words](strings/reverse_words.py) * [Snake Case To Camel Pascal Case](strings/snake_case_to_camel_pascal_case.py) From ad5119c50c5b47499c22b00a5c493fcfbc0ff5d5 Mon Sep 17 00:00:00 2001 From: Vijayalaxmi Wakode Date: Tue, 15 Oct 2024 00:13:19 +0530 Subject: [PATCH 06/10] Create read_file() --- file_handling/read_file() | 1 + 1 file changed, 1 insertion(+) create mode 100644 file_handling/read_file() diff --git a/file_handling/read_file() b/file_handling/read_file() new file mode 100644 index 000000000000..8b137891791f --- /dev/null +++ b/file_handling/read_file() @@ -0,0 +1 @@ + From 63e6784011028425a78c667a690eed8a0fcee1f5 Mon Sep 17 00:00:00 2001 From: Vijayalaxmi Wakode Date: Tue, 15 Oct 2024 00:26:42 +0530 Subject: [PATCH 07/10] Update and rename read_file() to read_file.py --- file_handling/read_file() | 1 - file_handling/read_file.py | 16 ++++++++++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) delete mode 100644 file_handling/read_file() create mode 100644 file_handling/read_file.py diff --git a/file_handling/read_file() b/file_handling/read_file() deleted file mode 100644 index 8b137891791f..000000000000 --- a/file_handling/read_file() +++ /dev/null @@ -1 +0,0 @@ - diff --git a/file_handling/read_file.py b/file_handling/read_file.py new file mode 100644 index 000000000000..fcc1d57982b5 --- /dev/null +++ b/file_handling/read_file.py @@ -0,0 +1,16 @@ +# Relative path +def read_file(): + fp = open(r'test_file.txt', 'r') + # read file + text = (fp.read()) + # Closing the file after reading + fp.close() + """ + >>> read_file() + >>> print("Hello World!") + """ + return text + +if __name__ == __main__: + from doctest import testmod + testmod() From 4e87228caff0ea4b628213f44fc4b6a01762cdf7 Mon Sep 17 00:00:00 2001 From: vijayalaxmi777 Date: Mon, 14 Oct 2024 18:56:55 +0000 Subject: [PATCH 08/10] updating DIRECTORY.md --- DIRECTORY.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/DIRECTORY.md b/DIRECTORY.md index b3891a3cbe8c..6a536d3fd640 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -424,6 +424,9 @@ * [Resonant Frequency](electronics/resonant_frequency.py) * [Wheatstone Bridge](electronics/wheatstone_bridge.py) +## File Handling + * [Read File](file_handling/read_file.py) + ## File Transfer * [Receive File](file_transfer/receive_file.py) * [Send File](file_transfer/send_file.py) From 707e325bc6516292b0d751016b6f29bee521c0e9 Mon Sep 17 00:00:00 2001 From: Vijayalaxmi Wakode Date: Tue, 15 Oct 2024 00:27:12 +0530 Subject: [PATCH 09/10] Create test_file.txt --- file_handling/test_file.txt | 1 + 1 file changed, 1 insertion(+) create mode 100644 file_handling/test_file.txt diff --git a/file_handling/test_file.txt b/file_handling/test_file.txt new file mode 100644 index 000000000000..980a0d5f19a6 --- /dev/null +++ b/file_handling/test_file.txt @@ -0,0 +1 @@ +Hello World! From aa980a545b3ba365f59504f81692ebcec213f17c Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Mon, 14 Oct 2024 18:58:06 +0000 Subject: [PATCH 10/10] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- file_handling/read_file.py | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/file_handling/read_file.py b/file_handling/read_file.py index fcc1d57982b5..3a2aeec47883 100644 --- a/file_handling/read_file.py +++ b/file_handling/read_file.py @@ -1,16 +1,18 @@ # Relative path def read_file(): - fp = open(r'test_file.txt', 'r') - # read file - text = (fp.read()) - # Closing the file after reading - fp.close() - """ + fp = open(r"test_file.txt", "r") + # read file + text = fp.read() + # Closing the file after reading + fp.close() + """ >>> read_file() >>> print("Hello World!") """ - return text + return text + if __name__ == __main__: - from doctest import testmod - testmod() + from doctest import testmod + + testmod()