Explore 1.5M+ audiobooks & ebooks free for days

Only $12.99 CAD/month after trial. Cancel anytime.

Programming Puzzles: Python Edition: The Guide to Sharpen Your Coding Skills with Engaging and Challenging Puzzles
Programming Puzzles: Python Edition: The Guide to Sharpen Your Coding Skills with Engaging and Challenging Puzzles
Programming Puzzles: Python Edition: The Guide to Sharpen Your Coding Skills with Engaging and Challenging Puzzles
Ebook498 pages1 hour

Programming Puzzles: Python Edition: The Guide to Sharpen Your Coding Skills with Engaging and Challenging Puzzles

Rating: 0 out of 5 stars

()

Read preview
LanguageEnglish
PublisherPackt Publishing
Release dateJun 6, 2024
ISBN9781836200307
Programming Puzzles: Python Edition: The Guide to Sharpen Your Coding Skills with Engaging and Challenging Puzzles
Author

Matthew Whiteside

Matthew Whiteside is a composer and Artistic Director of The Night With... concerts based in Glasgow. He was one of The List's Hot 100 for 2019 and in 2020 was named 'One to Watch Classical' by The Scotsman. He won the Scottish Music Industry Award for Creative Programming at the Scottish Awards for New Music for the 2019 season of The Night With..., and in 2021 won the Award for the Recording of New Music sponsored by VoxCarynx for his work on The Night With... Live Vol. One.His 2019 album Entangled, featuring three string quartets with electronic interludes, was described as "effective and unsettling" by BBC Music Magazine and as "post-minimal bold sparseness" by The Herald. Arcana.fm said, "It is refreshing to encounter a composer making albums with new classical music in this way, for when used imaginatively the format still has much to give".Other works include Night Thoughts, commissioned by Crash Ensemble for New Music Dublin and shortlisted in the 2021 Scottish Awards for New Music; the short opera, Little Black Lies, commissioned by Scottish Opera Connect, with libretto by Helene Grøn; Quartet No. 4 (Entangled), commissioned by the Institute of Physics for the Northern Ireland Science Festival; This Too Shall Pass, for Juice Vocal Ensemble; and Ground, Air, Life, commissioned by the Glasgow Barons.Matthew also regularly lectures on music, composition, sound design, and the music industry as a whole. His focus is on promoting a DIY mentality within the art.Along with his concert compositions, he composed the music for Michael Palin's Quest for Artemisia, broadcast on BBC 4, and has scored two feature films (Anna Unbound and The Loudest Sound) along with numerous shorts. His music was also used in Citizens Theatre production, The Macbeths. Matthew is also an avid Scuba diver who frequently shares his underwater experiences on social media.www.matthewwhiteside.co.uk

Related to Programming Puzzles

Related ebooks

Programming For You

View More

Reviews for Programming Puzzles

Rating: 0 out of 5 stars
0 ratings

0 ratings0 reviews

What did you think?

Tap to rate

Review must be at least 10 words

    Book preview

    Programming Puzzles - Matthew Whiteside

    Getting Started

    Welcome to the Programming Puzzles: Python Edition! Before you dive into the world of Python puzzles, let's cover a few essential points to ensure you have a smooth and enjoyable experience.

    Introduction

    The puzzles in this book have specifically been designed so that people of all levels can attempt them. You’ll find some puzzles for people who are earlier along in their Python journey, and you’ll find harder puzzles for people who are more experienced.

    The book is broken up into two main sections, challenge puzzles and fun puzzles. The challenge puzzles section contains 50 ( + a few bonus) puzzles that start off at beginner level and get harder as you progress. This is the main section of the book and it’s recommended you start on puzzle #1 and work your way through.

    The fun puzzles section focuses more around being creative and making use of multiple libraries that Python offers. This section is recommended if you want a break from the challenge puzzles, although they still aren’t easy!

    Note: it’s recommended you know at least the basics of Python before starting any puzzles from this book. Familiarity with fundamental concepts such as variables, conditionals, loops, and functions will greatly benefit you in solving the puzzles.

    A note on formatting…

    This version of the book has been formatted specifically for kindle. Each section is designed so it’s as easy as possible to work with and complete the puzzles e.g. the code won’t split over two pages.

    Due to this formatting some of the pages might look a bit empty, but this is intended. Splits half way through puzzles are just annoying!

    Environment Setup

    It’s important to get a working environment setup before you begin the puzzles - otherwise you won’t be able to test your solutions! Luckily Python is pretty straightforward to get working.

    Python Installation

    Python installation will be different for everyone as it depends on your operating system, however as a general guide you can use the following steps:

    Visit the official Python website at python.org

    Navigate to the Downloads section.

    Choose the appropriate installer for your operating system (Windows, macOS, or Linux) and select a version of python that is 3.10 or greater.

    Download the installer and follow the instructions to complete the installation. If the installer asks if you’d like to install the pip package manager say yes.

    After the installation is complete, you can open a command prompt or terminal and type python --version to verify that Python is installed correctly. It should display the version number you installed. If the version number isn’t correct you can try python3 --version or python3.10 --version.

    Code Editor

    You will also need a code editor to edit and execute python code, there are many options out there but a few of the mainstream ones are:

    IDLE - comes bundled with Python.

    Visual Studio Code - code.visualstudio.com

    PyCharm - jetbrains.com/pycharm

    In the scope of this book any of these editors will do the job, however as you continue your Python journey it’s beneficial to move towards a more powerful IDE such as Visual Studio Code or PyCharm.

    External Python Libraries

    A small number of puzzles make use of external Python libraries. These can be installed using Python’s package manager, pip.

    You can check if you have pip by executing python -m pip --version in your command prompt / terminal. You should use the same python prefix as you did above, so if python3 --version worked for you above then you’d do python3 -m pip --version to check your pip version.

    You can read more about pip at docs.python.org/3/installing/index.html

    The external modules we use for the puzzles in this book can be installed by executing the following pip commands in your command prompt / terminal:

    python -m pip install pygame

    python -m pip install PythonTurtle

    Git (optional)

    This book comes with a provided git repository that contains starter code and solutions for each puzzle. This can be downloaded directly from github however the proper way is to clone it using a tool called git. If you’re familiar with git, you can clone the repository as follows and then skip this section:

    HTTPS: git clone https://github.com/MatWhiteside/python-puzzle-book.git

    SSH: git clone [email protected]:MatWhiteside/python-puzzle-book.git

    If you’re unfamiliar with git you’ll first need to install it on your computer, I won’t cover it here as it can be different depending on your operating system but there are many guides that you’ll be able to follow online.

    If you can’t get git to work it’s not a big problem. Simply go to the github link in a browser and download the code as a zip file to your computer. You can then extract the file to a directory of your choosing and you’re ready to go. Link: github.com/MatWhiteside/python-puzzle-book

    Big O Notation

    There are a number of harder puzzles in this book that make reference to the time or space complexity of a problem. An example requirement could read like ensure your solution has a maximum time complexity of O(n) - but what does this mean?

    The Big O notation is often used in identifying how complex a problem is and defines the worst case complexity for a particular piece of code. The Big O notation is written as: O(...).

    Examples

    O(1), also known as constant, indicates that the given code will always take the exact same amount of time regardless of the input. A constant function could look like:

    If the input_num = 0 or the input_num = 99999 the code still only has to execute one line to carry out the addition. Therefore, we have a constant time complexity.

    O(n), also known as linear, indicates that the complexity increases with our input size. Our input is represented by the variable n. Let’s take a look at what a linear function could look like:

    If the input_list has a length of 5, print(item) will execute 5 times. If the input_list has a length of 9999, print(item) will execute 9999 times. Therefore, we have a linear time complexity.

    O(n²), also known as quadratic, indicates that the time complexity increases by our input squared as our input size increases. Let’s take a look:

    If the input_list has a length of 5, print(item, + , inner_item) will execute 25 times. If the input_list has a length of 1000, print(item, + , inner_item) will execute 1,000,000 times.

    If you’re confused, don’t worry. There aren’t mentions of the Big O notation until later in the book, and when you get there if you still need help you’ll be able to find lots of resources online. Just google something along the lines of Big O notation explained.

    Final Points Before Starting…

    Hopefully you’re now fully set up and ready to start solving puzzles! There are just a few final points to note before you get going…

    It doesn’t matter if the solutions to your puzzles break for bad inputs. We’re not writing production code here, if a puzzle states that the input will be a list of integers then assume that you will receive a list of integers. Do however think about edge cases e.g. what if the input list is empty? What if the input number is negative?

    All of the challenge puzzles have starter code that’s designed to get you going quickly. Most of them have just one function defined, however that doesn’t mean you can’t define more functions yourself. It’s only a guide, you can even choose to not use the starter code at all; it’s up to you!

    A number of puzzles have caveats defined, these must be met to successfully complete the puzzle. If you’re struggling, attempt the puzzle without the caveats met and then work out how you could improve your solution to meet them after.

    Any code that is provided makes use of typing in the function definitions to make it obvious what the inputs and output of the function are meant to be. If you’re not familiar with typing in python, don’t worry it doesn’t change anything from your side.

    In fact, if you really don’t like the typing, feel free to delete it from the function definitions and carry on as usual!

    Example with typing:

    Example with typing removed:

    Challenge Puzzles

    Welcome to the first section of the book, challenge puzzles. These puzzles are designed to start off easy and increase in difficulty as you work your way through.

    Each puzzle will describe a task and ask you to implement a function to solve the task. To allow you to focus on the important part there is also starter code provided so you can get straight into

    Enjoying the preview?
    Page 1 of 1