博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Codewars第三天--The museum of incredible dull things
阅读量:4302 次
发布时间:2019-05-27

本文共 2250 字,大约阅读时间需要 7 分钟。

Codewars第三天–The museum of incredible dull things

题目描述:

The museum of incredible dull things wants to get rid of some exhibitions. Miriam, the interior architect, comes up with a plan to remove the most boring exhibitions. She gives them a rating, and then removes the one with the lowest rating.
However, just as she finished rating all exhibitions, she's off to an important fair, so she asks you to write a program that tells her the ratings of the items after one removed the lowest one. Fair enough.
Task
Given an array of integers, remove the smallest value. Do not mutate the original array/list. If there are multiple elements with the same value, remove the one with a lower index. If you get an empty array/list, return an empty array/list.
Don't change the order of the elements that are left.

Examplesremove_smallest([1,2,3,4,5]) = [2,3,4,5]remove_smallest([5,3,2,1,4]) = [5,3,2,4]remove_smallest([2,2,1,2,1]) = [2,2,2,1]

这题需要我们找出给定的一个数组的最小值,并移除他,如果最后数组为空,则返回一个空数组,如果存在多个相同的最小值,则移除下标最小的那个值。最后结果中的数组顺序不能够改变。最最重要的一点就是:

Do not mutate the original array/list.
开始没反应过来这个问题,以为只要数组的顺序不变,就改变没有mutate 原数组。在这里没用用python3自带的min() 来写,用了一个for 循环找最小值的索引。开始代码如下:

def remove_smallest(numbers):    #raise NotImplementedError("TODO: remove_smallest")    if len(numbers) == 0:        return numbers    else:        key = numbers[0]        temp = 0        new_num = []        for i in range(1, len(numbers)):            if numbers[i] < key:                key = numbers[i]                temp = i        del numbers[temp]        return numbers

这样的结果是test用例都通过了,但是出现了一个错误:

check for mutations to original list
Mutated list: [373, 370] should equal [373, 370, 134]
开始一直想不通就去看了论坛,发现很多人都提出了这个问题。解决办法都说去修改自己的代码,因为真的改变了初始的数组。又回头仔细看了下代码,发现我真的改变了原始数组,问题出在我移除最小值的那句语句:del numbers[temp] 。我对初始数组进行了删除操作,将初始数组的元素减少了一个。很多人都忽略了这个问题。因此,在这里不能够直接删除掉原数组的最小值来得到最后的结果,而是需要将最小值找出来,然后越过这个值,将其他值链接起来。代码如下:

def remove_smallest(numbers):    #raise NotImplementedError("TODO: remove_smallest")    if len(numbers) == 0:        return numbers    else:        key = numbers[0]        temp = 0        new_num = []        for i in range(1, len(numbers)):            if numbers[i] < key:                key = numbers[i]                temp = i        return numbers[0:temp] + numbers[temp+1:]

转载地址:http://vmmws.baihongyu.com/

你可能感兴趣的文章
linux下载github中的文件
查看>>
HDP Sandbox里面git clone不了数据(HTTP request failed)【目前还没解决,所以hive的练习先暂时搁置了】
查看>>
动态分区最佳实践(一定要注意实践场景)
查看>>
HIVE—索引、分区和分桶的区别
查看>>
Hive进阶总结(听课总结)
查看>>
大数据领域两大最主流集群管理工具Ambari和Cloudera Manger
查看>>
Sqoop往Hive导入数据实战
查看>>
Mysql到HBase的迁移
查看>>
Sqoop import进阶
查看>>
Hive语句是如何转化成MapReduce任务的
查看>>
Hive创建table报错:Permission denied: user=lenovo, access=WRITE, inode="":suh:supergroup:rwxr-xr-x
查看>>
Hive执行job时return code 2排查
查看>>
hive常用函数及数据结构介绍
查看>>
Hive面试题干货(亲自跟着做了好几遍,会了的话对面试大有好处)
查看>>
力扣题解-230. 二叉搜索树中第K小的元素(递归方法,中序遍历解决)
查看>>
力扣题解-123. 买卖股票的最佳时机 III(动态规划)
查看>>
Django 源码阅读:服务启动(wsgi)
查看>>
Django 源码阅读:url解析
查看>>
Docker面试题(一)
查看>>
第一轮面试题
查看>>