- Executors
-
Learn
Learn Python Runnable examples, NumPy, pandas, and more Learn AI / Machine LearningNeural networks, ML and LLMs from scratch Learn C & C++ Pointers, memory, STL, and modern C++ Learn C#C# basics, OOP, LINQ, async, and .NET exercises Learn MatplotlibLine, bar, scatter and pie charts with live visualizers Learn RustOwnership, traits, error handling, and threads Learn GoSlices, interfaces, goroutines, and channels Python Algorithms Search, sorting, recursion, and visualizers O(n) C & C++ Algorithms Compare algorithms implemented in C and C++ Coding for Kids Learn to code by playing!
- ChallengesNew
- For TeachersNew
- Updates
- Buy me a coffee
A seating chart for a huge stadium is mostly empty, so storing every cell wastes memory. A SparseGrid stores only the cells that hold a non-zero value; every other cell is 0. The grid starts with rows rows, cols columns and no stored cells. Process these operations in order:
set r c v: cell (r, c) becomesv. Setting 0 removes the cell from storage.get r c: output the value of cell (r, c).count: output how many cells are non-zero.row_sum r: output the sum of the values in rowr.width: output the current number of columns.remove_col c: delete columnc. Its cells disappear, every cell to its right moves one column left, and the grid has one column fewer.
Rows and columns are numbered from 0, and every index is inside the grid at the time of the operation.
Implement sparse_grid(rows, cols, operations).
Input
rows: The number of rows.
cols: The starting number of columns.
operations: The operations, processed in order.
Output
Return the outputs of get, count, row_sum and width in the order they happen.
Constraints
1 ≤ rows ≤ 1,000,000
1 ≤ cols ≤ 1,000,000
0 ≤ number of operations ≤ 1,000
−1,000,000 ≤ v ≤ 1,000,000
Examples
Example 1
Input:
rows = 3
cols = 4
operations = ["set 0 1 5", "set 0 3 2", "set 2 2 7", "row_sum 0", "count", "get 1 1"]
Expected output:
[7, 3, 0]
Explanation
Row 0 holds 5 and 2, so its sum is 7; three cells are stored; (1, 1) was never set, so it is 0.
Example 2
Input:
rows = 2
cols = 3
operations = ["set 1 0 4", "set 1 2 9", "remove_col 0", "get 1 1", "width", "count"]
Expected output:
[9, 2, 1]
Explanation
Removing column 0 deletes the 4 and slides the 9 from column 2 to column 1, so get 1 1 is 9, the width is 2 and one cell is left.
References
-
Example 1 Not runInput:
rows = 3 cols = 4 operations = ["set 0 1 5", "set 0 3 2", "set 2 2 7", "row_sum 0", "count", "get 1 1"]Expected output:[7, 3, 0]Your output -
Example 2 Not runInput:
rows = 2 cols = 3 operations = ["set 1 0 4", "set 1 2 9", "remove_col 0", "get 1 1", "width", "count"]Expected output:[9, 2, 1]Your output
4 hidden tests also run when you submit. If your code fails one, you'll see that test. Solve the challenge to see them all.
Online Python Compiler & Debugger
Write, run, and debug Python code online. Set breakpoints and step through code in Server mode, or use the full IDE for multiple files, libraries, saved workspaces, sharing, and live collaboration - no setup required.
4,371,536 total executions (91,936 this month)
Practice Python with coding challenges
Solve hands-on problems right in this editor: run the sample tests, submit, and get an AI code review.
Learn and practise Python
Follow a learning path with runnable lessons, then test yourself with coding challenges.
- Python from zero
- Python practice problems
- Algorithms in Python
- Data analysis with NumPy and pandas
- OOP and design patterns in Python
- Python for AI
- AI concepts from scratch
- Programming fundamentals (Python)
- Python coding challenges
- Python Visualizer
- pandas and NumPy Online
- NumPy Online
- How to Run a Python Script
- Python Online Debugger
- Python Practice Problems
- Python Turtle Online
- Python on a Chromebook
- Run Python in the Browser
- Python Code Checker
- AI Code Checker
- Run on CodeUtility button
- AI Code Generator
Popular lessons
🐍 About This Python Online Executor
The CodeUtility Python Executor lets you write, run, and test Python code directly in your browser - no setup or installation required. It’s powered by a secure sandbox that supports real Python versions from 3.10 to 3.13.
Whether you’re learning Python for the first time, testing quick snippets, or debugging logic, this tool provides a fast, distraction-free coding environment. You can experiment freely and see output instantly in the built-in console.
It’s ideal for beginners, students, and developers who need a lightweight online IDE for Python practice or quick prototyping.
More than a code runner
Use a multi-file editor, an isolated execution terminal, runtime-specific libraries, persistent workspaces, sharing, and real-time collaboration from the same executor.
How to use this executor?
Choose a runtime, open or create a file, write your code, and select Run. Output appears in the terminal, while signed-in users can keep files in a workspace, install libraries, and collaborate through shares.
- Select a version and edit the current file or open another workspace file.
- Run the code, provide input in the terminal when requested, and review saved run history.
- Use libraries, workspace tools, sharing, and chat when your task needs more than one snippet.
💡 Python Basics & Examples You Can Try Above
1. Declaring Variables and Constants
Python is dynamically typed, so you don’t need to declare types explicitly. There's no built-in constant keyword, but by convention, constants are written in all-uppercase.
# Variables
x = 10
pi = 3.14
name = "Alice"
is_active = True
# Constants (by convention)
MAX_USERS = 100
APP_NAME = "CodeUtility"
# Constants can still be reassigned (not enforced)
MAX_USERS = 200 # ⚠️ Technically allowed, but discouraged
2. Conditionals
Control logic with if, elif, and else blocks. In Python 3.10+, you can also use match-case as a switch-case alternative.
# Traditional if-elif-else
x = 2
if x == 1:
print("One")
elif x == 2:
print("Two")
else:
print("Other")
Alternative: match-case (Python 3.10+)
# Requires Python 3.10+
x = 2
match x:
case 1:
print("One")
case 2:
print("Two")
case _:
print("Other")
3. Loops
for is used to iterate over sequences, while runs as long as a condition is true.
for i in range(3):
print(i)
count = 3
while count > 0:
print(count)
count -= 1
4. Lists
Lists are ordered, mutable collections. You can access elements by index.
fruits = ["apple", "banana"]
print(fruits[0])
print(len(fruits))
5. List Manipulation
Add, remove, slice, and reverse lists. List comprehensions allow compact iteration.
fruits.append("cherry")
fruits.insert(1, "kiwi")
fruits.remove("banana")
fruits.pop()
print(fruits[1:3])
print(fruits[::-1])
squares = [x*x for x in range(5)]
6. Console Input/Output
Use input() to read from users and print() to display output.
You can print multiple lines using \n (newline character) or by calling print() multiple times.
# Read input
name = input("Enter your name: ")
print("Hello", name)
# Print multiple lines
print("Line 1\nLine 2\nLine 3")
# Or use multiple print statements
print("Line A")
print("Line B")
print("Line C")
7. Functions
Functions help organize code and allow reuse. Use parameters and return values.
def greet(name):
return "Hello " + name
print(greet("Alice"))
8. Dictionaries
Store key-value pairs. Keys are unique and values can be accessed via keys.
person = {"name": "Bob", "age": 25}
print(person["name"])
print(person.get("email", "Not provided"))
9. Exception Handling
Use try and except to catch and handle errors gracefully.
try:
x = 1 / 0
except ZeroDivisionError:
print("Cannot divide by zero")
10. File I/O
Read from and write to files using open().
with open("data.txt", "w") as f:
f.write("Hello World")
with open("data.txt", "r") as f:
print(f.read())
11. String Manipulation
Python strings have powerful methods like strip(), replace(), and split().
text = " Hello World "
print(text.strip())
print(text.upper())
print(text.replace("Hello", "Hi"))
print(text.split())
12. Classes & Objects
Define reusable blueprints with classes. Use __init__ to initialize objects.
class Person:
def __init__(self, name):
self.name = name
def greet(self):
return "Hi, I'm " + self.name
p = Person("Alice")
print(p.greet())
🐞 How to Debug Python Code Online
Use the online Python debugger to find logic errors without installing Python or a desktop IDE. Debugging runs with a real Python runtime in Server mode and supports Python 3.10 through 3.13.
- Select a server-based Python version from the runtime menu.
- Click beside a line number to set one or more breakpoints.
- Select Debug to start the program and pause on the first matching breakpoint.
- Inspect the current line and variables, then use Continue, Step Over, Step Into, or Step Out.
- Select Stop when you want to end the debugging session.
For a quick Python online debug session, start with a small reproducible example and place a breakpoint just before the value becomes unexpected. Server mode can also run workspace files and installed libraries.