Think like computer scientists Python (3)

Source: Internet
Author: User

Fifth chapter conditions and recursion

"//" represents the down-divide method

"%" modulo operator

The code that determines the intertextuality:

#-*-Coding: UTF-8-*-

def First (word):

return word[0]

def Last (word):

return Word[-1]

def Middle(word):

return Word[1:-1]

def Huwen(word):

If Len (word) <=1:

Return True

If first (word)!=last (word):

Return False

Else

Return Huwen (middle (word))

Print (Huwen (' a '))

Print (Huwen ('afbhj'))

Print (Huwen ('adhsuia'))

Print (Huwen ('heeh'))

def Huwen(WORD1,WORD2):

If Len (word1)! = Len (word2):

Return False

I=0

J=len (WORD2)-1

While j>0:

If WORD1[I]!=WORD2[J]:

Return False

I+=1

J-=1

Return True

Print (Huwen ('hkl', 'lkh'))

def Is_huwen(word):

I=0

J=len (Word)-1

While I<=j:

If WORD[I]!=WORD[J]:

Return False

I+=1

J-=1

Return True

Print (Is_huwen ('guug'))

epsilon=0.0000001

Write the following code in Python

#-*-Coding:utf-8-*-

Import Math

def Jiecheng(n):

If n==0:

Return 1

Else

b=jiecheng (n-1)

C=n*jiecheng (n-1)

Return C

def Estimatepi():

D= (2*MATH.SQRT (2))/9801

K=0

Total=0

While True:

E= (Jiecheng (4*k)) * (1103+26390*k)

F= (Jiecheng (k)) **4

g=f* (396** (4*k))

Total1=d*e/g

Total+=total1

If ABS (TOTAL1) <1e-15:

Break

K+=1

Return 1/total

Print (Estimatepi ())

Two methods for traversing strings

Find usage

#-*-Coding: UTF-8-*-

word=' banana '

Print (Word.find ('na')) #2 look for the subscript when a string is encountered for the first time

Print (Word.find ('na', 3)) #4 indicates which subscript to start looking for

Print (Word.find (' A ', 0, 1)) #-1 indicates from which small label to start finding which of the following table ends

Find a word with three repeating letters

#-*-Coding:utf-8-*-

def Liayiyang(word):

Count=0

I=0

While I<len (word)-1:

If WORD[I]==WORD[I+1]:

Count+=1

If count==3:

Return True

I=i+2

Else

Count=0

I=i+1

Return False

def Wenjian():

Fin = open (' c:\\users\\chenxi\\Desktop\\words.txt ')

For line in Fin:

Word=line.strip ()

If Liayiyang (word):

Print (word)

Wenjian ()

Think like computer scientists Python (3)

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.