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 ...
随机推荐
- 二,mysql优化——sql优化基本概念
1,SQL优化的一般步骤 (1)通过show status命令了解各种SQL执行效率. (2)通过执行效率较低的SQL语句(重点select). (3)通过explain分析低效率的SQL语句的执行情 ...
- 三,PHP缓存机制实现页面静态化
页面静态化思路: 因为新闻这种信息对实时性要求不高,并且比较稳定,所以可以这样做:当地一个用户访问某条新闻后,我们使用ob缓存机制,将内容缓存到html页面.当下一次访问时候,直接访问html页面.这 ...
- day 70 crm(7):stark组件调用,以及权限分配
前情提要: 复习: 1: orm !!!!! 2: session 3: django 4: 前端在复习 5: 复习中间件 学习的stark 的组件调用,以及权限的应用 一:权限的概念, 1: ...
- #阿里云#云服务器部署可道云(KodExplorer)
前言:在做一些项目的时候,经常有一些文档交流,修改之后的文档在QQ或微信上发来发去,还要下载,很是不爽,有一个挺有用的东西叫做KodExplorer可道云. kodexplorer可道云是目前国内有代 ...
- Centos6.7安装Pycharm及升级JDK
首先到pycharm官网下载pycharm压缩包 wget https://www.jetbrains.com/pycharm/download/download-thanks.html?platfo ...
- ASP.NET站点Windows身份验证集成AD域,非LDAP
站点集成AD域验证 服务器机器入域 计算机右键属性-->“更改设置”-->“更改”-->填写所属域,确认后重启机器生效. 部署测试站点,localhost.ip.域名三种方式登录效果 ...
- svn新增文件时自动给文件设置强制只读属性needs-lock
1.从SVN客户端的“设置”->常规设置-> Subversion->Subversion 配置文件-> 编辑按钮 -> 打开配置文件 2.找到[miscellany], ...
- CountDownTimer倒计时器的使用
以前好多倒计时的需求都需要自己去写,今天发现android 原来自带了倒计时的类CountDownTimer,和适合用于发送短信 ,等待验证码的情况 代码展示了在一个TextView进行60S的倒计时 ...
- as和is,但is也有as所没有的功能[C#] --转载 甘木
在C#中,我们可以使用is as操作符来判断某数据类型和其它指定类型的关系 1. 用is运算符验证基础类型 C#允许在继承链中向下转型,所以如果DerivedClass派生自BaseClass,那么D ...
- 根据屏幕尺寸计算rem
!(function (doc, win) { var docEle = doc.documentElement, evt = "onorientationchange" in w ...