Solve mod7 fortune-telling (equivalent to paiza rank S) in Python

at first

I was solving the paiza skill check basic problem, but I didn't have a model answer, so I made it myself. The language is Python3.

problem

Paiza exercise mod7 fortune-telling (equivalent to paiza rank S) https://paiza.jp/works/mondai/skillcheck_sample/mod7?language_uid=python3 I couldn't see the problem statement without logging in. Registration is free and can be done immediately, so I recommend you to register for the time being.

Bad answer code

--Mie loop --Computational complexity: O (n ^ 3) --Large-scale data will fail due to time over.

mod7_v1.py


n = int(input())
cards = [int(input())%7 for i in range(n)]

total = 0
for n1 in range(len(cards)):
    for n2 in range(n1+1,len(cards)):
        for n3 in range(n2+1, len(cards)):
            if (cards[n1]+cards[n2]+cards[n3])%7==0:
                total+=1
                
print(total)

Bad answer code # 2

Not for the same reason as above.

mod7_v2.py


import itertools

n = int(input())
cards = [int(input())%7 for i in range(n)]

total = 0
for num_set in itertools.combinations(cards, 3):
    if sum(num_set)%7==0:
        total += 1

print(total)

Answer code

――Even if you take the remainder before adding, the result is the same -(A + B + C)% 7 is equal to (A% 7 + B% 7 + C% 7)% 7. --It can be considered that only the values from 0 to 6 were given. --The combination of choosing three values is 7 * 7 * 7 = 343

mod7.py


n = int(input())
cards = [int(input())%7 for i in range(n)]
total = 0
for n1 in range(7):
    for n2 in range(7):
        for n3 in range(7):
            if (n1 + n2 + n3) %7 == 0:
                c1 = cards.count(n1)
                c2 = cards.count(n2)
                c3 = cards.count(n3)
                if n2 == n1:
                    c2 -= 1
                if n3 == n1:
                    c3 -= 1
                if n3 == n2:
                    c3 -= 1
                pat = c1*c2*c3
                total += pat
print(total//6)

reference

https://www.slideshare.net/paiza_official/mod7-note

Summary

As a result of various trials and errors, I was impressed by myself because it became quite thimble. Now I want vocabulary and commentary to convey this impression to someone.

Recommended Posts

Solve mod7 fortune-telling (equivalent to paiza rank S) in Python
Solve island hunting (equivalent to paiza rank S) in Python
Solve addition (equivalent to paiza rank D) in Python
Solve multiplication (equivalent to paiza rank D) in Python
Solve number sorting (equivalent to paiza rank D) in Python
Solve word counts (equivalent to paiza rank C) in Python
Solve character matches (equivalent to paiza rank D) in Python
Solve Fizz Buzz (equivalent to paiza rank C) in Python
Solve the smallest value in Python (equivalent to paiza rank D)
[With commentary] Solve Fizz Buzz (equivalent to paiza rank C) in Python
PUT gzip directly to S3 in Python
Solve ABC168D in Python
Solve ABC167-D in Python
Solve ABC146-C in Python
Solve ABC098-C in Python
Solve ABC159-D in Python
Solve ABC169 in Python
Solve ABC160-E in Python
To do the equivalent of Ruby's ObjectSpace._id2ref in Python
To flush stdout in Python
Login to website in Python
Processing of python3 that seems to be usable in paiza
Solve ABC176 E in Python
Solve Wooldridge exercises in Python
Solve ABC175 D in Python
Speech to speech in python [text to speech]
13th Offline Real-time How to Solve Writing Problems in Python
Solve optimization problems in Python
How to develop in Python
Post to Slack in Python
I want to solve APG4b with Python (only 4.01 and 4.04 in Chapter 4)
The 17th Offline Real-time How to Solve Writing Problems in Python
How to write offline real time Solve E04 problems in Python
[Python] How to do PCA in Python
How to collect images in Python
How to use SQLite in Python
Solve Atcoder ABC169 A-D in Python
In the python command python points to python3.8
Try to calculate Trace in Python
Solve ABC036 A ~ C in Python
How to use Mysql in python
How to wrap C in Python
How to use ChemSpider in Python
6 ways to string objects in Python
How to use PubChem in Python
Solve ABC037 A ~ C in Python
Solve ordinary differential equations in Python
How to handle Japanese in Python
An alternative to `pause` in Python
I tried to solve AtCoder's depth-first search (DFS) in Python (result: TLE ...)
Upload what you got in request to S3 with AWS Lambda Python
[At Coder] What I did to reach the green rank in Python
I tried to implement PLSA in Python
[Introduction to Python] How to use class in Python?
Solve ABC175 A, B, C in Python
Try logging in to qiita with Python
Install Pyaudio to play wave in python
How to access environment variables in Python
I tried to implement permutation in Python
Method to build Python environment in Xcode 6
How to dynamically define variables in Python