The 19th offline real-time how to write reference problem to solve with Python

Click here for the problem http://nabetani.sakura.ne.jp/hena/ord19sanwa/

Click here for other people's answers http://qiita.com/Nabetani/items/0a711729fdea28b1c30b

Faster version:

from itertools import chain, combinations_with_replacement as combi_r

def solve(d):
    n=map(int, d.split(','))
    s=set(n)
    a=[(x,y,z)
       for x in xrange(1, n[0]+1)
       for y in xrange(x, n[-1]+1)
       for z in xrange(max(y, n[-1]/3), n[-1]+1)
       if s == s&{sum(c) for c in chain(*[combi_r((x,y,z), r) for r in (1,2,3)])}]
    return not a and 'none' or a[1:] and 'many' or ','.join(map(str, a[0]))

First implementation:

from itertools import *

def solve(d):
    n=map(int, d.split(','))
    s=set(n)
    a=[(x,y,z)
       for x in xrange(1, n[0]+1)
       for y in xrange(x, n[-1]+1)
       for z in xrange(y, n[-1]+1)
       if s == s&{sum(p) for p in chain(*[product((x,y,z), repeat=r) for r in (1,2,3)])}]
    return not a and 'none' or len(a)>1 and 'many' or ','.join(map(str, a[0]))

def test(data, correct):
    answer = solve(data)
    print 'xo'[answer == correct], data, correct, answer

0, test( "3,11,12,102,111,120", "1,10,100" );
1, test( "10,20,30,35,70", "many" );
2, test( "1,5,20,80", "none" );
3, test( "1,2,3,4,5,6,7,8,9,10,11,12,13,14", "many" );
4, test( "1,2,3,4,5,6,7,8,9,10,11,12,13,14,15", "1,4,5" );
5, test( "1,2,3,4,5,6,7,8,9,10,11,12,13,14,17", "none" );
6, test( "1,2,3,4,5,6,7,8,9,10,11,12,13,14,18", "1,4,6" );
7, test( "5,6,7,8,9,10,11,12,13,14,15,16", "2,5,6" );
8, test( "9,10,11,12,13,14,15,16,17,18,19", "4,5,7" );
9, test( "11,36,37,45,55,70,71", "1,10,35" );
10, test( "92,93,94,95,96,97,98,99", "30,32,33" );
11, test( "95,96,97,98,99,100", "many" );
12, test( "27,30,34,37,43,44,46,51,57", "10,17,23" );
13, test( "6,10,13,17,65,73,76,80", "none" );
14, test( "12,19,21,29,85", "none" );
15, test( "1,2,8,10,14,23,58,62,64", "none" );
16, test( "4,22,25,31,44,50,58,69,71,72,73,77", "none" );
17, test( "8,16,26,27,42,53,65,69,81,83,88,99", "none" );
18, test( "9,10,23,24,28,33,38,39,58,68,84", "none" );
19, test( "11,16,24,26,88", "none" );
20, test( "24,33,47,56,63,66,75,78,89,93", "none" );
21, test( "7,26,72,77", "many" );
22, test( "69,88,95,97", "many" );
23, test( "9,14,48,89", "many" );
24, test( "69,76,77,83", "many" );
25, test( "11,14,24", "many" );
26, test( "8,25,75,93", "many" );
27, test( "11,55,93,98,99", "many" );
28, test( "71,83,87", "many" );
29, test( "22,76,77,92", "7,15,62" );
30, test( "33,61,66,83,95", "17,33,61" );
31, test( "6,16,49,55,72", "6,16,33" );
32, test( "62,85,97,98", "12,25,73" );
33, test( "54,60,67,70,72", "20,25,27" );
34, test( "54,61,68,84,87", "27,30,34" );
35, test( "65,67,69,75,79,89,99", "21,23,33" );
36, test( "69,72,80,81,89", "23,24,33" );
37, test( "1,2,3", "many" );

Speeding up:

product((x,y,z), repeat=r)

To

combinations_with_replacement((x,y,z), r)

By changing to, the execution time is now 13 seconds, which was 22 seconds. The minimum value of z is 1/3 of the maximum input value, so the following is a little faster.

for z in xrange(max(y, n[-1]/3), n[-1]+1)

Recommended Posts

The 16th offline real-time how to write reference problem to solve with Python
The 19th offline real-time how to write reference problem to solve with Python
The 15th offline real-time how to write reference problem in Python
The 14th offline real-time how to write reference problem in python
The 18th offline real-time how to write reference problem in Python
The 15th offline real-time I tried to solve the problem of how to write with python
The 16th offline real-time how to write problem was solved with Python
The 17th offline real-time how to write reference problem implemented in Python
The 15th offline real-time how to write problem was solved with python
The 14th offline real-time writing reference problem with Python
The 17th Offline Real-time How to Solve Writing Problems in Python
20th Offline Real-time How to Write Problems in Python
How to write offline real time I tried to solve the problem of F02 with Python
13th Offline Real-time How to Solve Writing Problems in Python
How to write offline real-time Solving E05 problems with Python
The 10th offline real-time writing reference problem. Implementation example by Python.
Offline real-time how to write Python implementation example of E15 problem
The 11th offline real-time writing reference problem. Implementation example by python.
How to write offline real time Solve F01 problems with Python
Answer to "Offline Real-time How to Write F01 Problem"
Answer to "Offline Real-time How to Write E13 Problem"
The 19th offline real-time writing problem in Python
How to write offline real time I tried to solve E11 with python
How to write offline real time I tried to solve E12 with python
Try to solve the internship assignment problem with Python
I tried to solve the problem with Python Vol.1
Part 1 I wrote the answer to the reference problem of how to write offline in real time in Python
Offline real-time how to write Python implementation example of E14
The twelfth offline real-time writing reference problem. Implementation by python
Part 1 I wrote an example of the answer to the reference problem of how to write offline in real time in Python
How to solve the bin packing problem
Offline real-time how to write E11 ruby and python implementation example
How to write offline real time Solve E04 problems in Python
I wanted to solve the ABC164 A ~ D problem with Python
Try to solve the fizzbuzz problem with Keras
Try to solve the Python class inheritance problem
Try to solve the man-machine chart with Python
[Introduction to Python] How to write a character string with the format function
Try to solve the traveling salesman problem with a genetic algorithm (Python code)
Try to solve the programming challenge book with python3
The first algorithm to learn with Python: FizzBuzz problem
I tried to solve the soma cube with python
[Python] How to specify the download location with youtube-dl
[Python] How to rewrite the table style with python-pptx [python-pptx]
Write to csv with Python
[Note] How to write QR code and description in the same image with python
I tried to simulate how the infection spreads with Python
A story about how to deal with the CORS problem
How to get into the python development environment with Vagrant
[Introduction to Python] How to get data with the listdir function
How to write a Python class
Python: How to use async with
[Python] Write to csv file with Python
How to get the Python version
How to get started with Python
[Python] How to import the library
How to use FTP with Python
How to calculate date with python
I tried to solve the ant book beginner's edition with python
[Introduction to Python] How to split a character string with the split function
Try to solve the N Queens problem with SA of PyQUBO