Python rewrite C language program 100 example -- Part8

Source: Internet
Author: User

''' [Program 61] Question: print out the Yang Hui triangle (10 rows required for printing) 1. program Analysis: ''' if _ name _ = '_ main _': a = [] for I in range (10):. append ([]) for j in range (10): a [I]. append (0) for I in range (10): a [I] [0] = 1 a [I] [I] = 1 for I in range (2, 10 ): for j in range (1, I ): a [I] [j] = a [I-1] [J-1] + a [I-1] [j] from sys import stdout for I in range (10 ): for j in range (I + 1): stdout. write (a [I] [j]) stdout. write ('') print

'''Question: draw an elliptical ellipse 1. program Analysis: 2. program source code: ''' if _ name _ = '_ main __': from Tkinter import * x = 360 y = 160 top = y-30 bottom = y-30 canvas = Canvas (width = 400, height = 600, bg = 'white ') for I in range (20): canvas. create_oval (250-top, 250-bottom, 250 + top, 250 + bottom) top-= 5 bottom + = 5 canvas. pack () mainloop ()

'''Question: Use ellipse and rectangle to draw a picture. 1. program Analysis: 2. program source code: '''if _ name _ = '_ main _': from Tkinter import * canvas = Canvas (width = 400, height = 600, bg = 'white') left = 20 right = 50 top = 50 num = 15 for I in range (num): canvas. create_oval (250-right, 250-left, 250 + right, 250 + left) canvas. create_oval (250-20,250-top, 250 + 20,250 + top) canvas. create_rectangle (20-2 * I, 20-2 * I, 10 * (I + 2), 10 * (I + 2 )) right + = 5 left + = 5 top + = 10 canvas. pack () mainloop ()

''' [Program 65]: an optimal pattern. 1. program Analysis: 2. program source code: ''' import mathclass PTS: def _ init _ (self): self. x = 0 self. y = 0 points = [] def LineToDemo (): from Tkinter import * screenx = 400 screeny = 400 canvas = Canvas (width = screenx, height = screeny, bg = 'white ') aspectRatio = 0.85 MAXPTS = 15 h = screeny w = screenx xcenter = w/2 ycenter = h/2 radius = (h-30)/(AspectRatio * 2) -20 step = 360/MAXPTS angle = 0.0 for I in range (MAXPTS): rads = angle * math. pig/180.0 p = PTS () p. x = xcenter + int (math. cos (rads) * radius) p. y = ycenter-int (math. sin (rads) * radius * AspectRatio) angle + = step points. append (p) canvas. create_oval (xcenter-radius, ycenter-radius, xcenter + radius, ycenter + radius) for I in range (MAXPTS): for j in range (I, MAXPTS): canvas. create_line (points [I]. x, points [I]. y, points [j]. x, points [j]. y) canvas. pack () mainloop () if _ name _ = '_ main _': LineToDemo ()

''' [Program 66] Question: Enter 3 numbers a, B, c, and output in order of size. 1. Program Analysis: Use the pointer method. 2. program source code: ''' if _ name _ = '_ main _': n1 = int (raw_input ('n1 =: \ n ')) n2 = int (raw_input ('n2 =: \ n') n3 = int (raw_input ('n3 =: \ n') def swap (p1, p2 ): return p2, p1 if n1> n2: n1, n2 = swap (n1, n2) if n1> n3: n1, n3 = swap (n1, n3) if n2> n3: n2, n3 = swap (n2, n3) print n1, n2, n3

'''Question: Input array. The largest one is exchanged with the first element, the smallest one is exchanged with the last element, and the output array. 1. program analysis: the answer in Tan haoqiang's book is problematic. 2. program source code: ''' def 6B (numbers): for I in range (9): numbers. append (int (raw_input ('input a number: \ n') numbers. append (int (raw_input ('input a number: \ n') p = 0def max_min (array): max = min = 0 for I in range (1, len (array)-1): p = I if array [p]> array [max]: max = p elif array [p] <array [min]: min = p k = max l = min array [0], array [l] = array [l], array [0] array [9], array [k] = array [k], array [9] def outp (numbers): for I in range (len (numbers )): print numbers [I] if _ name _ = '_ main _': array = [] Indium (array) max_min (array) outp (array)

''' [Program 68] Question: There are n integers, so that the numbers in the front are shifted to the back m positions, and the last m number is changed to the front m number 1. program Analysis: 2. program source code: ''' if _ name _ = '_ main _': n = int (raw_input ('the total number is: \ n ')) m = int (raw_input ('back m: \ n') def move (array, n, m ): array_end = array [n-1] for I in range (n-1,-1,-1 ): array [I] = array [I-1] array [0] = array_end m-= 1 if m> 0: move (array, n, m) number = [] for I in range (n): number. append (int (raw_input ('input a number: \ n') print 'originnumber: ', number move (number, n, m) print 'after moved :', number

''' [Program 69] Question: There are n people in a circle with sequential troubleshooting. When the first person reports the number (from 1 to 3), the person who reports the number 3 leaves the circle and asks the last person who left the number. 1. program Analysis: 2. program source code: ''' if _ name _ = '_ main _': nmax = 50 n = int (raw_input ('Please input the total of numbers: ') num = [] for I in range (n): num. append (I + 1) I = 0 k = 0 m = 0 while m <n-1: if num [I]! = 0: k + = 1 if k = 3: num [I] = 0 k = 0 m + = 0 I + = 1 if I = n: I = 0 I = 0 while num [I] = 0: I + = 1 print num [I]

'''Question: Write a function, calculate the length of a string, input a string in the main function, and output its length. 1. program Analysis: 2. the program source code is as follows: '''if _ name _ = '_ main _': s = raw_input ('Please input a string: \ n ') print 'the string has % d characters. '% len (s)



Related Article

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.