top of page

Python Practice Set (Part 17): Common Algorithms & Data Processing


Python practice series
Python practice series

Welcome to Part 17 of our Python practice series! You've mastered the syntax, learned the data structures, and can write clean functions. Now it's time to put it all together to solve problems efficiently. This practice set focuses on common algorithms and data processing patterns—the kind of logic puzzles you'll encounter in technical interviews and real-world data manipulation tasks. We'll be finding patterns, reorganizing data, and building foundational algorithmic thinking skills.


1. Find the Minimum and Maximum (Algorithmic Thinking)


You have a list of numbers: data = [23, 5, 67, 1, 10, 30, 89]. Write a function find_min_max that iterates through the list and finds both the smallest and the largest number without using the built-in min() or max() functions. The function should return a tuple containing (minimum, maximum).


2. Word Frequency Counter from a File (File I/O and Dictionaries)


Write a function count_word_frequency that takes a filename as input. The function should:

  1. Read the content of the text file.

  2. Convert all text to lowercase and remove punctuation (e.g., periods, commas).

  3. Count the occurrences of each word.

  4. Return a dictionary where the keys are the words and the values are their frequencies.


3. Grouping Data by Category (Data Aggregation)


You have a list of dictionaries, where each dictionary represents a product. products = [ {'name': 'Laptop', 'category': 'Electronics'}, {'name': 'T-Shirt', 'category': 'Apparel'}, {'name': 'Keyboard', 'category': 'Electronics'}, {'name': 'Jeans', 'category': 'Apparel'} ] Write a function group_by_category that takes this list and returns a new dictionary where the keys are the category names and the values are lists of the product names in that category.


4. Anagram Checker (String Manipulation)


An anagram is a word or phrase formed by rearranging the letters of a different word or phrase. Write a function are_anagrams that takes two strings as input. The function should return True if the two strings are anagrams of each other, and False otherwise. The comparison should be case-insensitive and should ignore spaces.

  • Example: are_anagrams("Listen", "Silent") should return True.

  • Example: are_anagrams("Dormitory", "dirty room") should return True.


5. Inverting a Dictionary (Dictionary Manipulation)


Write a function invert_dictionary that takes a dictionary as input and returns a new dictionary where the keys and values have been swapped. For example, if the input is {'a': 1, 'b': 2}, the output should be {1: 'a', 2: 'b'}.

Challenge: How would you handle the case where multiple keys in the original dictionary have the same value? Your function should group the original keys into a list. For example, {'a': 1, 'b': 2, 'c': 1} should become {1: ['a', 'c'], 2: ['b']}.




Comments


bottom of page