[PYTHON] Programming beginner (junior high school student) Optimize the created algorithm

Math puzzle

Mr. K studying Python [Mathematical puzzle](https://www.amazon.co.jp/%E3%83%97%E3%83%AD%E3%82%B0%E3%83%A9%E3% 83% 9E% E8% 84% B3% E3% 82% 92% E9% 8D% 9B% E3% 81% 88% E3% 82% 8B% E6% 95% B0% E5% AD% A6% E3% 83% 91% E3% 82% BA% E3% 83% AB-% E3% 82% B7% E3% 83% B3% E3% 83% 97% E3% 83% AB% E3% 81% A7% E9% AB% 98 % E9% 80% 9F% E3% 81% AA% E3% 82% B3% E3% 83% BC% E3% 83% 89% E3% 81% 8C% E6% 9B% B8% E3% 81% 91% E3 % 82% 8B% E3% 82% 88% E3% 81% 86% E3% 81% AB% E3% 81% AA% E3% 82% 8B70% E5% 95% 8F-% E5% A2% 97% E4% BA% 95-% E6% 95% 8F% E5% 85% 8B / dp / 479814245X / ref = asc_df_479814245X /? Tag = jpo-22 & linkCode = df0 & hvadid = 295723231663 & hvpos = 1o1 & hvnetw = g & hvrand = 6299120856193524254 & hvpone & hvlocint = & hvlocphy = 100009242 & hvtargid = pla-526233948639 & psc = 1 & th = 1 & psc = 1) Bring it and try this. I said.

The problem is that the minimum number of palindromic numbers (numbers such as 12321) is 10 or more in any of decimal, binary, and octal numbers.

Please find out where you can go.


D = list()
E = list()
F = list()
def checker(num):
    a = str(num)
    b = list(reversed(a))
    c = len(b)
    for j in range(c//2):
        if b[j] != b[-j-1]:
            return None
    return num

for i in range(10,1000):
    d = checker(i)
    if d != None:
        D.append(d)

for i in D:
    e = checker(str(oct(i))[2:])
    if e != None:
        E.append(int(e,8))

for i in E:
    f = checker(str(bin(i))[2:])
    if f != None:
        F.append(int(f,2))
print(F)

I'm looking for the correct answer, so it's a Good Job.

Instead of one pass, first check the number of palindromes up to 1000, and then check whether the number of palindromes found is palindrome or binary. I guess I made it while putting it on by making various thoughts and mistakes.

K-kun's refactoring version

But K-kun, who realized that he could do it with one pass, fixed it.

number = 10
result_list = []
def checker(num):
    a = list(reversed(str(num)))
    for j in range(len(a)//2):
        if a[j] != a[-j-1]:
            return None
    return num  
                     
while True:
    if checker(number) != None and checker(str(oct(number))[2:]) != None and checker(str(bin(number))[2:]) != None:
        result_list.append(number)
        print(number)
        break
    else:
        number += 1

This is a fairly straightforward flow

Start cooking

There are various ways to convert to binary or octal character strings, and I think that K's method is fine, but I changed it to "{: o} ". format (number). I don't think this is the level of which method is better.

The most mysterious thing is the return value of the checker function. If it's a function that simply checks for the number of palindromes, I think it should return True / False.

The palindrome is the remnant of trying to find the characters in reverse order, reversed (str (num)). As a result, you don't need this, right?

I tried to fix that hit.

def checker(num_str):
    for j in range(len(num_str)//2):
        if num_str[j] != num_str[-j-1]:
            return False
    return True
                     
number = 10
while True:
    if (checker(str(number)) and 
        checker( "{:o}".format(number)) and
        checker( "{:b}".format(number))) :
        print(number)
        break
    number += 1

The program you commented on was simpler and easier to understand, so I'll add it.

from itertools import count

def is_palindrome(text):
    return text == text[::-1]

for number in count(10):
    if (is_palindrome(f"{number}")
    and is_palindrome(f"{number:o}")
    and is_palindrome(f"{number:b}")):
        print(number)
        break

Recommended Posts

Programming beginner (junior high school student) Optimize the created algorithm
I tried to refactor the code of Python beginner (junior high school student)
I tried to remodel the code of Python beginner (junior high school student) into object-oriented crunchy
Junior high school committee
Python beginner (junior high school student) combination generation was diagonally above (combination generation by recursive processing)
Junior high school committee (NetworkX version)
Script of "Programming book starting from elementary and junior high school students"
[Python] A junior high school student implemented Perceptron and tried to classify irises.