Here is some sample problems to practice. please note that, problems are typical but can be solved in a compact way with python. try to find a pythonic way with python list and string comprehensions.
-
Reverse a String : Enter a string and the program will reverse it and print it out. sample input: "Hello" output: "olleH"
''' def reverse(some_seq): return some_seq[::-1] '''
-
Count Vowels: Enter a string and the program counts the number of each vowels in the text. sample input: "This is a sample input" output: a=2, e=0, i=3, o=0, u=1
''' def count_vowels(string): vowels = "aeiou" count = {char:0 for char in vowels} for char in string: if char in vowels: count[char] += 1 return count '''
-
Count words: Count number of individual words in a string: sample input: "hello world hello" output: hello = 2 world = 1
''' def count_words(text=None): count = {} for word in text.split(): try: count[word] += 1 except KeyError: count[word] = 1 return count '''
-
Palindrome: Check if a string is palindrome sample input: "Live not on Evil" output: True
''' def is_palindrome(some_seq): formatted_str = "".join(some_seq.split()).lower().replace("'","") return formatted_str == reverse(formatted_str) '''
-
Drop all negetive numbers from a list: return a new list removing all negetive numbers. sample input: [1, 2, -3, 0, -4, 5] output: [1,2,0,5]
''' def non_negetive(list): new_list = [i for i in list if i>=0] return new_list '''
-
Pick only non-palindromes from a list of words. given a list of words, construct a new list droping palindromes sample input: ["hello", "adda", "rotator", "python"] output: ["hello", "python"]
''' def non_palindromes(list): new_list = [word for word in list if is_palindrome(word)] '''