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 be
arrayReplace(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的更多相关文章

  1. Code Signal_练习题_digitDegree

    Let's define digit degree of some positive integer as the number of times we need to replace this nu ...

  2. Code Signal_练习题_alphabeticShift

    Given a string, replace each its character by the next one in the English alphabet (z would be repla ...

  3. Code Signal_练习题_palindromeRearranging

    Given a string, find out if its characters can be rearranged to form a palindrome. Example For input ...

  4. Code Signal_练习题_Knapsack Light

    You found two items in a treasure chest! The first item weighs weight1 and is worth value1, and the ...

  5. Code Signal_练习题_growingPlant

    Each day a plant is growing by upSpeed meters. Each night that plant's height decreases by downSpeed ...

  6. Code Signal_练习题_arrayMaxConsecutiveSum

    Given array of integers, find the maximal possible sum of some of its k consecutive elements. Exampl ...

  7. Code Signal_练习题_differentSymbolsNaive

    Given a string, find the number of different characters in it. Example For s = "cabca", th ...

  8. Code Signal_练习题_firstDigit

    Find the leftmost digit that occurs in a given string. Example For inputString = "var_1__Int&qu ...

  9. Code Signal_练习题_extractEachKth

    Given array of integers, remove each kth element from it. Example For inputArray = [1, 2, 3, 4, 5, 6 ...

随机推荐

  1. jQuery基础笔记(4)

    day55 参考:https://www.cnblogs.com/liwenzhou/p/8178806.html#autoid-1-9-3 文本操作 HTML代码: html()// 取得第一个匹配 ...

  2. Kafka文件存储机制

    一.topic中partition存储分布 在本地的kafka中,我们只启动一个broker,创建两个topic:single-todo和single-todo-vip ,每个topic有两个part ...

  3. Spring MVC+MySQL保存中文变成乱码

    环境:MySQL,Spring MVC3.2.0,jQuery v2.0.3,使用JdbcTemplate访问数据库,相当于全套Spring解决方案. 现象 直接使用表单POST,或者使用jQuery ...

  4. C#里面获取web和非web项目路径

    非Web程序获取路径几种方法如下: 1.AppDomain.CurrentDomain.BaseDirectory  2.Environment.CurrentDirectory 3.HttpRunt ...

  5. JAVA线程本地变量ThreadLocal和私有变量的区别

    ThreadLocal并不是一个Thread,而是Thread的局部变量,也许把它命名为ThreadLocalVariable更容易让人理解一些. 所以,在Java中编写线程局部变量的代码相对来说要笨 ...

  6. TCP-IP 端口号

    FTP服务器的TCP端口号  21 Telnet服务器的TCP端口号  23 TETP(简单文件传输协议)服务器的UDP端口号  69 任何TCP/IP实现所提供的服务都用1-1023之间的端口号 至 ...

  7. jade直接写类似JavaScript语法的东西,不需要写script

    我们知道,html做计算都是在JavaScript中完成的,那么不用JavaScript行不行呢,可以直接在jade中一样的编写 如: -var a = 3 -var b = 4 div a+b = ...

  8. 修改WAMPServer(Apache+PHP+MySQL一键式安装)中mysql默认空密码

    Note:在EclipsePHP中配置WorkSpace时,将工作目录指到执行PHP代码的www目录下 ,便于在Eclipse下编写PHP项目          eg:D:\KelvinSoftwar ...

  9. Java之集合(八)HashMap

    转载请注明源出处:http://www.cnblogs.com/lighten/p/7338372.html 1.前言 本章介绍Java中最常用的一个集合类HashMap,此类在不同的JDK版本有不同 ...

  10. notepad++上搭建gtk+2.0/3.x开发环境

    前言 老师布置了一道题需要用到图形界面,于是开始找图形库.最后选择了gtk+图形库,然后折腾了大概一天. 这里记录自己新学到的知识,同时也给后来者一些便利. 准备 下载以下内容 notepad++(由 ...