SlideShare ist ein Scribd-Unternehmen logo
1 von 54
Downloaden Sie, um offline zu lesen
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
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?
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/
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

Weitere ähnliche Inhalte

Ähnlich wie Andrii Soldatenko "Competitive programming using Python"

Play framework: lessons learned
Play framework: lessons learnedPlay framework: lessons learned
Play framework: lessons learned
Peter Hilton
 
Code Analysis-run time error prediction
Code Analysis-run time error predictionCode Analysis-run time error prediction
Code Analysis-run time error prediction
NIKHIL NAWATHE
 

Ähnlich wie Andrii Soldatenko "Competitive programming using Python" (20)

Blueprints: Introduction to Python programming
Blueprints: Introduction to Python programmingBlueprints: Introduction to Python programming
Blueprints: Introduction to Python programming
 
Play framework: lessons learned
Play framework: lessons learnedPlay framework: lessons learned
Play framework: lessons learned
 
Which programming language to learn R or Python - MeasureCamp XII
Which programming language to learn R or Python - MeasureCamp XIIWhich programming language to learn R or Python - MeasureCamp XII
Which programming language to learn R or Python - MeasureCamp XII
 
Code Analysis-run time error prediction
Code Analysis-run time error predictionCode Analysis-run time error prediction
Code Analysis-run time error prediction
 
Pyhton-1a-Basics.pdf
Pyhton-1a-Basics.pdfPyhton-1a-Basics.pdf
Pyhton-1a-Basics.pdf
 
Python for Swift
Python for SwiftPython for Swift
Python for Swift
 
A Python Tutorial
A Python TutorialA Python Tutorial
A Python Tutorial
 
Deploying your Predictive Models as a Service via Domino
Deploying your Predictive Models as a Service via DominoDeploying your Predictive Models as a Service via Domino
Deploying your Predictive Models as a Service via Domino
 
FPGA DESIGN FLOW.pdf
FPGA DESIGN FLOW.pdfFPGA DESIGN FLOW.pdf
FPGA DESIGN FLOW.pdf
 
Mufix Network Programming Lecture
Mufix Network Programming LectureMufix Network Programming Lecture
Mufix Network Programming Lecture
 
Python basics
Python basicsPython basics
Python basics
 
Introduction-to-Python.pptx
Introduction-to-Python.pptxIntroduction-to-Python.pptx
Introduction-to-Python.pptx
 
W1-Intro to python.pptx
W1-Intro to python.pptxW1-Intro to python.pptx
W1-Intro to python.pptx
 
More about PHP
More about PHPMore about PHP
More about PHP
 
Not Your Fathers C - C Application Development In 2016
Not Your Fathers C - C Application Development In 2016Not Your Fathers C - C Application Development In 2016
Not Your Fathers C - C Application Development In 2016
 
Python final presentation kirti ppt1
Python final presentation kirti ppt1Python final presentation kirti ppt1
Python final presentation kirti ppt1
 
Introduction to python programming
Introduction to python programmingIntroduction to python programming
Introduction to python programming
 
2019 session 6 develop programs to solve a variety of problems in math , phys...
2019 session 6 develop programs to solve a variety of problems in math , phys...2019 session 6 develop programs to solve a variety of problems in math , phys...
2019 session 6 develop programs to solve a variety of problems in math , phys...
 
Code Reviews in Python - PyZh
Code Reviews in Python - PyZhCode Reviews in Python - PyZh
Code Reviews in Python - PyZh
 
05 python.pdf
05 python.pdf05 python.pdf
05 python.pdf
 

Kürzlich hochgeladen

%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
masabamasaba
 
%+27788225528 love spells in Toronto Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Toronto Psychic Readings, Attraction spells,Brin...%+27788225528 love spells in Toronto Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Toronto Psychic Readings, Attraction spells,Brin...
masabamasaba
 
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
masabamasaba
 
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...
masabamasaba
 
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
Health
 
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
masabamasaba
 

Kürzlich hochgeladen (20)

tonesoftg
tonesoftgtonesoftg
tonesoftg
 
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
 
%in Harare+277-882-255-28 abortion pills for sale in Harare
%in Harare+277-882-255-28 abortion pills for sale in Harare%in Harare+277-882-255-28 abortion pills for sale in Harare
%in Harare+277-882-255-28 abortion pills for sale in Harare
 
%in kempton park+277-882-255-28 abortion pills for sale in kempton park
%in kempton park+277-882-255-28 abortion pills for sale in kempton park %in kempton park+277-882-255-28 abortion pills for sale in kempton park
%in kempton park+277-882-255-28 abortion pills for sale in kempton park
 
AI & Machine Learning Presentation Template
AI & Machine Learning Presentation TemplateAI & Machine Learning Presentation Template
AI & Machine Learning Presentation Template
 
WSO2CON2024 - It's time to go Platformless
WSO2CON2024 - It's time to go PlatformlessWSO2CON2024 - It's time to go Platformless
WSO2CON2024 - It's time to go Platformless
 
%+27788225528 love spells in Toronto Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Toronto Psychic Readings, Attraction spells,Brin...%+27788225528 love spells in Toronto Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Toronto Psychic Readings, Attraction spells,Brin...
 
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
 
WSO2CON 2024 - Building the API First Enterprise – Running an API Program, fr...
WSO2CON 2024 - Building the API First Enterprise – Running an API Program, fr...WSO2CON 2024 - Building the API First Enterprise – Running an API Program, fr...
WSO2CON 2024 - Building the API First Enterprise – Running an API Program, fr...
 
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...
 
Announcing Codolex 2.0 from GDK Software
Announcing Codolex 2.0 from GDK SoftwareAnnouncing Codolex 2.0 from GDK Software
Announcing Codolex 2.0 from GDK Software
 
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
 
8257 interfacing 2 in microprocessor for btech students
8257 interfacing 2 in microprocessor for btech students8257 interfacing 2 in microprocessor for btech students
8257 interfacing 2 in microprocessor for btech students
 
WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...
WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...
WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...
 
%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview
%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview
%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview
 
%in Soweto+277-882-255-28 abortion pills for sale in soweto
%in Soweto+277-882-255-28 abortion pills for sale in soweto%in Soweto+277-882-255-28 abortion pills for sale in soweto
%in Soweto+277-882-255-28 abortion pills for sale in soweto
 
Artyushina_Guest lecture_YorkU CS May 2024.pptx
Artyushina_Guest lecture_YorkU CS May 2024.pptxArtyushina_Guest lecture_YorkU CS May 2024.pptx
Artyushina_Guest lecture_YorkU CS May 2024.pptx
 
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
 
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
 
WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...
WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...
WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...
 

Andrii Soldatenko "Competitive programming using Python"