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 ...
随机推荐
- 微信小程序redirect 到tab不刷新
// 更新2018/11/20:现在小程序的页面栈长度为10 更正 2018/11/20: 经过一段时间的实践,我发现以前方法存在很多问题,比如 getCurrentPages 方法并不在官方的 AP ...
- 【bzoj4240】 有趣的家庭菜园 树状数组
这一题最终要构造的序列显然是一个单峰序列 首先有一个结论:一个序列通过交换相邻的元素,进行排序,最少的交换次数为该序列的逆序对个数 (该结论很久之前打表意外发现的,没想到用上了.....) 考虑如何构 ...
- POJ 2245
//此题一看便是简单的回溯题用DFS+回溯便可以做出来了. #include <iostream> #define MAXN 20 using namespace std; int _m[ ...
- Android UiAutomator UiDevice API
UiDevice为单例模式 1.获取设备 static UiDevice getInstance() This method is deprecated. Should use getInstance ...
- Kubernetes使用GlusterFS实现数据持久化
k8s中部署有状态应用等需要持久化数据的应用,必不可少得用存储,k8s支持很多中存储方案,我司目前使用的存储有glusterfs(分为容器化和裸机方式).nfs供应用选用,本次就简单实战下gluste ...
- (转)AIX的Dump文件学习笔记
原文:http://czmmiao.iteye.com/blog/1144999 DUMP文件概述 为了增强故障分析能力,IBM的服务器增加了对设备故障当前环境的保存功能,就是保存一份设备故障时的内存 ...
- Eclipse *版本(图文详解)
不多说,直接上干货! 关于Eclipse的版本介绍 Eclipse Standard 该版本是eclipse最基础的版本,适合Java se个人开发者.或希望根据自己需求配置插件的开发者使用. Ecl ...
- wordpress 后台404解决办法
1.vim /usr/local/nginx/conf/wordpress.conf2.rewrite /wp-admin$ $scheme://$host$uri/ permanent;3.ngni ...
- Python -- 图片处理
使用PIL库 转换图片格式(jpg --> png) from PIL import Image Image.open('E:/art.jpg').save('E:/art.png')
- 关于C++中操作符重载的疑问 :四个运算符=, ->, [], ()不可以重载为全局函数(友员函数)
转载自:http://blog.csdn.net/u014610226/article/details/47679323 以下是对C++中不能重载为友元函数的四个运算符进行了详细的分析介绍,需 ...