【Kata Daily 190923】Odder Than the Rest(找出奇数)
题目:
Create a method that takes an array/list as an input, and outputs the index at which the sole odd number is located.
This method should work with arrays with negative numbers. If there are no odd numbers in the array, then the method should output -1.
Examples:
odd_one([2,4,6,7,10]) # => 3
odd_one([2,16,98,10,13,78]) # => 4
odd_one([4,-8,98,-12,-7,90,100]) # => 4
odd_one([2,4,6,8]) # => -1
题目大意:给定一个list,找出唯一的那个奇数的位置
解法:
def odd_one(arr):
# Code here
for x in arr:
if x%2:
return arr.index(x)
return -1
看一下另外的解法:
def odd_one(arr):
for i in range(len(arr)):
if arr[i] % 2 != 0:
return i
return -1
知识点:
1、获取list的索引,可以使用index的方法,也可以使用arr[i]通过获取i的值来获取
2、判断是否为奇数偶数时,可以使用if x%2来判断或者和0进行相不相等来判断
【Kata Daily 190923】Odder Than the Rest(找出奇数)的更多相关文章
- 【Kata Daily 190919】Sort Out The Men From Boys(排序)
题目: Scenario Now that the competition gets tough it will Sort out the men from the boys . Men are th ...
- 【Kata Daily 190909】The Supermarket Queue(超市队列)
题目: There is a queue for the self-checkout tills at the supermarket. Your task is write a function t ...
- 【Kata Daily 190908】How Much?
原题: I always thought that my old friend John was rather richer than he looked, but I never knew exac ...
- Entity Framework 6 Recipes 2nd Edition(9-3)译->找出Web API中发生了什么变化
9-3. 找出Web API中发生了什么变化 问题 想通过基于REST的Web API服务对数据库进行插入,删除和修改对象图,而不必为每个实体类编写单独的更新方法. 此外, 用EF6的Code Fri ...
- 使用T-SQL找出执行时间过长的作业
有些时候,有些作业遇到问题执行时间过长,因此我写了一个脚本可以根据历史记录,找出执行时间过长的作业,在监控中就可以及时发现这些作业并尽早解决,代码如下: SELECT sj.name , ...
- [LeetCode] Find All Numbers Disappeared in an Array 找出数组中所有消失的数字
Given an array of integers where 1 ≤ a[i] ≤ n (n = size of array), some elements appear twice and ot ...
- [LeetCode] Find All Duplicates in an Array 找出数组中所有重复项
Given an array of integers, 1 ≤ a[i] ≤ n (n = size of array), some elements appear twice and others ...
- [LeetCode] Find Median from Data Stream 找出数据流的中位数
Median is the middle value in an ordered integer list. If the size of the list is even, there is no ...
- 如何找出标有"App Store 精华","Essentials"的所有软件?
如何找出标有"App Store 精华","Essentials"的所有软件? 中国区: +"App Store 精华" site:http ...
随机推荐
- 091 01 Android 零基础入门 02 Java面向对象 02 Java封装 01 封装的实现 03 # 088 01 Android 零基础入门 02 Java面向对象 02 Java封装 02 static关键字 01 static关键字(上)
091 01 Android 零基础入门 02 Java面向对象 02 Java封装 01 封装的实现 03 # 088 01 Android 零基础入门 02 Java面向对象 02 Java封装 ...
- Java基础系列-RandomAccess
原创文章,转载请标注出处:https://www.cnblogs.com/V1haoge/p/10755424.html Random是随机的意思,Access是访问的意思,合起来就是随机访问的意思. ...
- 第0天 | 12天搞定Pyhon,前言
依稀记得,在2014年的某一天,一位运营电商平台的多年好朋友,找我帮忙:一个月内,实现抓取竞争对手在某电商平台上的所有产品信息并统计每个产品的点击率. 说出来有些不好意思,那些年,参与过的产品挺多的, ...
- 手把手教你AspNetCore WebApi:缓存(MemoryCache和Redis)
前言 这几天小明又有烦恼了,系统上线一段时间后,系统性能出现了问题,马老板很生气,叫小明一定要解决这个问题.性能问题一般用什么来解决呢?小明第一时间想到了缓存. 什么是缓存 缓存是实际工作中非常常用的 ...
- 机器学习:集成学习:随机森林.GBDT
集成学习(Ensemble Learning) 集成学习的思想是将若干个学习器(分类器&回归器)组合之后产生一个新学习器.弱分类器(weak learner)指那些分类准确率只稍微好于随机猜测 ...
- RLP序列化算法
RLP RLP(Recursive Length Prefix)递归长度前缀编码,是由以太坊提出的序列化/反序列化标准,相比json格式体积更小,相比protobuf对多语言的支持更强. RLP将数据 ...
- spring boot:接收数组参数及多文件混合json参数(spring boot 2.3.4)
一,生产环境中的复杂参数上传的场景 1,保存排序值 : 例如:某一件商品的多张展示图片排序,提交的排序值要和图片的id相对应 2,上传多张图片,图片要和指定的变量相对应 例如:在添加商品sku时, 需 ...
- 笔记之Utility.DataAccess
挤出时间看了一些代码,做一些笔记,备忘!!! 现在ORM随处可见,为什么不要已有的ORM而要手动写SQL呢?这肯定是有因为滴,存在必合理嘛! 自认为关于性能.维护.Maybe还有其他的,欢迎大家拍砖! ...
- python第一章:基础
1.数学操作符: 数学操作符与数学基本类似 最高级:** 第二级:*././/.% 第三级:+ .- 遵循从左到右的顺序 如果想改变优先级 可以使用括号,比如:(3+2)*(5-4)=5*1=5 2. ...
- PS矢量工具
4.1PS矢量及位图 (1)位图就是像素图,由一个个像素点组成:矢量图是记录点到点的连线或者说程序算出来的图. (2)位图放大很多倍之后就会失真,可以看到像素点,类似于马赛克,所以有分辨率这一说. ( ...