SlideShare a Scribd company logo
Competitive
Programming
using Python
Andrii Soldatenko
29 September 2018
OdessaPY ‘18
@a_soldatenko
Andrii Soldatenko
• Contributor in OpenAPI, Golang,
Pyhelm, python-sendgrid, tutorial of
aiohttp, ansible, requests, etc.
• Speaker at many PyCons,
committee member of PyCon
Belarus and PyCon Russia and
open source contributor
• blogger at https://asoldatenko.com
@a_soldatenko
Andrii Soldatenko "Competitive programming using Python"
Andrii Soldatenko "Competitive programming using Python"
Edsger Dijkstra
1930-2002
Given well-known
Computer Science
problems
@a_soldatenko
https://en.wikipedia.org/wiki/
List_of_unsolved_problems_in_computer_science
@a_soldatenko
Disclamer
@a_soldatenko
Anatomy of a Programming
Contest Problem
• Background story / Summary
• Input and Output description
• Sample Input/Output
• Hints/Pictures
@a_soldatenko
How it looks like?
Andrii Soldatenko "Competitive programming using Python"
top500 ~500 problem solved
0
5000
10000
15000
20000
25000
30000
2008 2009 2010 2011 2012 2013 2014 2015 2016 2017
Number of contestants used
Python compare to other
languages
C C++ Java Python
Tip 1:
Type code faster
https://www.typingtest.com/
https://www.typingtest.com/
Andrii Soldatenko "Competitive programming using Python"
Tip 2:
Do Algorithm analysis
Time complexity
O( . . . )
for i in range(n):
pass
for i in range(n):
pass
O(n)
for i in range(n//2):
pass
for i in range(n//2):
pass
O(n)
for i in range(n+2):
pass
for i in range(n+2):
pass
O(n)
for i in range(n):
for j in range(n):
for i in range(n):
for j in range(n):
O(n2
)
def foo(n):
if n == 1: return
foo(n-1)
foo(n-1)
function call number of calls
foo(n) 1
foo(n-1) 2
…. …
foo(1)
function call number of calls
foo(n) 1
foo(n-1) 2
…. …
foo(1) 2n
− 1
Time Complexity
CPython -> list
Append[1] -> O(1)
Get Item -> O(1)
Sort -> O(n log n)
https://wiki.python.org/moin/TimeComplexity
Tip 3:
Identify
problem types
Problem types:
• Ad hoc
• Complete Search (Iterative/Recursive)
• Divide and Conquer
• Dynamic Programming
Problem types:
• String Processing
• Computational Geometry
• Graph
• Mathematics
@a_soldatenko
Tip 5:
Master Your
Programming Languages
@a_soldatenko
https://docs.python.org/3/reference/index.html
Tip 6:
@a_soldatenko
Input / Output Routines:
Sample input:
3
1 2
5 7
6 3
@a_soldatenko
Input / Output Routines:
input()
sys.stdin
open(‘sample.in’)
@a_soldatenko
Please don’t use prints
for debugging
$ python main.py < sample.in
> /Users/andrii/fan/fan/uva_answers/000/
main.py(6)main()
5 import ipdb; ipdb.set_trace()
----> 6 for line in sys.stdin:
7 print(line)
ipdb> *** SyntaxError: invalid syntax
ipdb> *** SyntaxError: invalid syntax
@a_soldatenko
Simple tip:
def fopen(filename, default=sys.stdin):
    try:
        f = open(filename)
    except FileNotFoundError:
        f = default
    return f
@a_soldatenko
Simple tip:
$ python main.py
> /Users/andrii/fan/fan/uva_answers/000/
main.py(13)main()
12 import ipdb; ipdb.set_trace()
---> 13 for line in fopen('sample.in'):
14 print(line)
Let’s solve problems
195 - Anagram
Given the word “abc”, your
program should — by exploring
all different combination of the
three letters — output the
words “abc”, “acb”, “bac”,
“bca”, “cab” and “cba”.
https://uva.onlinejudge.org/external/1/195.pdf
int main()
{
string s = "aba";
sort(s.begin(), s.end());
do {
cout << s << 'n';
} while(next_permutation(s.begin(),
s.end()));
}
// aab
// aba
// baa
@a_soldatenko
from itertools import permutations

def main():
for perm in permutations('aba'):
print(''.join(perm))
# aab
# aab
# aba
# aba
# baa
# baa
from itertools import permutations

def main():
for perm in permutations('aba'):
print(''.join(perm))
https://docs.python.org/3/library/
itertools.html#itertools.permutations
Serhiy Storchaka proposed
in python-ideas to add:
• factorial(n)
• gcd(n, m)
• as_integer_ration(x)
• binom(n, k)
• isprime(n)
• primes()
@a_soldatenko
Telegram channel
http://t.me/
python_programming_challenges
Thank You
andrii.soldatenko@toptal.com
@a_soldatenko
Questions
? @a_soldatenko
Telegram channel
http://t.me/
python_programming_challenges

More Related Content

PDF
Python in programming competitions
PPTX
Python.pptx
PPTX
Python Workshop - Learn Python the Hard Way
PDF
Python Part 1
PDF
python lab programs.pdf
PDF
Python lecture 05
PDF
Python cheatsheat.pdf
PPT
python presentation lists,strings,operation
Python in programming competitions
Python.pptx
Python Workshop - Learn Python the Hard Way
Python Part 1
python lab programs.pdf
Python lecture 05
Python cheatsheat.pdf
python presentation lists,strings,operation

Similar to Andrii Soldatenko "Competitive programming using Python" (20)

PDF
Python for High School Programmers
PPT
Python Training v2
PDF
GE3151_PSPP_UNIT_5_Notes
ODP
Python quickstart for programmers: Python Kung Fu
PDF
Python_ 3 CheatSheet
PPT
Python scripting kick off
PPTX
Python_Unit_III.pptx
PPT
Python tutorialfeb152012
PDF
Python Cheat Sheet
PPTX
Python programming workshop
PDF
xii cs practicals class 12 computer science.pdf
PPT
python language programming presentation
PPTX
IoT-Week1-Day1-Lab.pptx
PDF
python practicals-solution-2019-20-class-xii.pdf
PDF
cos 102 - getting into programming with python.pdf
PDF
Mementopython3 english
PDF
PDF
Python Cheatsheet_A Quick Reference Guide for Data Science.pdf
PDF
Introduction to python cheat sheet for all
Python for High School Programmers
Python Training v2
GE3151_PSPP_UNIT_5_Notes
Python quickstart for programmers: Python Kung Fu
Python_ 3 CheatSheet
Python scripting kick off
Python_Unit_III.pptx
Python tutorialfeb152012
Python Cheat Sheet
Python programming workshop
xii cs practicals class 12 computer science.pdf
python language programming presentation
IoT-Week1-Day1-Lab.pptx
python practicals-solution-2019-20-class-xii.pdf
cos 102 - getting into programming with python.pdf
Mementopython3 english
Python Cheatsheet_A Quick Reference Guide for Data Science.pdf
Introduction to python cheat sheet for all
Ad

Recently uploaded (20)

DOCX
How to Use SharePoint as an ISO-Compliant Document Management System
PDF
AI-Powered Threat Modeling: The Future of Cybersecurity by Arun Kumar Elengov...
PPTX
WiFi Honeypot Detecscfddssdffsedfseztor.pptx
PPTX
AMADEUS TRAVEL AGENT SOFTWARE | AMADEUS TICKETING SYSTEM
PDF
How Tridens DevSecOps Ensures Compliance, Security, and Agility
PPTX
Tech Workshop Escape Room Tech Workshop
PPTX
Computer Software and OS of computer science of grade 11.pptx
PPTX
Custom Software Development Services.pptx.pptx
PDF
Cost to Outsource Software Development in 2025
PDF
How to Make Money in the Metaverse_ Top Strategies for Beginners.pdf
PDF
Types of Token_ From Utility to Security.pdf
PPTX
Advanced SystemCare Ultimate Crack + Portable (2025)
PDF
iTop VPN Crack Latest Version Full Key 2025
PDF
Ableton Live Suite for MacOS Crack Full Download (Latest 2025)
PDF
AI/ML Infra Meetup | Beyond S3's Basics: Architecting for AI-Native Data Access
PDF
Autodesk AutoCAD Crack Free Download 2025
PPTX
Cybersecurity: Protecting the Digital World
PPTX
Introduction to Windows Operating System
PDF
Digital Systems & Binary Numbers (comprehensive )
PPTX
Why Generative AI is the Future of Content, Code & Creativity?
How to Use SharePoint as an ISO-Compliant Document Management System
AI-Powered Threat Modeling: The Future of Cybersecurity by Arun Kumar Elengov...
WiFi Honeypot Detecscfddssdffsedfseztor.pptx
AMADEUS TRAVEL AGENT SOFTWARE | AMADEUS TICKETING SYSTEM
How Tridens DevSecOps Ensures Compliance, Security, and Agility
Tech Workshop Escape Room Tech Workshop
Computer Software and OS of computer science of grade 11.pptx
Custom Software Development Services.pptx.pptx
Cost to Outsource Software Development in 2025
How to Make Money in the Metaverse_ Top Strategies for Beginners.pdf
Types of Token_ From Utility to Security.pdf
Advanced SystemCare Ultimate Crack + Portable (2025)
iTop VPN Crack Latest Version Full Key 2025
Ableton Live Suite for MacOS Crack Full Download (Latest 2025)
AI/ML Infra Meetup | Beyond S3's Basics: Architecting for AI-Native Data Access
Autodesk AutoCAD Crack Free Download 2025
Cybersecurity: Protecting the Digital World
Introduction to Windows Operating System
Digital Systems & Binary Numbers (comprehensive )
Why Generative AI is the Future of Content, Code & Creativity?
Ad

Andrii Soldatenko "Competitive programming using Python"