"Python Core programming second Edition" after class exercise 6-12 answers

Source: Internet
Author: User

Afternoon brain Watt, thought for a while did not come up with algorithms, just convulsions finally realized, hereby recorded.  ̄- ̄| |

Topic

(a) Create a function with the name FINDCHR (), FINDCHR () to find the character char in string strings, find the index that returns the value, otherwise return-1.

(b) Create another function called RFINDCHR (), looking for the character char last occurrence. It works like FINDCHR (), but it looks forward from the end of the string.

(c) Create a third function, called SUBCHR (), SUBCHR () similar to FINDCHR (), and the difference is that if a matching character is found, the original character is replaced with a new one. Returns the modified string.

Note: You cannot use the String.*find () or String.*index () functions and methods

Def findchr (String, char):    if char in string:        i=0 while        String[i:i+len (char)]!=char:            i+=1        Else:            return i    else:        return-1        def rfindchr (String, char):    if char in string:        i=0 while        i< (Len (String)-len (char) +1):            if String[i:i+len (char)]!=char:                i+=1            Else:                s=i                i+=1        Else:            return s    else:        return-1def subchr (String, Origchar, Newchar):    if Origchar in string:        I =0 while        String[i:i+len (Origchar)]!=origchar:            i+=1        Else:            new_string=string[:i]+newchar+ string[(I+len (Origchar)):]            return new_string    else:        return-1

  

#运行结果print (FINDCHR (' xabcxxxx ', ' abc ') Print (RFINDCHR (' abcxxxxabc ', ' abc ')) Print (SUBCHR (' abcxxxx ', ' abc ', ' Lilip ')) 17lilipxxxx

  

"Python Core programming second Edition" after class exercise 6-12 answers

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.