| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 667 人关注过本帖
标题:请教一个关于RE的问题
只看楼主 加入收藏
JasonKQiao
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2010-11-10
结帖率:0
收藏
 问题点数:0 回复次数:4 
请教一个关于RE的问题
如何用RE来解决这个题呢,O(∩_∩)O谢谢
程序代码:
[color=#0000FF]def stock_price(price):
    '''Parse Stock prices. Create a function that will
decode the old-style fractional stock price. The price
can be a simple floating point number or it can be a
fraction, for example, 4 5/8.

Develop two patterns, one for numbers with optional
decimal places and another for a number with a space
and a fraction. Write a function that accepts a string
and checks both patterns, returning the correct decimal
price for whole numbers (e.g., 14), decimal prices
(e.g., 5.28) and fractional prices (27 1/4).

For the fractional prices (27 1/4), only whole numbers
are allowed in it, and the fractional part must be proper,
that is, it must in the (0,1) range, excluding 0 and 1.
Otherwise, your fuction should return float('nan'),
that is, Not-A-Number.

return a float type object representing the parsed price.

For example,

stock_price('27 1/4') returns float('27.25')
stock_price('27 4/2') returns float('nan')
stock_price('1/2') returns float('0.5')
stock_price('half') returns float('nan')
stock_price('0.1/0.2') returns float('nan')
stock_price('0.2') returns float('0.2')
stock_price('2') returns float('2')
'''
    if price.count('.')==1:
        while price.count('/')==0:# 如果有'.'和'/',那么pretest将其输出为'nan'
           return float(price) #如果有'.'没有'/',那么就是直接转为float
        return float('nan')
    elif price.count('.')==0:
        if  price.count('/')==1:#如果没有'.',有'/'
            b=price.find('/')  #找到'/'和 ' '所在的位置(如果有' '的话)
            c=price.find(' ')
            if price[b-1]<price[b+1] and price.count(' ')==1:#前面的数字小于后面的数字
                return float(int(price[:c])+int(price[c+1:b])/int(price[b+1:]))
            #找到位置后,将string分开并且转成int,此时有' ',那么将' '前面的数字和后面'/'
            #前后的数字相除即可
            elif price[b-1]<price[b+1] and price.count(' ')==0:#前面的数字小于后面的数字
                return float(int(price[:b])/int(price[b+1:]))
            #如果没有' ',那么直接将'/'前后的数字相除即可
            else:
                return float('nan')
            #如果'/'前面的数字大于后面的数字,就输出'nan'
        elif price.count('/')==0: #既没有'.'也没有'/'
            if price.isdigit()==True:#如果是数字,没有' '
                return float(price)
            else: #如果不是数字,包括其中包含' '
                return float('nan')
    else: # 多于两个'.的输入一定无效
        return float('nan')
[/color]            
搜索更多相关主题的帖子: function another example numbers Create 
2010-11-10 08:45
KLML88422
Rank: 2
来 自:火星
等 级:论坛游民
威 望:1
帖 子:34
专家分:20
注 册:2010-11-12
收藏
得分:0 
你到底要解决什么问题?看你写的代码看着很复杂其实很简单你是要什么样的结果?
2010-11-18 09:06
外部三电铃
Rank: 16Rank: 16Rank: 16Rank: 16
来 自:那一年
等 级:贵宾
威 望:57
帖 子:2012
专家分:7306
注 册:2007-12-17
收藏
得分:0 
就是啊,LZ先说明要解决什么问题,把最需要解决的环节指出来,不然光给出一段代码说要解决问题,谁有那么多时间仔细研究你的代码?我们都是有工作的,又不是闲人

那一年,苍井空还是处女
2010-11-18 12:17
KLML88422
Rank: 2
来 自:火星
等 级:论坛游民
威 望:1
帖 子:34
专家分:20
注 册:2010-11-12
收藏
得分:0 
太乱,没看明白!
2011-01-05 15:42
yangfanconan
Rank: 7Rank: 7Rank: 7
等 级:贵宾
威 望:10
帖 子:397
专家分:541
注 册:2009-9-22
收藏
得分:0 
以下是引用JasonKQiao在2010-11-10 08:45:45的发言:

如何用RE来解决这个题呢,O(∩_∩)O谢谢
def stock_price(price):
    '''Parse Stock prices. Create a function that will
decode the old-style fractional stock price. The price
can be a simple floating point number or it can be a
fraction, for example, 4 5/8.
 
Develop two patterns, one for numbers with optional
decimal places and another for a number with a space
and a fraction. Write a function that accepts a string
and checks both patterns, returning the correct decimal
price for whole numbers (e.g., 14), decimal prices
(e.g., 5.28) and fractional prices (27 1/4).
 
For the fractional prices (27 1/4), only whole numbers
are allowed in it, and the fractional part must be proper,
that is, it must in the (0,1) range, excluding 0 and 1.
Otherwise, your fuction should return float('nan'),
that is, Not-A-Number.
 
return a float type object representing the parsed price.
 
For example,
 
stock_price('27 1/4') returns float('27.25')
stock_price('27 4/2') returns float('nan')
stock_price('1/2') returns float('0.5')
stock_price('half') returns float('nan')
stock_price('0.1/0.2') returns float('nan')
stock_price('0.2') returns float('0.2')
stock_price('2') returns float('2')
'''
    if price.count('.')==1:
        while price.count('/')==0:# 如果有'.'和'/',那么pretest将其输出为'nan'
           return float(price) #如果有'.'没有'/',那么就是直接转为float
        return float('nan')
    elif price.count('.')==0:
        if  price.count('/')==1:#如果没有'.',有'/'
            b=price.find('/')  #找到'/'和 ' '所在的位置(如果有' '的话)
            c=price.find(' ')
            if price
                return float(int(price[:c])+int(price[c+1:b])/int(price))
            #找到位置后,将string分开并且转成int,此时有' ',那么将' '前面的数字和后面'/'
            #前后的数字相除即可
            elif price
                return float(int(price[:b])/int(price))
            #如果没有' ',那么直接将'/'前后的数字相除即可
            else:
                return float('nan')
            #如果'/'前面的数字大于后面的数字,就输出'nan'
        elif price.count('/')==0: #既没有'.'也没有'/'
            if price.isdigit()==True:#如果是数字,没有' '
                return float(price)
            else: #如果不是数字,包括其中包含' '
                return float('nan')
    else: # 多于两个'.的输入一定无效
        return float('nan')
            
给你个建议,调试是一个很基本的技能,尤其是写的很乱的时候,自己调试时对自己的一种提升,关于细节不懂的可以问度娘,而思路可以问大家。
2011-01-11 22:58
快速回复:请教一个关于RE的问题
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.016180 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved