Skip to content

Folders and files

NameName
Last commit message
Last commit date

Latest commit

ย 

History

2 Commits
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 

Repository files navigation

Codeforces Python Template

A clean and efficient template for Codeforces competitions in Python.

๐Ÿš€ Quick Setup

1. Create Conda Environment

# Run the setup script
./setup.sh

# Or manually create the environment
conda env create -f _environment.yml

2. Activate Environment

conda activate codeforces

3. Verify Installation

python --version  # Should show Python 3.11.x

๐Ÿ“ Project Structure

python/
โ”œโ”€โ”€ A.py, B.py, C.py, D.py, E.py, F.py  # Solution files for each problem
โ”œโ”€โ”€ main.py                              # Test runner script
โ”œโ”€โ”€ test.py                              # Unit test template
โ”œโ”€โ”€ input.txt                            # Input test cases
โ”œโ”€โ”€ output.txt                           # Expected output (optional)
โ”œโ”€โ”€ requirements.txt                     # Python dependencies
โ”œโ”€โ”€ environment.yml                      # Conda environment config
โ””โ”€โ”€ setup.sh                             # Setup script

๐Ÿ’ป Usage

Method 1: Using the Test Runner (Recommended)

  1. Copy test cases to input.txt
  2. Write your solution in the problem file (e.g., A.py)
  3. Run the test:
python main.py A        # Test problem A
python main.py B        # Test problem B

Auto-Verification Feature

The test runner automatically compares your output with output.txt and shows pass/fail indicators:

Verification Results:
==================================================
  #   Got   Expected
--------------------------------------------------
  1   โœ“ 6     6
  2   โœ“ 10    10
  3   โœ— 15    14       <- Red โœ— for wrong answer
  4   โœ“ 0     0
--------------------------------------------------
Failed: 3/4 passed
==================================================

Usage:

python main.py A              # Run with auto-verification (default)
python main.py A --no-verify  # Run without verification

How to use:

  1. Copy sample input to input.txt
  2. Copy expected output to output.txt
  3. Run python main.py A - instantly see which test cases pass or fail

Method 2: Direct Execution

python A.py < input.txt

Method 3: Interactive Testing

Modify and run test.py for quick unit tests:

python test.py

๐Ÿ“ Template Structure

Each problem file (A.py - F.py) follows this structure:

def solve():
    """Main solution function"""
    # Read input
    n = int(input())

    # Your logic here
    result = n

    # Output result
    print(result)

def main():
    """Handle multiple test cases"""
    t = int(input())
    for _ in range(t):
        solve()

if __name__ == "__main__":
    main()  # Or solve() for single test case

๐ŸŽฏ Best Practices for Codeforces

1. Input/Output Tips

# Fast I/O for large inputs
import sys
input = sys.stdin.readline

# Multiple values on one line
a, b, c = map(int, input().split())

# List of integers
arr = list(map(int, input().split()))

# Multiple lines
n = int(input())
data = [input().strip() for _ in range(n)]

2. Common Patterns

# Binary search
from bisect import bisect_left, bisect_right

# Sorting with custom key
arr.sort(key=lambda x: (x[0], -x[1]))

# Counter for frequency
from collections import Counter
freq = Counter(arr)

# Default dict
from collections import defaultdict
graph = defaultdict(list)

# Deque for efficient queue operations
from collections import deque
q = deque()

3. Mathematical Helpers

import math

# GCD and LCM
from math import gcd
def lcm(a, b):
    return abs(a * b) // gcd(a, b)

# Prime checking
def is_prime(n):
    if n < 2:
        return False
    for i in range(2, int(n**0.5) + 1):
        if n % i == 0:
            return False
    return True

# Modular arithmetic
MOD = 10**9 + 7
result = (a + b) % MOD

4. Time Complexity Tips

  • O(n): Linear - usually safe up to n = 10^8
  • O(n log n): Sorting - safe up to n = 10^6
  • O(nยฒ): Nested loops - safe up to n = 10^4
  • O(2^n): Exponential - safe up to n = 20

5. Common Mistakes to Avoid

  • โŒ Forgetting to handle multiple test cases
  • โŒ Not using strip() when reading strings
  • โŒ Integer overflow (Python handles this well!)
  • โŒ Wrong input format (check problem statement carefully)
  • โŒ Printing extra spaces or newlines

๐Ÿ”ง Quick Commands

# Activate environment
conda activate codeforces

# Deactivate environment
conda deactivate

# Update environment
conda env update -f environment.yml

# Install new package
pip install <package-name>

# Delete environment (if needed)
conda env remove -n codeforces

๐Ÿ“š Useful Resources

๐ŸŽฎ During Contest

  1. Read all problems first - identify the easiest ones
  2. Start with easier problems - build confidence and rating
  3. Use input.txt for testing - faster than manual input
  4. Test edge cases:
    • Minimum values (n=1, empty arrays)
    • Maximum values (constraints)
    • Special cases (all same, all different)
  5. Before submitting:
    • Check input/output format
    • Test with sample cases
    • Check for TLE (time limit)
    • Remove debug prints

๐Ÿ› Debugging Tips

# Debug print (remove before submission!)
print(f"Debug: n={n}, arr={arr}", file=sys.stderr)

# Assert for validation during development
assert len(arr) == n, "Array length mismatch"

# Timing your code
import time
start = time.time()
# ... your code ...
print(f"Time: {time.time() - start:.3f}s", file=sys.stderr)

๐Ÿ“Š Typical Problem Types

  1. Math/Number Theory: GCD, LCM, primes, modular arithmetic
  2. Greedy: Sorting, optimization
  3. DP: Memoization, tabulation
  4. Graph: BFS, DFS, shortest paths
  5. Data Structures: Arrays, hash maps, heaps
  6. Strings: Pattern matching, manipulation
  7. Binary Search: On answer space
  8. Combinatorics: Permutations, combinations

Good luck in your competition! ๐Ÿš€

About

A template for Codeforces competitions in Python 3.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages