【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 ...
随机推荐
- Java知识系统回顾整理01基础01第一个程序07Eclipse使用----找不到类如何解决?
一.现象 有时候会碰到如图所示的问题,分明有Hello这个类,并且也有主方法,可是运行就会出现找不到或者无法加载类Hello,或者Class Not Found 异常. 出现这个状况,有多种原因造成, ...
- 03 ArcPython实战篇一
1.自增计算 (字段计算器) total = 0 def accumulate(increment): global total if total: ...
- 01 Arcgis10.6 安装教程
一.ArcGIS系统要求 包括: Win7 SP1(及以上) 32/64位系统 Win8.1 32/64位系统 Win10 32/64位系统 二.下载ArcGIS 10.6安装文件 链接:https: ...
- C语言&C++ 中External dependencies
参考:https://blog.csdn.net/yyyzlf/article/details/4419593 External Dependencies是说你没有把这个文件加入到这个工程中,但是 ...
- PADS Layout VX.2.3 灌铜之后没有显示整块铜皮的原因
操作系统:Windows 10 x64 工具1:PADS Layout VX.2.3 灌铜之后没有显示整块铜皮,如下图所示: 点击菜单Tools > Options...(快捷键:Ctrl + ...
- 【手摸手,带你搭建前后端分离商城系统】01 搭建基本代码框架、生成一个基本API
[手摸手,带你搭建前后端分离商城系统]01 搭建基本代码框架.生成一个基本API 通过本教程的学习,将带你从零搭建一个商城系统. 当然,这个商城涵盖了很多流行的知识点和技术核心 我可以学习到什么? S ...
- MeteoInfoLab脚本示例:Trajectory
示例读取HYSPLIT模式输出的气团轨迹数据文件,生成轨迹图层,并显示轨迹各节点的气压图.脚本程序: f = addfile_hytraj('D:/MyProgram/Distribution/jav ...
- MeteoInfoLab脚本示例:闪电位置图
这个脚本示例读取文本格式的闪电数据,读出每条闪电记录的经纬度和强度,在地图上绘制出每个闪电的位置,并用符号和颜色区分强度正负.数据格式如下:0 2009-06-06 00:01:16.6195722 ...
- 【值得收藏】C语言入门基础知识大全!从C语言程序结构到删库跑路!
01 C语言程序的结构认识 用一个简单的c程序例子,介绍c语言的基本构成.格式.以及良好的书写风格,使小伙伴对c语言有个初步认识. 例1:计算两个整数之和的c程序: #include main() { ...
- vbox挂载共享文件夹
版权 挂载共享文件夹很简单,有2种方法,1是自动挂载,2是手动挂载. 一.自动挂载步骤: 1,把想共享的文件夹设置为共享. 2,在virtualbox界面对虚拟机设置共享文件夹,如下图.