您的位置:首页 > 职场人生

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

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

第一题

选择出行方式,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"
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  阿里巴巴 面试