diff --git a/PYTHON_Problems/problem_04.py b/PYTHON_Problems/problem_04.py index dfccf81..2cb3c07 100644 --- a/PYTHON_Problems/problem_04.py +++ b/PYTHON_Problems/problem_04.py @@ -1,6 +1,6 @@ # Input: [4, 1, 9, 3] → Output: 9 -a = list(map(int, input("Enter Your data with sapces").split())) +a = list(map(int, input("Enter Your data with spaces").split())) print(max(a)) diff --git a/beginner/strings/exercise_1_palindrome.py b/beginner/strings/exercise_1_palindrome.py new file mode 100644 index 0000000..caf03f2 --- /dev/null +++ b/beginner/strings/exercise_1_palindrome.py @@ -0,0 +1,25 @@ +""" +Title: Palindrome Checker +Level: Beginner +Task: Write a function is_palindrome(word) that returns True if word is a palindrome, False otherwise. +Examples: + is_palindrome("racecar") -> True + is_palindrome("hello") -> False + is_palindrome("madam") -> True + is_palindrome("") -> True +""" + +def is_palindrome(word: str) -> bool: + return word == word[::-1] + +if __name__ == "__main__": + print(is_palindrome("racecar")) + print(is_palindrome("hello")) + print(is_palindrome("madam")) + print(is_palindrome("")) + + +# ---------------------------------------- +# Author: hygroupseries +# GitHub: https://github.com/hygroupseries +# ---------------------------------------- \ No newline at end of file diff --git a/beginner/strings/exercise_2_anagram.py b/beginner/strings/exercise_2_anagram.py new file mode 100644 index 0000000..7ad300f --- /dev/null +++ b/beginner/strings/exercise_2_anagram.py @@ -0,0 +1,27 @@ +""" +Title: Anagram Checker +Level: Beginner +Task: Write a function are_anagrams(word1, word2) that returns True if the words are anagrams, False otherwise. +Examples: + are_anagrams("listen", "silent") -> True + are_anagrams("hello", "world") -> False + are_anagrams("triangle", "integral") -> True + are_anagrams("", "") -> True +""" + +def are_anagrams(word1: str, word2: str) -> bool: + word1_clean = word1.replace(" ", "").lower() + word2_clean = word2.replace(" ", "").lower() + return len(word1_clean) == len(word2_clean) and sorted(word1_clean) == sorted(word2_clean) + +if __name__ == "__main__": + print(are_anagrams("listen", "silent")) + print(are_anagrams("hello", "world")) + print(are_anagrams("triangle", "integral")) + print(are_anagrams("", "")) + + +# ---------------------------------------- +# Author: hygroupseries +# GitHub: https://github.com/hygroupseries +# ---------------------------------------- \ No newline at end of file diff --git a/beginner/strings/exercise_3_vowel_consonant_counter.py b/beginner/strings/exercise_3_vowel_consonant_counter.py new file mode 100644 index 0000000..de0017c --- /dev/null +++ b/beginner/strings/exercise_3_vowel_consonant_counter.py @@ -0,0 +1,37 @@ +""" +Title: Vowel and Consonant Counter +Level: Beginner +Task: Write a function count_vowels_consonants(text) that returns a tuple (vowel_count, consonant_count). +Examples: + count_vowels_consonants("Hello World") -> (3, 7) + count_vowels_consonants("Python Programming") -> (4, 11) + count_vowels_consonants("A E I O U") -> (5, 0) + count_vowels_consonants("") -> (0, 0) +""" + +def count_vowels_consonants(text: str) -> tuple: + vowels = "aeiouAEIOU" + vowel_count = 0 + consonant_count = 0 + + for char in text: + if char.isalpha(): + if char in vowels: + vowel_count += 1 + else: + consonant_count += 1 + + return (vowel_count, consonant_count) + +if __name__ == "__main__": + print(count_vowels_consonants("Hello World")) + print(count_vowels_consonants("Python Programming")) + print(count_vowels_consonants("A E I O U")) + print(count_vowels_consonants("b c d f g")) + print(count_vowels_consonants("")) + + +# ---------------------------------------- +# Author: hygroupseries +# GitHub: https://github.com/hygroupseries +# ---------------------------------------- \ No newline at end of file diff --git a/beginner/strings/exercise_4_duplicate_remover.py b/beginner/strings/exercise_4_duplicate_remover.py new file mode 100644 index 0000000..7de598b --- /dev/null +++ b/beginner/strings/exercise_4_duplicate_remover.py @@ -0,0 +1,33 @@ +""" +Title: Remove Duplicate Characters +Level: Beginner +Task: Write a function remove_duplicates(text) that returns text with duplicate characters removed while preserving order. +Examples: + remove_duplicates("hello") -> "helo" + remove_duplicates("programming") -> "progamin" + remove_duplicates("mississippi") -> "misp" + remove_duplicates("") -> "" +""" + +def remove_duplicates(text: str) -> str: + seen = set() + result = [] + + for char in text: + if char not in seen: + seen.add(char) + result.append(char) + + return ''.join(result) + +if __name__ == "__main__": + print(remove_duplicates("hello")) + print(remove_duplicates("programming")) + print(remove_duplicates("mississippi")) + print(remove_duplicates("")) + + +# ---------------------------------------- +# Author: hygroupseries +# GitHub: https://github.com/hygroupseries +# ---------------------------------------- \ No newline at end of file diff --git a/beginner/strings/exercise_5_longest_word.py b/beginner/strings/exercise_5_longest_word.py new file mode 100644 index 0000000..b238fcc --- /dev/null +++ b/beginner/strings/exercise_5_longest_word.py @@ -0,0 +1,29 @@ +""" +Title: Find Longest Word +Level: Beginner +Task: Write a function find_longest_word(sentence) that returns the longest word in the sentence. +Examples: + find_longest_word("The quick brown fox jumps") -> "quick" + find_longest_word("Python programming is fun") -> "programming" + find_longest_word("Hello world") -> "Hello" + find_longest_word("") -> "" +""" + +def find_longest_word(sentence: str) -> str: + words = sentence.split() + if not words: + return "" + + return max(words, key=len) + +if __name__ == "__main__": + print(find_longest_word("The quick brown fox jumps")) + print(find_longest_word("Python programming is fun")) + print(find_longest_word("Hello world")) + print(find_longest_word("")) + + +# ---------------------------------------- +# Author: hygroupseries +# GitHub: https://github.com/hygroupseries +# ---------------------------------------- \ No newline at end of file