zoukankan      html  css  js  c++  java
  • 《Python核心编程》第二版第160页第六章练习 续五 Python核心编程答案自己做的

    (b)代码如下,另外一种做法,逆序查找:
    def rfindchr(string, char):
        a = string
        index = -1
        k = len(a)
        for i in a[::-1]:
            k = k - 1
            if i == char:
                index = k
                print index
                break
        if index == -1: print 'index = ', index
       
    a = raw_input('Please input a string ... ')
    b = raw_input('Please input a character to be find in this string ... ')
    rfindchr(a, b)    

    (c)代码如下:
    def subchr(string, origchar, newchar):
        output = ''
        for i in origchar:
            if i == string:
                output = output + newchar
            else:
                output = output + i
        print output
               
    subchr('c', 'abcddfasdfddacda', 'k')       
           
           

    6-13.
    字符串.string模块包含三个函数,atoi(),atol()和atof(),他们分别负责把字符串转换成整型、长整型和浮点型数字。从Python 1.5起,Python的内建函数int()、long()、float()也可以做同样的事了,本文来,complex()函数可以把字符串转换成复数(然而1.5之前,这些转换函数只能工作于数字之上)自博客园。
    string模块中并没有实现一个atoc()函数,那么你来实现一个atoc(),接受单个字符串做参数输入,一个表示复数的字符串,例如'-1.23e+4-5.67j',返回相应的复数对象。你不能用eval()函数,但可以使用complex()函数,而且你只能在如下的限制之下使用:complex():complex(real, imag)的real和imag都必须是浮点值。
    【答案】
    代码如下:
    def atoc(input):
        print complex(input).real
        print complex(input).imag

    input = raw_input('please input a complex number ... ')
    atoc(input)

  • 相关阅读:
    文件和目录之文件访问权限
    文件和目录之设置用户ID和设置组ID
    文件和目录之文件类型
    文件和目录之stat、fstat和lstat函数
    实用编程技术之多个头文件中变量的重复定义
    带缓冲I/O和不带缓冲I/O的区别与联系
    如何使用nodejs发邮件
    cosmic_download-AsyncPool待修正
    python性能分析之line_profiler模块
    python性能分析之cProfile模块
  • 原文地址:https://www.cnblogs.com/balian/p/2064213.html
Copyright © 2011-2022 走看看