Python answers the number of palindrome in the blue bridge cup

Problem description
  1221 is a very special number. Reading from the left is the same as reading from the right. Programming to find all such four-digit decimal numbers.
Output format
  output the four-digit decimal numbers that meet the conditions in ascending order.
  
Two solutions are given below

  • The first uses list storage.
for x in range(1000,10000):
    z=str(x)
    y=[]
    for i in z:
        y.append(i)
    first=y.pop(0)
    sencond=y.pop(0)
    third=y.pop(0)
    fourth=y.pop(0)
    if first==fourth and sencond==third:
        print(x)
  • The second method is implemented by using a string to obtain a substring. The code length of this scheme is less than that of scheme 1, and the CPU running time displayed by the submitted system is significantly less than scheme 1.
for x in range(1000,10000):
    z=str(x)
    first=z[0:1]
    second=z[1:2]
    third=z[2:3]
    fourth=z[3:4]
    if first==fourth and second==third:
        print(x)

Guess you like

Origin blog.csdn.net/qq_45701131/article/details/104398271