Python Program DS
Python Program DS
Algorithm:
Step 1: Define Function to Reverse String: Create reverse_string(s) that returns the
reversed string.
Step 2: Define Function to Count Vowels: Create count_vowels(s) that counts and returns
the number of vowels in s.
Step 3: Define Function to Check Palindrome: Create is_palindrome(s) that checks if s is a
palindrome and returns True or False.
Step 4: Define Function to Convert to Uppercase: Create to_uppercase(s) that converts s to
uppercase and returns it.
Step 5: Define Main Function: Create main() to execute the program.
Step 6: Set Input String: Inside main(), define input_string with a sample string (e.g., "A
man a plan a canal Panama").
Step 7: Call Reverse Function: Call reverse_string(input_string) and store the result
in reversed_str.
Step 8: Call Count Vowels Function: Call count_vowels(input_string) and store the result
in vowel_count.
Step 9: Call Palindrome Check Function: Call is_palindrome(input_string) and store the
result in palindrome_check.
Step 10: Call Uppercase Function: Call to_uppercase(input_string) and store the result
in uppercase_str, then print all results.
ASSIGNMENT
SOURCE CODE:
def reverse_string(s):
"""Return the reversed version of the string."""
return s[::-1]
def count_vowels(s):
"""Return the count of vowels in the string."""
vowels = 'aeiouAEIOU'
count = sum(1 for char in s if char in vowels)
return count
def is_palindrome(s):
"""Check if the string is a palindrome."""
cleaned_string = ''.join(char.lower() for char in s if char.isalnum()) # Remove spaces and
punctuation
return cleaned_string == cleaned_string[::-1]
def to_uppercase(s):
"""Convert the string to uppercase."""
return s.upper()
# Main function to demonstrate the use of the above functions
def main():
input_string = "A man a plan a canal Panama"
print(f"Original String: '{input_string}'")
# Reverse the string
reversed_str = reverse_string(input_string)
print(f"Reversed String: '{reversed_str}'")
# Count vowels
vowel_count = count_vowels(input_string)
print(f"Number of Vowels: {vowel_count}")
# Check if the string is a palindrome
palindrome_check = is_palindrome(input_string)
print(f"Is Palindrome: {palindrome_check}")
# Convert to uppercase
uppercase_str = to_uppercase(input_string)
print(f"Uppercase String: '{uppercase_str}'")
# Run the main function
if __name__ == "__main__":
main()
OUTPUT:
Original String: 'A man a plan a canal Panama'
Reversed String: 'amanaP lanac a nalp a nam A'
Number of Vowels: 10
Is Palindrome: True
Uppercase String: 'A MAN A PLAN A CANAL PANAMA'
PROGRAM FOR TRAVERSING DICTIONARYS IN PYTHON
Aim: write a python program for traversing dictionaries
Algorithm:
Step 1: Create a Dictionary: Define a dictionary my_dict with keys: 'name', 'age', 'city', and
'occupation' with corresponding values.
Step 2: Print Header for Keys: Print "Keys:" to indicate the start of key traversal.
Step 3: Traverse Keys: Use a loop to iterate over each key in my_dict.
Step 4: Print Each Key: Inside the loop, print each key.
Step 5: Print Header for Values: Print "Values:" to indicate the start of value traversal.
Step 6: Traverse Values: Use a loop to iterate over each value in my_dict.values().
Step 7: Print Each Value: Inside the loop, print each value.
Step 8: Print Header for Key-Value Pairs: Print "Key-Value Pairs:" to indicate the start of
key-value pair traversal.
Step 9: Traverse Key-Value Pairs: Use a loop to iterate over each key-value pair
in my_dict.items().
Step 10: Print Each Key-Value Pair: Inside the loop, print each key and its corresponding
value in the format "key: value".
SOURCE CODE:
my_dict = {
'name': 'Alice',
'age': 30,
'city': 'New York',
'occupation': 'Engineer'
}
# Traversing Keys
print("Keys:")
for key in my_dict:
print(key)
# Traversing Values
print("\nValues:")
for value in my_dict.values():
print(value)
# Traversing Key-Value Pairs
print("\nKey-Value Pairs:")
for key, value in my_dict.items():
print(f"{key}: {value}")
OUTPUT
Keys:
name
age
city
occupation
Values:
Alice
30
New York
Engineer
Key-Value Pairs:
name: Alice
age: 30
city: New York
occupation: Engineer