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
Code Golf Guide - Techniques and Strategies
General Code Golf Techniques
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)
Replace loops with list comprehensions
Example: [f(x)for x in l] instead of traditional loop
Use a if c else b instead of full if/else
Example: print('Even'if n%2<1else'Odd')
Leverage map(), filter(), sum(), any(), all()
Use eval() carefully for mathematical expressions
Example: sum(map(int,input().split()))
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)
*map(int,input().split()) for multiple integers
print(*list,sep='\n') to print list vertically
# 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 < 2 or f (n - 1 )+ f (n - 2 )
# Normal
def is_palindrome (s ):
return s == s [::- 1 ]
# Code Golf
p = lambda s :s == s [::- 1 ]
# Normal
def factorial (n ):
if n <= 1 :
return 1
return n * factorial (n - 1 )
# Code Golf
f = lambda n :n < 2 or n * f (n - 1 )
Use ; for multiple statements on one line
exec() can execute code from strings
*args and **kwargs for flexible functions
Walrus operator := (Python 3.8+)
Use => instead of function
Template literals for strings
Destructuring assignment
&& and || for flow control
python utils/count_chars.py solutions/problem01.py
python utils/test_solution.py solutions/problem01.py
python -m py_compile solutions/problem01.py
Unnecessary comments
Long variable names
Excessive indentation
Unused imports
Redundant code