《面试》 --阿里巴巴数据分析岗面试编程题解析

昨天做阿里非研发岗编程题,虽不是很难,但还是因为个人原因调试失败,特此今日复盘,希望能帮助大家提高。

第一题

选择出行方式,ofo是每1.5公里1元,不足1.5公里的按1.5计算
永安行是使用时间,每分钟0.2元,使用时间不足1分钟按1分钟算。
hellobike是不足2km 1元,不足4km 3元,不足8公里,5元,超过8km 8元,选择合适的出行方式

def  bike_plan(distance, speed):
    # 计算出三种方式的花费
    ofo = distance/1.5
    if ofo > int(ofo):
        ofo = float(int(ofo)+1)

    yongan_1 = distance/speed
    if yongan_1 > int(yongan_1):
        yongan_1 = float(int(yongan_1)+1)
    yongan = 0.2*yongan_1

    if distance<= 2:
        hellobike = 1.0
    elif distance <= 4:
        hellobike = 3.0
    elif distance <= 8:
        hellobike = 5.0
    else:
        hellobike = 8.0

    # 将花费放置在h中
    h = []
    h.append(ofo)
    h.append(yongan)
    h.append(hellobike)
    print h

    # 找出所有值和最小值相同的坐标,放入h1中
    h1 = []
    for i in range(3):
        if h[i] == min(h):
            h1.append(h.index(min(h),i))
    print h1

    **# index(self)
    # index(...)
    # L.index(value, [start, [stop]]) -> integer -- return first index of value.
    # Raises ValueError if the value is not present.
    # 当值不存在的时候,返回的ValueError ,汗当时没考虑到这个问题啊!**

    # 通过坐标找到相对应的骑行方式,放置在h3中,方便最后输出
    h2 = ['ofo','永安行','hellobike']
    h3 = []
    for i in h1:
        h3.append(h2[i])

    print '骑行距离'+str(distance)+'(千米),匀速骑行速度'+str(speed)+'(千米/分钟)最省钱方案:'

    if len(h3) == 1:
        return h3[0] + str(min(h)) +'(元)'
    if len(h3) == 2:
        return h3[0] + str(min(h)) +'(元)和'+h3[1] + str(min(h)) +'(元)'
    if len(h3) == 3:
        return h3[0] + str(min(h)) +'(元)和'+h3[1] + str(min(h)) +'(元)'+h3[2]+ str(min(h)) +'(元)'

_distance = float(raw_input())
_speed = float(raw_input())
res = bike_plan(_distance, _speed)
print res + "\n"
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值