14 votes

Python challenges or projects with just the standard library?

I've been slowly learning python for some months already. I used the Python Crash Course book from No Starch Press, it teaches the basics and then goes on with some projects with pygame, matplotlib, etc.

However, I feel that my Python skills aren't very good yet, and before learning to use libraries I would like to have a better command of the standard library.

I have been looking for some book with projects or, even better, challenges using just the standard library, but haven't found any good ones. Most of them either are for absolute beginners, or use additional libraries, or are very technical and without focus on practice.

Do you know of any good book or resource with challenges or projects that don't depend on additional libraries? Or, do you have any idea for a project or challenge using just the standard library?

Thanks in advance!

16 comments

  1. [6]
    aymm
    Link
    I usually go to Advent of Code or Project Euler to tinker with a new language

    I usually go to Advent of Code or Project Euler to tinker with a new language

    8 votes
    1. [5]
      chembliss
      Link Parent
      Advent of Code seems to be what I was looking for! Regarding Project Euler, I'm not very strong at mathematics, but at least the first problems seem to not require very advanced maths. Thank you!

      Advent of Code seems to be what I was looking for! Regarding Project Euler, I'm not very strong at mathematics, but at least the first problems seem to not require very advanced maths. Thank you!

      4 votes
      1. [2]
        gpl
        Link Parent
        Also check out Project Lovelace for more science-y oriented challenges.

        Also check out Project Lovelace for more science-y oriented challenges.

        5 votes
        1. chembliss
          Link Parent
          That one seems really fun, I like that they also have a "time sink" rating haha.

          That one seems really fun, I like that they also have a "time sink" rating haha.

          2 votes
      2. nsz
        Link Parent
        I'm garbage at maths but I use project Euler--partly to get better at it--but mainly to learn C and then when I saw how much easier it was Python. I started from the first problem and worked my...

        I'm garbage at maths but I use project Euler--partly to get better at it--but mainly to learn C and then when I saw how much easier it was Python.

        I started from the first problem and worked my way up from there. At least in the beginning, highschool maths is sufficient and it's not like you can't reserch things just avoide the phrasing used in the question. Over all I recon it was the best tool I tried, mind you it's just a hobby for me.

        The freedom to approach the problem as you want is very much what forces the learning process. The forum you access once the question is correctly awnsered is in valuble. You've already spent time thinking about and have your own solution so reading others work--and sometimes an official pdf awnser--is far more relevant.
        As an example it took my laptop 20 mins to calculate an awnser to a question involving primes, I felt pretty proud, it was my solution after all. Once I'd read the official awnser and a quick rewrite; it solved in under a second. Sure if I'd known my basic maths I'd have used the simpler and more efficient method but what can you do, at least I got the awnser in the end rather having it handed to me as part of a pre-planned exercise with each step broken down in absurd minutia.

        3 votes
      3. aymm
        Link Parent
        Math isn't my strong suit either! A lot of them are solvable with basic math, but can be improved/get better performance if you know more advanced math

        Math isn't my strong suit either! A lot of them are solvable with basic math, but can be improved/get better performance if you know more advanced math

        1 vote
  2. [2]
    pvik
    Link
    I think python koans might help too.

    I think python koans might help too.

    4 votes
    1. chembliss
      Link Parent
      Thank you! Looks promising.

      Thank you! Looks promising.

      2 votes
  3. [8]
    spit-evil-olive-tips
    Link
    Writing a Sudoku solver is a fun exercise that wouldn't require any extra batteries beyond the stdlib. Also on the puzzle theme, you could build a simple little hangman game. Read in an English...

    Writing a Sudoku solver is a fun exercise that wouldn't require any extra batteries beyond the stdlib.

    Also on the puzzle theme, you could build a simple little hangman game. Read in an English dictionary, pick a word at random, and have the user guess letters until they figure it out or die.

    Reading through the list of stdlib modules, another one that comes to mind is an fdupes clone. Point it at a directory, it figures out if any files are copies of each other (by contents, not by name). That could be done just with os.walk and other filesystem access methods in the stdlib, plus the builtin datastructures.

    1 vote
    1. [7]
      chembliss
      Link Parent
      The hangman game seems like would be easy enough, maybe I get started with something like that. The Sudoku one could be interesting as well, probably getting an optimal solution (and not just kind...

      The hangman game seems like would be easy enough, maybe I get started with something like that. The Sudoku one could be interesting as well, probably getting an optimal solution (and not just kind of bruteforce-ish one) will be challenging.
      Thanks!

      1 vote
      1. [5]
        spit-evil-olive-tips
        Link Parent
        As @stu2b50 mentioned, there's no One Weird Trick for quickly solving Sudoku, only brute-force. On a 9x9 grid it basically doesn't matter, any solvable puzzle should be solved in under a second on...

        As @stu2b50 mentioned, there's no One Weird Trick for quickly solving Sudoku, only brute-force. On a 9x9 grid it basically doesn't matter, any solvable puzzle should be solved in under a second on modern hardware.

        Where the problem gets interesting is when you start trying to do more complicated deductions. This is assuming you try to implement your solver in a "naive" way - that is, you write code that does more or less the same things you would do if you were trying to solve it by hand. That's what I'd recommend for your first implementation of trying to solve this, at least. As stu2b50 mentioned, you can also do backtracking search (the manual equivalent of this would be "suppose this cell had a 1...would that cause any contradictions? no...OK, then suppose this cell had a 1 and the cell next to it had a 7...") or using a full-blown constraint solver. Once you get more comfortable with using 3rd party libraries python-constraint looks fun.

        The low-hanging fruit are rules like "if a cell has X as its only possibility, then assign it X" and "if a row/column/box has only one cell where X is a possibility, then assign X to that cell". Those two alone will solve almost any puzzle you can find with an "easy" difficulty rating.

        Where it gets more fun is things like "if two cells in the same row/column/box have X and Y as their only possibilities, then X and Y can be removed as possibilities from other cells in that row/column/box". You can go quite far down this rabbit hole, see X-wing for an example.

        2 votes
        1. [4]
          chembliss
          Link Parent
          I didn't understand @stu2b50 's comment because my maths are slightly above high school level (I started a computer science degree but dropped from it after the first semester), and I figured that...

          I didn't understand @stu2b50 's comment because my maths are slightly above high school level (I started a computer science degree but dropped from it after the first semester), and I figured that I'll learn about that when the time comes. Your comment, I understand better. I have started with some Project Euler challenges, but now you got me thinking about the Sudoku solver, and it's way more interesting that I had considered before. So maybe in a couple of days I'll start with it. Only I'm a bit intimidated haha.

          1 vote
          1. [3]
            spit-evil-olive-tips
            Link Parent
            Start off small, break the problem into bite-size chunks, tackle them one at a time. Now would be a great time to introduce yourself to unit testing (also available in the standard library) since...

            Only I'm a bit intimidated haha.

            Start off small, break the problem into bite-size chunks, tackle them one at a time.

            Now would be a great time to introduce yourself to unit testing (also available in the standard library) since solving a Sudoku puzzle has very well-defined inputs and outputs which makes it very easy to test. Those tests are how you convince yourself that your code isn't broken as you're working on it.

            For example, your code is going to have to read in the state of an unsolved board, parse it into some sort of internal representation, and solve it. You can write a test that feeds it this:

            123 456 78.
            ... ... ...
            ... ... ...
            
            ... ... ...
            ... ... ...
            ... ... ...
            
            ... ... ...
            ... ... ...
            ... ... ...
            

            The solver should be able to figure out that 9 goes in to the upper-right. After it figures that out, it realizes it's not making any further progress solving the rest of the board and gives up. So it hands the (partially) solved puzzle back to the caller (the unit test in this case) which asserts that the upper-right cell has a 9 and the rest is unchanged.

            You can write similar tests for a column with 1 number missing, and for a 3x3 box as well. Here's another test case, for example, the solver should be able to deduce that 1 belongs in the upper-left:

            ... ... ...
            ... .1. ...
            ... ... ..1
            
            ... ... ...
            .1. ... ...
            ... ... ...
            
            ... ... ...
            ... ... ...
            ..1 ... ...
            

            From there you can start feeding it real puzzles (websudoku.com is free and useful for this) and see if it solves them correctly.

            2 votes
            1. [2]
              chembliss
              Link Parent
              This is very helpful, thanks! If it's okay with you, can I DM you here when I have a working solution so I can get some feedback?

              This is very helpful, thanks! If it's okay with you, can I DM you here when I have a working solution so I can get some feedback?

              1. spit-evil-olive-tips
                Link Parent
                Sure, though I think it'd also be interesting to post it in ~comp as a topic and see if anyone else has feedback. There's a Code Review StackExchange and I think it'd be interesting to get...

                Sure, though I think it'd also be interesting to post it in ~comp as a topic and see if anyone else has feedback. There's a Code Review StackExchange and I think it'd be interesting to get something similar going on Tildes.

                1 vote
      2. stu2b50
        Link Parent
        You can pretty quickly reduce CSP or 3SAT into a general Sudoku puzzle, i.e Sudoku is NP-hard (and NP-complete, of course). So, uh, bruteforce-ish is about as good as you'll get. Now, that's in...

        probably getting an optimal solution (and not just kind of bruteforce-ish one) will be challenging.

        You can pretty quickly reduce CSP or 3SAT into a general Sudoku puzzle, i.e Sudoku is NP-hard (and NP-complete, of course).

        So, uh, bruteforce-ish is about as good as you'll get.

        Now, that's in the general case. In reality, something like backtrace search will get the solution usually quicker than exponential time.

        1 vote