Code Signal_练习题_Array Replace
Given an array of integers, replace all the occurrences of elemToReplace with substitutionElem.
Example
For inputArray = [1, 2, 1], elemToReplace = 1, and substitutionElem = 3, the output should bearrayReplace(inputArray, elemToReplace, substitutionElem) = [3, 2, 3].
我的解答:
def arrayReplace(inputArray, elemToReplace, substitutionElem):
for i in range(len(inputArray)):
if inputArray[i] == elemToReplace:
inputArray[i] = substitutionElem
return inputArray
def arrayReplace(inputArray, elemToReplace, substitutionElem):
return [substitutionElem if x==elemToReplace else x for x in inputArray]
膜拜大佬
Code Signal_练习题_Array Replace的更多相关文章
- 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_练习题_alphabeticShift
Given a string, replace each its character by the next one in the English alphabet (z would be repla ...
- Code Signal_练习题_palindromeRearranging
Given a string, find out if its characters can be rearranged to form a palindrome. Example For input ...
- 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_练习题_growingPlant
Each day a plant is growing by upSpeed meters. Each night that plant's height decreases by downSpeed ...
- 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 ...
随机推荐
- Python 去除列表中重复的元素
Python 去除列表中重复的元素 来自比较容易记忆的是用内置的set l1 = ['b','c','d','b','c','a','a'] l2 = list(set(l1)) print l2 还 ...
- 【wireshark】抓包和文件格式支持
1. 抓包 捕获从网络适配器提取包,并将其保存到硬盘上. 访问底层网络适配器需要提升的权限,因此和底层网卡抓包的功能被封装在dumpcap中,这是Wireshark中唯一需要特权执行的程序,代码的其他 ...
- Understanding Undefined Behavior
"undefined behavior: behavior for which this International Standard imposes no requirements.&qu ...
- 关于Ubuntu的默认python版本
大部分Ubuntu系统默认python版本都是python2.x系列,但最新版本已经是3.5和3.6了,软件系统跟着版本走总是有诸多好处的,所以,以下是作者在修改Ubantu默认python版本时的一 ...
- C语言 for循环次数
for (i = 0;i < n;i++) 则循环次数是N,而循环结束以后,i的值是n.循环的控制变量i,是选择从0开始还是从1开始,是判断i<n 还是i <= n,对循环的次数,循 ...
- 剑指offer四十六之孩子们的游戏(圆圈中最后剩下的数,约瑟夫环问题)
一.题目 每年六一儿童节,牛客都会准备一些小礼物去看望孤儿院的小朋友,今年亦是如此.HF作为牛客的资深元老,自然也准备了一些小游戏.其中,有个游戏是这样的:首先,让小朋友们围成一个大圈.然后,他随机指 ...
- Fiddler4抓包工具使用教程一
本文参考自http://blog.csdn.net/ohmygirl/article/details/17846199,纯属读书笔记,加深记忆 1.抓包工具有很多,为什么要使用Fiddler呢?原因如 ...
- wordpress常用标记
博客地址:bloginfo('url'); 博客名称:bloginfo("name"); 博客描述:bloginfo("decription"); 文章内容: ...
- Python -- 网络编程 -- 认识Python3的urllib库
Python3的urllib包含5个模块 urllib error parse request response robotparser 各个模块的主要成员: error ['ContentTooSh ...
- jetty9优化的两处地方
http://www.cnblogs.com/LBSer/p/3637387.html jetty 9两个优化: https://webtide.intalio.com/2013/01/jetty-9 ...