【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 ...
随机推荐
- 080 01 Android 零基础入门 02 Java面向对象 01 Java面向对象基础 01 初识面向对象 05 单一职责原则
080 01 Android 零基础入门 02 Java面向对象 01 Java面向对象基础 01 初识面向对象 05 单一职责原则 本文知识点:单一职责原则 说明:因为时间紧张,本人写博客过程中只是 ...
- C/C++常用头文件
原文来源:https://blog.csdn.net/thisispan/article/details/7470335 无聊的时候可以多看看: C/C++头文件一览C#include <ass ...
- matlab中fspecial Create predefined 2-D filter以及中值滤波均值滤波以及高斯滤波
来源: 1.https://ww2.mathworks.cn/help/images/ref/fspecial.html?searchHighlight=fspecial&s_tid=doc_ ...
- matlab中ischar确定输入是否为字符数组
来源:https://ww2.mathworks.cn/help/matlab/ref/ischar.html?searchHighlight=ischar&s_tid=doc_srchtit ...
- 伺服电机的Arduino库函数
servo.attach(pin) //连接伺服电机的信号线于控制板的引脚,9或10号引脚servo.attach(pin, min, max) servo: a variable of type ...
- 【Excel技巧】用IF函数进行等级评定
如果下面给出一份"2月份语文成绩考核表",那么如何对成绩进行等级评定呢. 等级评定规则: 总分(100分) A级(91-100) B级(81-90) C级(71-80) D级(70 ...
- Dotnet Core使用特定的SDK&Runtime版本
Dotnet Core的SDK版本总在升级,怎么使用一个特定的版本呢? 假期过完了,心情还在.今天写个短的. 一.前言 写这个是因为昨天刷微软官方文档,发现global.json在 SDK 3.0 ...
- Redis 的完整安装过程
Windos 版本安装 Redis 官方并不支持 Window 版本,但是微软公司在 Github 上维护了一个 Windows 版本的 Redis 项目,供 Windows 用户下载使用. 下载地址 ...
- 利用babel工具将es6语法转换成es5,Object.assign方法报错
一.新建工程初始化项目 1.新建工程文件夹这里起名叫做es6,然后在里面创建两个文件夹分别为src .dist如下图:(src为待转换es6 js存放目录,dist为编译完成后的es5 js存放目录) ...
- python中的对文件的读写
简单的实例 open函数获取文件,w是写权限,可以对文件进行io操作 file=open('C:/Users/Administrator/Desktop/yes.txt','w') file.writ ...