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].

我的解答:

  1. def arrayReplace(inputArray, elemToReplace, substitutionElem):
  2. for i in range(len(inputArray)):
  3. if inputArray[i] == elemToReplace:
  4. inputArray[i] = substitutionElem
  5. return inputArray
  1. def arrayReplace(inputArray, elemToReplace, substitutionElem):
  2. 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. 刚才在windows下发现拖拽不了文件了

    百度了下  摁了两下esc就可以了.以下是百度得到的答案 按几下Esc目的是:没按前ESC键[接触不好]或[键不灵]或其他原因导致ESC处于[按下]状态,这样鼠标就会拖不了文件,点击菜单也会马上消失, ...

  2. 编写 ES6 的 7 个实用技巧

    无脑翻译走一波~ Hack #1 - 变量交换 使用数组解构交换变量的值 let a = 'world', b = 'hello' [a, b] = [b, a] console.log(a) // ...

  3. java中无参,有参,默认构造方法的应用及举例

    类的构造方法: (1)构造方法的名字和类名相同,并且没有返回值. (2)构造方法主要用于为类的对象定义初始化状态. (3)不能直接调用构造方法,必修通过new关键字来自动调用,从而创建类的实例. ne ...

  4. centos7启动顺序加密的问题

    在上一篇博客中我们说的是如何进入单用户模式,这篇我主要来讲centos7启动加密. 首先我们来说centos的启动顺序: 上一篇我们所说的进入单用户模式,就是在boot loader 这一层次下进入的 ...

  5. 使用win10自带邮件应用发送文件

    之前的电脑装过邮件客户端,想发送文件给别人时,只需要“右键文件——发送到邮件”,就能把文件作为附件发送给对方.新电脑win10系统自带邮件客户端,所以就想直接用.但是右键发送到邮件没有关联上,用不了. ...

  6. oracle case when 语句的用法详解

    1. CASE WHEN 表达式有两种形式 复制代码代码如下: --简单Case函数  CASE sex  WHEN '1' THEN '男'  WHEN '2' THEN '女'  ELSE '其他 ...

  7. 【洛谷p3994】Highway 二分+斜率优化DP

    题目大意:给你一颗$n$个点的有根树,相邻两个点之间有距离,我们可以从$x$乘车到$x$的祖先,费用为$dis\times P[x]+Q[x]$,问你除根以外每个点到根的最小花费. 数据范围:$n≤1 ...

  8. 移植C/C++代码的十个技巧

    这篇文章是我翻译自Top 10 tips for code porting c/c++的一篇小短文,以下是翻译全文,如有错误请留言或查阅原文. 代码的可移植性基本上是指使得源代码能够在不同的平台上编译 ...

  9. webpack使用来打包前端代码

    使用webpack打包js文件(隔行变色案例) 1.webpack安装的两种方式 运行npm i webpack -g全局安装webpack,这样就能在全局使用webpack的命令 在项目根目录中运行 ...

  10. 【转】使用SQL Server 2012的FileTable轻松管理文件

    一 .FileStream和FileTable介绍 我们经常需要把结构化数据(int.Char等)和非结构化数据(如Varbinary(max))一起存储,那我们在怎么存储的呢? 1. 在SQL Se ...