Skip to content

Latest commit

 

History

History
141 lines (115 loc) · 3.65 KB

File metadata and controls

141 lines (115 loc) · 3.65 KB

MIT License

Copyright (c) 2025 Mauro Risonho de Paula Assumpção

Permission is hereby granted, free of charge, to any person obtaining a copy

of this software and associated documentation files (the "Software"), to deal

in the Software without restriction, including without limitation the rights

to use, copy, modify, merge, publish, distribute, sublicense, and/or sell

copies of the Software, and to permit persons to whom the Software is

furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all

copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR

IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,

FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE

AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER

LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,

OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE

SOFTWARE.

Code Golf Guide - Techniques and Strategies

General Code Golf Techniques

1. Space Removal

  • Remove all unnecessary spaces
  • Use ; instead of line breaks when possible
  • Example: if x:y instead of if x: y

2. Single Letter Variables

  • Use a,b,c instead of descriptive names
  • Reuse variables when possible
  • Example: a=input();print(a*2)

3. List Comprehensions

  • Replace loops with list comprehensions
  • Example: [f(x)for x in l] instead of traditional loop

4. Ternary Operators

  • Use a if c else b instead of full if/else
  • Example: print('Even'if n%2<1else'Odd')

5. Built-in Functions

  • Leverage map(), filter(), sum(), any(), all()
  • Use eval() carefully for mathematical expressions
  • Example: sum(map(int,input().split()))

6. Mathematical Tricks

  • Use x//y instead of int(x/y)
  • Use x%y<1 instead of x%y==0
  • Use ~x for -x-1

7. Strings and Formatting

  • Use % or f-strings compactly
  • Multiply strings: '*'*n
  • Join with empty strings: ''.join(list)

8. Compact Input/Output

  • *map(int,input().split()) for multiple integers
  • print(*list,sep='\n') to print list vertically

Practical Examples

Fibonacci

# Normal
def fib(n):
    a, b = 0, 1
    for _ in range(n):
        a, b = b, a + b
    return a

# Code Golf
f=lambda n:n<2or f(n-1)+f(n-2)

Palindrome

# Normal
def is_palindrome(s):
    return s == s[::-1]

# Code Golf
p=lambda s:s==s[::-1]

Factorial

# Normal
def factorial(n):
    if n <= 1:
        return 1
    return n * factorial(n-1)

# Code Golf
f=lambda n:n<2or n*f(n-1)

Tips by Language

Python

  • Use ; for multiple statements on one line
  • exec() can execute code from strings
  • *args and **kwargs for flexible functions
  • Walrus operator := (Python 3.8+)

JavaScript

  • Use => instead of function
  • Template literals for strings
  • Destructuring assignment
  • && and || for flow control

Useful Tools

Character Measurement

python utils/count_chars.py solutions/problem01.py

Local Testing

python utils/test_solution.py solutions/problem01.py

Syntax Validation

python -m py_compile solutions/problem01.py

Things to Avoid

  • Unnecessary comments
  • Long variable names
  • Excessive indentation
  • Unused imports
  • Redundant code

External Resources