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. Django(app的概念、ORM介绍及编码错误问题)

    day61 Django中的APP:         什么是APP?以及为什么要用APP?                  project  --> 项目  (老男孩教育大学校)        ...

  2. MySQL(ORM框架)

    day63 参考:http://www.cnblogs.com/wupeiqi/articles/5713330.html SQLAlchemy本身无法操作数据库,其必须以来pymsql等第三方插件, ...

  3. 使用VS Code开发.Net Core 2.0 MVC Web应用程序教程之三(配置文件读取)

    干了一天的活,还有点时间,给兄弟们写点东西吧. 大家有没有发现一个问题?那就是在.Net Core的MVC项目里面,没有.config文件了!!!同志们,没有config文件了啊,这样搞,我以后要做些 ...

  4. git 查看提交记录

    查看提交的内容 -p 选项,同时在 - 后加数字限制一下数目 git log -p -2. commit 500eeadd71a21f1166803e12a792bfa86f4ca784 (HEAD ...

  5. D11——C语言基础学PYTHON

    C语言基础学习PYTHON——基础学习D11 20180908内容纲要: 1.RabbitMQ消息队列 (1)RabbitMQ安装 (2)Rabbits示例 模式一:fanout 模式二:direct ...

  6. 下载Centos7 64位镜像

    下载Centos7 64位镜像 1.打开Centos官网 打开Centos官方网站地址:https://www.centos.org/,点击Get CentOS Now 2.点击Minimal ISO ...

  7. POJ 1183

    #include<iostream> #include<stdio.h> using namespace std; int main() { //freopen("a ...

  8. Redis客户端使用

    http://wenku.baidu.com/view/6ccd650af12d2af90242e63d.html 一.下载jedis 代码 jedis 代码地址:https://github.com ...

  9. github的本地配置和项目创建

    之前完成了github的安装和账号的注册,接下来要进行项目的创建和本地代码仓库的建立 1.创建项目 2.填写项目相关信息 注意:在给项目起名时,尽量起一些有意义的名字,否则会被管理员删除.因为服务器上 ...

  10. Java之IO(零)总结

    转载请注明原出处:http://www.cnblogs.com/lighten/p/7274378.html 1.前言 本章是对之前所讲述的整个Java的IO包的一个总结,抽出个人认为比较重要的知识点 ...