avatar Bite 260. Let's play with Pandas DataFrames

To this point in the Pandas learning path we have just looked at Series. DataFrames are only a small step up in complexity. Instead of looking at Column A of your Excel Spreadsheet, we are now going to look at multiple columns of data.

Before we start, time for some Pandas revision. This introductory tutorial on Pandas DataFrames, Python Pandas Tutorial: A Complete Introduction for Beginners, is, in my opinion, one of the best there is out there. So, rather then spend time reinventing the wheel, if you are still learning about Pandas you should take an hour and read this tutorial start to end and follow along. You really will nearly learn everything you need to know for this Bite. And if that's not enough one of my favourite YouTube Python Vloggers has just startd a new Pandas Tutorials Series. Might not have the information yet tht you'll need to complete this Bite but well worth watching now and in the future as more videos are added.

Christmas Lights

Full disclosure here. This Bite is based on a challenge that I completed on the excellent Advent of Code Site. If you want to tackle the original, it appeared on Day 06, 2015.

The basic premise for this Bite is that you are to create a customisable square grid of light. You are given instuructions to:

  1. Create a grid of the given size (always the first instruction and lights are always initially off)
  2. Turn on specified lights (initially turn them on at intensity 1)
  3. Turn off specified lights (set to 0)
  4. Turn up specified lights by given amount (lights max out at 5)
  5. Turn down specified lights by given amount (don't go below 0)
  6. Toggle lights (on lights to off, off lights to intensity 3)
  7. Turning on a light that is already on does nothing, the intensity does not change
  8. Turning off a light that is already off has no affect on the light

Some other details you'll require:

  • The grid is 0 indexed so, for example if you are asked to created a grid of size 5 the row indexes are [0, 1, 2, 3, 4] and likewise the column indexes are also [0, 1, 2, 3, 4]
  • The top left corner of the 5 x 5 grid above is position (0, 0). The bottom right hand corner of the grid is position (4, 4). The middle light in the grid is position (2, 2)
  • When parsing an instruction, the lights to apply the operation to are given in (top left position, bottom right position). So, for example, if the instruction is "turn on 1,1 through 3,3" then the 9 lights in the center of the grid are turned on at intensity 1
  • The top row of the lights is referred to as "0,0 through 0,4
  • The middle column of the lights is referred to as "0,2 through 4,2"
  • The previous two points might seem counter intuitive but believe me, it should help you to think like this. Don't think x,y coordinates, think row and columns. For example think of "turn up 3 0,2 through 4,2" as turn up 3 row 0 column 2 through row 4 column 2

The Test Cases will contain some sample lists of instructions for you to play with but the primary challenge is to take the list of given instructions and run main and get the same answer I do, 12317.

Here are some example instructions and their cumlulative affect on a small grid of lights.

lights samples

This example above is given as one of the test cases.

Login and get coding
go back Advanced level
Bitecoin 4X

26 out of 28 users completed this Bite.
Will you be Pythonista #27 to crack this Bite?
Resolution time: ~127 min. (avg. submissions of 5-240 min.)
Pythonistas rate this Bite 8.0 on a 1-10 difficulty scale.
» Up for a challenge? 💪

We use Python 3.8