杰瑞科技汇

Python Challenge是什么?如何入门?

Of course! A Python challenge can be anything from a simple coding puzzle to a complex project. To give you the best experience, I'll provide a range of challenges, from easy to very hard.

Python Challenge是什么?如何入门?-图1
(图片来源网络,侵删)

Let's start with a classic and practical one that tests several core Python skills.


Challenge: The File Word Counter

Difficulty: Intermediate

Objective: Write a Python script that analyzes a text file and provides a report of the most frequently used words, ignoring common "stop words" (like 'the', 'a', 'is') and punctuation.

Requirements:

  1. Input: The script should accept the path to a .txt file as a command-line argument.
  2. Reading & Cleaning:
    • Read the file's content.
    • Convert all text to lowercase.
    • Remove all punctuation (e.g., ).
  3. Filtering:
    • Create a list of "stop words" to ignore (e.g., ['a', 'an', 'the', 'in', 'on', 'of', 'is', 'it', 'to', 'and', 'for', 'with']).
    • Filter out all words from your text that are in the stop words list.
  4. Counting:

    Count the frequency of each remaining word.

    Python Challenge是什么?如何入门?-图2
    (图片来源网络,侵删)
  5. Output:
    • Print the top 10 most frequent words and their counts, in descending order of frequency.
    • The format should be: word: count

Example

If you have a file named my_document.txt with the following content:

Python is an amazing language. Python is versatile. Learning Python opens many doors.

Running your script should produce an output similar to this:

$ python word_counter.py my_document.txt
python: 3
is: 2
an: 1
amazing: 1
language: 1
versatile: 1
learning: 1
opens: 1
many: 1
doors: 1

Hints & Key Concepts to Use

  • Command-line arguments: Use the sys module, specifically sys.argv, to get the file path from the user.
  • File Reading: Use a with open(...) statement to safely read the file.
  • String Manipulation:
    • str.lower() to convert to lowercase.
    • The string module has a handy string.punctuation constant.
    • You can use str.translate() with str.maketrans() for efficient punctuation removal, or a simple loop/replace.
  • Word Splitting: str.split() is the easiest way to break the text into a list of words.
  • Counting Frequencies: A collections.Counter object is perfect for this. It's a specialized dictionary subclass for counting hashable objects. If you want to do it manually, a standard dictionary works well.
  • Sorting: You'll need to sort the word-count pairs. The sorted() function with a key argument is your friend. dict.items() will give you (key, value) pairs to sort.

Solution (Spoiler Alert!)

Try to solve it yourself first. When you're ready, you can check this example solution.

import sys
import string
from collections import Counter
def main():
    """
    Main function to run the word counter.
    """
    # 1. Get file path from command-line arguments
    if len(sys.argv) != 2:
        print("Usage: python word_counter.py <file_path>")
        sys.exit(1) # Exit with an error code
    file_path = sys.argv[1]
    # Define a list of stop words
    stop_words = [
        'a', 'an', 'and', 'are', 'as', 'at', 'be', 'by', 'for', 'from',
        'has', 'he', 'in', 'is', 'it', 'its', 'of', 'on', 'that', 'the',
        'to', 'was', 'were', 'will', 'with', 'i', 'you', 'your', 'we',
        'this', 'but', 'have', 'or', 'not', 'they', 'them', 'their'
    ]
    try:
        # 2. Read and clean the file
        with open(file_path, 'r', encoding='utf-8') as f:
            # Read the file, make lowercase, and remove punctuation
            text = f.read().lower()
            # Create a translation table to remove punctuation
            translator = str.maketrans('', '', string.punctuation)
            cleaned_text = text.translate(translator)
        # 3. Split into words and filter out stop words
        words = cleaned_text.split()
        filtered_words = [word for word in words if word not in stop_words]
        # 4. Count the frequency of each word
        # A Counter is a dictionary subclass for counting
        word_counts = Counter(filtered_words)
        # 5. Get the top 10 most common words and print them
        # most_common(n) returns a list of the n most common elements and their counts
        top_10_words = word_counts.most_common(10)
        print("--- Top 10 Most Frequent Words ---")
        for word, count in top_10_words:
            print(f"{word}: {count}")
    except FileNotFoundError:
        print(f"Error: The file '{file_path}' was not found.")
        sys.exit(1)
    except Exception as e:
        print(f"An error occurred: {e}")
        sys.exit(1)
if __name__ == "__main__":
    main()

Ready for More Challenges?

Here are a few more ideas at different difficulty levels:

Easy: FizzBuzz

A classic programming interview question.

  • Task: Write a program that prints the numbers from 1 to 100. But for multiples of three, print "Fizz" instead of the number, and for multiples of five, print "Buzz". For numbers which are multiples of both three and five, print "FizzBuzz".

Intermediate: Number Guessing Game

  • Task: Generate a random number between 1 and 100. The user has to guess the number. The program should give hints like "Too high!" or "Too low!" until the user guesses correctly. It should also count the number of attempts.

Hard: Web Scraper for Headlines

  • Task: Write a script that uses a library like requests and BeautifulSoup to fetch the front page of a news website (e.g., CNN, BBC News). Extract the headlines and print them to the console. You'll need to inspect the website's HTML structure to find the right tags.

Very Hard: API Client & Data Visualizer

  • Task: Find a public API (like OpenWeatherMap, JSONPlaceholder, or NASA API). Write a Python script to make a request to the API, parse the JSON response, and then use a library like matplotlib or seaborn to create a simple graph or chart from the data you received.

Let me know which challenge you'd like to try, or if you have a specific area of Python you want to focus on (e.g., OOP, data science, web scraping, etc.)

分享:
扫描分享到社交APP
上一篇
下一篇