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. python http post json

    直接上代码吧 #coding=utf-8 import os import urllib import urllib2 import re import cookielib import json h ...

  2. poj1122

    FDNY to the Rescue! Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 2917   Accepted: 89 ...

  3. 关于finecms v5 会员头像 任意文件上传漏洞分析

    看到我私藏的一个洞被别人提交到补天拿奖金,所以我干脆在社区这里分享,给大家学习下 本文原创作者:常威,本文属i春秋原创奖励计划,未经许可禁止转载! 1.定位功能 下载源码在本地搭建起来后,正常登陆了用 ...

  4. js闭包之我见

    很久前的一个问题终于得以解决,内心是无比喜悦的,不多说,先上代码: function test(){ for(var i=0;i<5;i++){ window.onclick=function( ...

  5. ansible 的第一次亲密接触

    如何添加一台机器 编辑 /etc/ansible/hosts 添加本机的 public ssh key 到目标机器的 authorized_keys 添加本机的 私钥 到 ansible 运行 ans ...

  6. ES6字符串相关扩展

    变量的解构赋值 // 数组的解构赋值 let [a,b,c] = [1,2,3]; //1,2,3 let [a,b,c] = [,123,]; //undefined 123 undefined l ...

  7. grub覆盖mbr引导系统

    grub覆盖mbr引导系统 0.个人PC,WIN 7 + Kali,easybcd 不起作用,需要制作 kali 安装盘 PS:推荐使用 universal usb installer 制作. 方案一 ...

  8. POJ 1056

    #include <iostream> #include <string> #define MAXN 50 using namespace std; struct node { ...

  9. Codeforces Round #556 (Div. 2)

    比赛链接 A 贪心 #include <cstdlib> #include <cstdio> #include <algorithm> #include <c ...

  10. 机器大数据也离不开Hadoop

    转自:http://gtstorageworld.blog.51cto.com/908359/1286758 根据数据来源划分,大数据主要包括三类:商业运作产生的数据.人类行为产生的数据和机器数据.目 ...