Code Signal_练习题_Sort by Height
Some people are standing in a row in a park. There are trees between them which cannot be moved. Your task is to rearrange the people by their heights in a non-descending order without moving the trees. People can be very tall!
Example
For a = [-1, 150, 190, 170, -1, -1, 160, 180]
, the output should besortByHeight(a) = [-1, 150, 160, 170, -1, -1, 180, 190]
.
我的解答:
def sortByHeight(a):
j = -2
y = 0
li = []
if -1 not in a:
a = sorted(a)
else:
for i in a:
if i == -1:
pass
else:
li.append(i)
a[a.index(i)] = j
j -= 1
li = sorted(li)
for x in a:
if x != -1:
a[a.index(x)] = li[y]
y += 1
return a
感觉自己总是不按常规出牌
膜拜大佬:
def sortByHeight(a): l = sorted([i for i in a if i > 0])
for n,i in enumerate(a):
if i == -1:
l.insert(n,i)
return l
Code Signal_练习题_Sort by Height的更多相关文章
- Code Signal_练习题_growingPlant
Each day a plant is growing by upSpeed meters. Each night that plant's height decreases by downSpeed ...
- Code Signal_练习题_digitDegree
Let's define digit degree of some positive integer as the number of times we need to replace this nu ...
- Code Signal_练习题_Knapsack Light
You found two items in a treasure chest! The first item weighs weight1 and is worth value1, and the ...
- Code Signal_练习题_arrayMaxConsecutiveSum
Given array of integers, find the maximal possible sum of some of its k consecutive elements. Exampl ...
- Code Signal_练习题_differentSymbolsNaive
Given a string, find the number of different characters in it. Example For s = "cabca", th ...
- Code Signal_练习题_firstDigit
Find the leftmost digit that occurs in a given string. Example For inputString = "var_1__Int&qu ...
- Code Signal_练习题_extractEachKth
Given array of integers, remove each kth element from it. Example For inputArray = [1, 2, 3, 4, 5, 6 ...
- Code Signal_练习题_stringsRearrangement
Given an array of equal-length strings, check if it is possible to rearrange the strings in such a w ...
- Code Signal_练习题_absoluteValuesSumMinimization
Given a sorted array of integers a, find an integer x from a such that the value of abs(a[0] - x) + ...
随机推荐
- Storm的并行度
在Storm集群中,运行Topolopy的实体有三个:工作进程,executor(线程),task(任务),下图可以形象的说明他们之间的关系. 工作进程 Storm集群中的一台机器会为一个或则多个To ...
- [nuget]VS中包管理器打开后找不到其它工程的问题
今天新建工程做小组内用的工具,打算做个winform的项目, 用vs新建了winform项目,简单分下层吧,又加了两个类库项目, 然后,要用到的包需要nuget安装,于是发生这个问题: [VS]在so ...
- Linux 套接字通信笔记(一)
协议 TCP(传输控制协议),UDP(用户数据包协议)为传输层重要的两大协议,向上为HTTP提供底层协议,向下为数据链路层封装底层接口,乃是通信重中之重.TCP是面向流传输的协议,在编程中形象化为St ...
- WWDC: Thread Sanitizer and Static Analysis
Thread Sanitizer 过程 编译过程中链接了一个新的库.  也可以通过命令行来操作: $ clang -fsanitize=thread source.c -o executable $ ...
- 指定nginx某个目录显示目录结构
1.修改配置文件/usr/local/nginx/conf/nginx.conf 指定目录,开启autoindex为on. location /study { autoindex on; } 2. 保 ...
- day 49 数据分析, 数据聚合 F 查询 Q 查询
6.聚合查询和分组查询 1.聚合查询aggregate 我们先通过一个例子来感受一下吧. 1 2 3 # 计算所有图书的平均价格 books = models.Book.objects.aggrega ...
- Yarn 和 Npm 命令行切换 摘录
原文作者: @Gant Laborde原文地址: https://shift.infinite.red/np...中文翻译: @文蔺译文地址:http://www.wemlion.com/2016/n ...
- Observer观察者设计模式
Observer设计模式主要包括以下两种对象: (1)被观察对象:Subject,它往往包含其他对象感兴趣的东西,上面例子中热水器中就是Subject(被监视对象); (2)观察对象:Observer ...
- 视口(viewport)原理详解之第二部分(移动端浏览器)
一.移动端浏览器的问题 当我们把移动端浏览器和桌面浏览器比较时,最明显的差异就是尺寸.移动端浏览器尺寸要比桌面屏幕小得多,移动浏览器最多差不多也就400px.最重要的问题集中在我们的CSS上,特别是v ...
- Impala查询详解
Impala的定位是一种新型的MPP查询引擎,但是它又不是典型的MPP类型的SQL引擎,提到MPP数据库首先想到的可能是GreenPlum,它的每一个节点完全独立,节点直接不共享数据,节点之间的信息传 ...