美文网首页
Python_16_Udacity_Evans_Intro to

Python_16_Udacity_Evans_Intro to

作者: 张一闻 | 来源:发表于2016-05-13 01:43 被阅读22次

    <a href="http://www.jianshu.com/p/54870e9541fc">总目录</a>


    课程页面:https://www.udacity.com/course/intro-to-computer-science--cs101
    授课教师:Dave Evans https://www.cs.virginia.edu/~evans/
    如下内容包含课程笔记和自己的扩展折腾

    课堂笔记

    String index out of range?

    • s='cs'
    • 如果print s[3]会出现error string index out of range
    • 但是如果是print s[3:] 就不会,只会出来empty string
    • a tricky one: 如果s = "", 那么print s[0] 结果是error!

    Find strings in strings

    • 格式1:<search string>.find(target string)
    • if target string is not found: returns -1
    • 例子
    s = "Von Neumann was born Neumann János Lajos \
    (in Hungarian the family name comes first), \
    Hebrew name Yonah, in Budapest, Kingdom of Hungary, \
    which was then part of the Austro-Hungarian Empire, \
    to wealthy Jewish parents of the Haskalah." 
    #source: https://en.wikipedia.org/wiki/John_von_Neumann
    
    s.find("in")
    

    Input:

    print s.find("Von")
    print s[0:]
    print s.find("in")
    print s.find("of")
    print s[126:]
    

    Output:

    0
    Von Neumann was born Neumann János Lajos (in Hungarian the family name comes first), Hebrew name Yonah, in Budapest, Kingdom of Hungary, which was then part of the Austro-Hungarian Empire, to wealthy Jewish parents of the Haskalah.
    43
    126
    of Hungary, which was then part of the Austro-Hungarian Empire, to wealthy Jewish parents of the Haskalah.
    
    • 格式2:<search string>.find(target string, number)
    • 这个number就是相当于从<search string>[number:]的地方开始找
    • 但是需要注意,输出的结果还是count from the starting point of the original search string

    Rounding numbers

    这个练习真的很有意思

    # Given a variable, x, that stores the 
    # value of any decimal number, write Python 
    # code that prints out the nearest whole 
    # number to x.
    # If x is exactly half way between two 
    # whole numbers, round up, so
    # 3.5 rounds to 4 and 2.5 rounds to 3.
    # You may assume x is not negative.
    
    # Hint: The str function can convert any number into a string.
    # eg str(89) converts the number 89 to the string '89'
    
    # Along with the str function, this problem can be solved 
    # using just the information introduced in unit 1.
    
    # x = 3.14159 
    # >>> 3 (not 3.0)
    # x = 27.63 
    # >>> 28 (not 28.0)
    # x = 3.5 
    # >>> 4 (not 4.0)
    

    下面是我的解答,太太太太太不美了:

    x = 3.14159
    
    #ENTER CODE BELOW HERE
    xs = str(x)
    point = xs.find(".")
    integer_x = xs[:point]
    if int(xs[point+1]) >= 5:
        print int(integer_x)+1
    else:
        print int(integer_x)
    

    正确的思路,不用round, int, if, else
    极简的解法,借助数学:

    x = 3.14159
    x = str(x + 0.5)
    point = x.find(".")
    print x[:point]
    

    太美的解法。
    不过针对非大牛的人,实际编程中也不需要太注重美学,还是综合效率第一。

    相关文章

      网友评论

          本文标题:Python_16_Udacity_Evans_Intro to

          本文链接:https://www.haomeiwen.com/subject/omeprttx.html