Chapter 8 answers to exercises in Python core programming (1)

Source: Internet
Author: User

8-9 Fibonacci series. the Fibonacci sequence is like 1, 1, 2, 3, 5, 8, 13, 21, and so on. that is to say, the next value is the sum of the first two values in the sequence. write a function. Given N, the return value is the nth Fibonacci number.

# Filename: test8-9.pydef maid (N-1): If n <= 0: Return-1 Elif n <= 2: return 1 else: Return maid (N-2) + maid) # recursive print Fibonacci (6)

8-10. text processing. count the number of vowels, consonants, and words (separated by spaces) in a sentence. ignore special situations of vowels and consonants, such as "H", "Y", and "Qu. additional question: write the code to handle these special situations

# Filename: test8-10.pyimport stringletters = set (string. uppercase) vowels = set ('aeiou') def dealtext (): vlen = 0 clen = 0 text = raw_input ("Enter text :"). upper () WLEN = Len ([word for word in text. split ()]) tlen = sum ([Len (Word) for word in text. split ()]) CQU = text. count ('qu') # count the number of occurrences of special characters and filter out CH = text. count ('H') Cy = text. count ('y') # print CQU, CH, Cy, tlen vlen = Len ([X for X in text if X in vowels]) -In CQU # Qu, 'U' should be excluded from the vowel clen = tlen-vlen-2 * CQU-ch-cy # exclude 'q' u'h 'y' print WLEN, vlen, clen dealtext ()

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.