Python Mini-Exercises

Source: Internet
Author: User

Requirement 1:

ABCD * 9 = DCBA

By calculating the method, we calculate a =?  B =?  C =? D =?

A:1-9

B:0-9

C:0-9

D:1-9

A! = B! = C! = D

Code:

Class countnumber (object):     def __init__ (self):         print ("abcd * 9 = dcba; a != b != c !=  d ")     def numabcd (self):         for  a in range (1, 10):             for b in range (0, 10):                 for c in range (0, 10):                     for d in  range (1, 10):                         if  ( a * 1000 + b  * 100 + c&nbsP;* 10 + D)  * 9 ==  (d * 1000 + c * 100 +  b * 10 + a):                             print ("{0}{1}{ 2}{3} * 9 = {4}{5}{6}{7} ". Format (A, b, c, d, d, c, b, a ))                              print ("A = {0}, b = {1},  C = {2}, D = {3} ". Format (a, b, c, d)) Def main ():     countnumber = countnumber ()     COUNTNUMBER.NUMABCD () if __ name__ ==  ' __main__ ':     main ()



Requirement 2:

_____________

|_a_|_b_|_c_|

|_d_|_e_|_f_|

|_g_|_h_|_i_|


A, B, C, D, E, F, G, H, I must be 1-9 digits and cannot be duplicated

All rows, columns, diagonal and both are 15

Class ninepaper (object):     def __init__ (self):         self.numbers = list ()         for i  in range (1, 10):             Self.numbers.append (i)         print ("numbers = {0}". Format ( Self.numbers)     def run (self):         for  a in range (1, 10):             l1 = list ()             l1 +=  self.numbers            l1.remove (A)              for B in l1:         &nBsp;       l2 = list ()                  l2 += l1                 l2.remove (B)                  for C in l2:                     l3  = list ()                      l3 += l2                     l3.remove (C)                      FOR D IN L3:                          l4 = list ()                          l4 += l3                          l4.remove (D)                          for E in l4:                              l5 = list ()                               l5 += l4                             l5.remove (E)                               for F in l5:                                  l6 = list ()                                   l6 += l5                                  l6.remove (F)                                  for g in l6 :                                     l7  = list ()                                       l7 += l6                                      l7.remove (G)                                       for H in l7:                                          l8 = list ()                                           l8 += l7                                          l8.remove (H)                                           for I in l8:                                               if a+b+c == d+e+f == g+h+i == a+d+g  == B+E+H == C+F+I == A+E+I == C+E+G == 15:                                                   print ("                                              _____________                                               |_{0}_|_{1}_|_{2}_|                                              |_{3}_|_{4}_|_ {5}_|                                              |_{6}_|_{7}_|_{8}_|                                                    ". Format (a, b, c, d, e, f, g, h, i)) Def main ():     ninepaper = ninepaper ()     ninepaper.run () if __name __ ==  ' __main__ ':     main ()



Requirement 3:

Writes the contents of the file passwd to the new file and sorts it, the new file is named NEWPASSWD

IMPORT CODECSIMPORT OSCLASS SORTPASSWD (object):     def __init__ (self):         self.passwd =  "passwd"          self.newpasswd =  "NEWPASSWD"          Self.contextlist = list ()         if not  Os.path.exists (SELF.PASSWD):             print (" Please download passwd from linux. ")             exit (1)          print ("sort file is :{0}". Format (SELF.PASSWD))          print ("sorted file is :{0}". Format (SELF.NEWPASSWD))      Def getcontextlist (self):         with  Codecs.open ("passwd")  as fr:             self.contextlist += sorted (Fr.readlines (),  key=lambda line:int (Line.split (":") [2]),  reverse=false)     def writecontextlist (self):         with codecs.open ("new_passwd",  "W")  as fw:             fw.writelines (self.contextlist) def main ():     SORTPASSWD = SORTPASSWD ()     sortpasswd.getcontextlist ()      sortpasswd.writecontextlist () if __name__ ==  ' __main__ ':     main ()


Python Mini-Exercises

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.