Question 1: Reverse a String
Write a function reverse_string(s) that takes a string s and returns the string in reverse order.
python
Copy code
def reverse_string(s):
# Your code here
Example:
python
Copy code
reverse_string("hello") # Output: "olleh"
Question 2: Count Vowels
Create a function count_vowels(s) that takes a string s and returns the number of vowels (a, e, i, o, u)
in the string.
python
Copy code
def count_vowels(s):
# Your code here
Example:
python
Copy code
count_vowels("hello world") # Output: 3
Question 3: Palindrome Check
Write a function is_palindrome(s) that checks if the string s is a palindrome (reads the same
backward as forward). The function should return True or False.
python
Copy code
def is_palindrome(s):
# Your code here
Example:
python
Copy code
is_palindrome("racecar") # Output: True
is_palindrome("hello") # Output: False
Question 4: String Compression
Implement a function compress_string(s) that takes a string s and compresses it by replacing
consecutive repeated characters with a count of occurrences. For example, "aaabbc" should become
"a3b2c1".
python
Copy code
def compress_string(s):
# Your code here
Example:
python
Copy code
compress_string("aaabbc") # Output: "a3b2c1"
Question 5: Remove Duplicates
Write a function remove_duplicates(s) that removes duplicate characters from the string s while
maintaining the original order of characters.
python
Copy code
def remove_duplicates(s):
# Your code here
Example:
python
Copy code
remove_duplicates("banana") # Output: "ban"
Feel free to try these out or modify them as needed!
4o mini
You said:
create few python questions based on string
ChatGPT said:
ChatGPT
Sure! Here are some additional Python questions based on string manipulation:
Question 1: Character Frequency
Write a function char_frequency(s) that takes a string s and returns a dictionary where the keys are
characters and the values are the number of times each character appears in the string.
python
Copy code
def char_frequency(s):
# Your code here
Example:
python
Copy code
char_frequency("hello") # Output: {'h': 1, 'e': 1, 'l': 2, 'o': 1}
Question 2: Capitalize Each Word
Create a function capitalize_words(s) that takes a string s and returns the string with the first letter of
each word capitalized.
python
Copy code
def capitalize_words(s):
# Your code here
Example:
python
Copy code
capitalize_words("hello world") # Output: "Hello World"
Question 3: Longest Word
Write a function longest_word(s) that takes a string s and returns the longest word in the string. If
there are multiple words of the same length, return the first one.
python
Copy code
def longest_word(s):
# Your code here
Example:
python
Copy code
longest_word("I love programming in Python") # Output: "programming"
Question 4: Anagram Check
Create a function are_anagrams(s1, s2) that checks if two strings s1 and s2 are anagrams of each
other (i.e., they contain the same characters in a different order).
python
Copy code
def are_anagrams(s1, s2):
# Your code here
Example:
python
Copy code
are_anagrams("listen", "silent") # Output: True
are_anagrams("hello", "world") # Output: False
Question 5: Find Substring
Write a function find_substring(s, substring) that returns the starting index of the first occurrence of
substring in s. If substring is not found, return -1.