Learn to Code by Solving Problems

Book description

Computers are capable of solving almost any problem when given the right instructions. That’s where programming comes in. This beginner’s book will have you writing Python programs right away. You’ll solve interesting problems drawn from real coding competitions and build your programming skills as you go.

Every chapter presents problems from coding challenge websites, where online judges test your solutions and provide targeted feedback. As you practice using core Python features, functions, and techniques, you’ll develop a clear understanding of data structures, algorithms, and other programming basics. Bonus exercises invite you to explore new concepts on your own, and multiple-choice questions encourage you to think about how each piece of code works.

You’ll learn how to:

•Run Python code, work with strings, and use variables
•Write programs that make decisions
•Make code more efficient with while and for loops
•Use Python sets, lists, and dictionaries to organize, sort, and search data
•Design programs using functions and top-down design
•Create complete-search algorithms and use Big O notation to design more efficient code

By the end of the book, you’ll not only be proficient in Python, but you’ll also understand how to think through problems and tackle them with code. Programming languages come and go, but this book gives you the lasting foundation you need to start thinking like a programmer.

Table of contents

  1. Cover Page
  2. Title Page
  3. Copyright Page
  4. Dedication
  5. About the Author
  6. About the Technical Reviewer
  7. BRIEF CONTENTS
  8. CONTENTS IN DETAIL
  9. ACKNOWLEDGMENTS
  10. INTRODUCTION
    1. Online Resources
    2. Who This Book Is For
    3. Why Learn Python?
    4. Installing Python
    5. How to Read This Book
    6. Using Programming Judges
    7. Making Your Programming Judge Accounts
    8. About This Book
  11. 1 GETTING STARTED
    1. What We’ll Be Doing
    2. The Python Shell
    3. Problem #1: Word Count
    4. Strings
    5. Integer and Floating-Point Numbers
    6. Counting the Words Using a Variable
    7. Reading Input
    8. Writing Output
    9. Solving the Problem: A Complete Python Program
    10. Problem #2: Cone Volume
    11. More Math in Python
    12. Converting Between Strings and Integers
    13. Solving the Problem
    14. Summary
    15. Chapter Exercises
    16. Notes
  12. 2 MAKING DECISIONS
    1. Problem #3: Winning Team
    2. Conditional Execution
    3. The Boolean Type
    4. Relational Operators
    5. The if Statement
    6. Solving the Problem
    7. Problem #4: Telemarketers
    8. Boolean Operators
    9. Solving the Problem
    10. Comments
    11. Input and Output Redirection
    12. Summary
    13. Chapter Exercises
    14. Notes
  13. 3 REPEATING CODE: DEFINITE LOOPS
    1. Problem #5: Three Cups
    2. Why Loops?
    3. for Loops
    4. Nesting
    5. Solving the Problem
    6. Problem #6: Occupied Spaces
    7. A New Kind of Loop
    8. Indexing
    9. Range for loops
    10. Range for Loops Through Indices
    11. Solving the Problem
    12. Problem #7: Data Plan
    13. Looping to Read Input
    14. Solving the Problem
    15. Summary
    16. Chapter Exercises
    17. Notes
  14. 4 REPEATING CODE: INDEFINITE LOOPS
    1. Problem #8: Slot Machines
    2. Exploring a Test Case
    3. A Limitation of for loops
    4. while loops
    5. Solving the Problem
    6. The Mod Operator
    7. F-Strings
    8. Problem #9: Song Playlist
    9. String Slicing
    10. Solving the Problem
    11. Problem #10: Secret Sentence
    12. Another Limitation of for loops
    13. while Loops Through Indices
    14. Solving the Problem
    15. break and continue
    16. Summary
    17. Chapter Exercises
    18. Notes
  15. 5 ORGANIZING VALUES USING LISTS
    1. Problem #11: Village Neighborhood
    2. Why Lists?
    3. Lists
    4. List Mutability
    5. Learning About Methods
    6. List Methods
    7. Solving the Problem
    8. Avoiding Code Duplication: Two More Solutions
    9. Problem #12: School Trip
    10. Splitting Strings and Joining Lists
    11. Changing List Values
    12. Solving Most of the Problem
    13. How to Handle the Catch
    14. Problem #13: Baker Bonus
    15. Representing a Table
    16. Solving the Problem
    17. Summary
    18. Chapter Exercises
    19. Notes
  16. 6 DESIGNING PROGRAMS WITH FUNCTIONS
    1. Problem #14: Card Game
    2. Exploring a Test Case
    3. Defining and Calling Functions
    4. Function Documentation
    5. Solving the Problem
    6. Problem #15: Action Figures
    7. Representing the Boxes
    8. Top-Down Design
    9. Summary
    10. Chapter Exercises
    11. Notes
  17. 7 READING AND WRITING FILES
    1. Problem #16: Essay Formatting
    2. Working with Files
    3. Solving the Problem
    4. Problem #17: Farm Seeding
    5. Exploring a Test Case
    6. Top-Down Design
    7. Summary
    8. Chapter Exercises
    9. Notes
  18. 8 ORGANIZING VALUES USING SETS AND DICTIONARIES
    1. Problem #18: Email Addresses
    2. Using a List
    3. Efficiency of Searching a List
    4. Sets
    5. Set Methods
    6. Efficiency of Searching a Set
    7. Solving the Problem
    8. Problem #19: Common Words
    9. Exploring a Test Case
    10. Dictionaries
    11. Indexing Dictionaries
    12. Looping Through Dictionaries
    13. Inverting a Dictionary
    14. Solving the Problem
    15. Problem #20: Cities and States
    16. Exploring a Test Case
    17. Solving the Problem
    18. Summary
    19. Chapter Exercises
    20. Notes
  19. 9 DESIGNING ALGORITHMS WITH COMPLETE SEARCH
    1. Problem #21: Lifeguards
    2. Exploring a Test Case
    3. Solving the Problem
    4. Problem #22: Ski Hills
    5. Exploring a Test Case
    6. Solving the Problem
    7. Problem #23: Cow Baseball
    8. Using Three Nested Loops
    9. Sorting First
    10. Python Modules
    11. The bisect Module
    12. Solving the Problem
    13. Summary
    14. Chapter Exercises
    15. Notes
  20. 10 BIG O AND PROGRAM EFFICIENCY
    1. The Problem with Timing
    2. Big O
    3. Problem #24: Longest Scarf
    4. Exploring a Test Case
    5. Algorithm 1
    6. Algorithm 2
    7. Problem #25: Ribbon Painting
    8. Exploring a Test Case
    9. Solving the Problem
    10. Summary
    11. Chapter Exercises
    12. Notes
  21. AFTERWORD
  22. APPENDIX: PROBLEM CREDITS
  23. INDEX

Product information

  • Title: Learn to Code by Solving Problems
  • Author(s): Daniel Zingaro
  • Release date: June 2021
  • Publisher(s): No Starch Press
  • ISBN: 9781718501324