题目:

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(找出奇数)的更多相关文章

  1. 【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 ...

  2. 【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 ...

  3. 【Kata Daily 190908】How Much?

    原题: I always thought that my old friend John was rather richer than he looked, but I never knew exac ...

  4. Entity Framework 6 Recipes 2nd Edition(9-3)译->找出Web API中发生了什么变化

    9-3. 找出Web API中发生了什么变化 问题 想通过基于REST的Web API服务对数据库进行插入,删除和修改对象图,而不必为每个实体类编写单独的更新方法. 此外, 用EF6的Code Fri ...

  5. 使用T-SQL找出执行时间过长的作业

        有些时候,有些作业遇到问题执行时间过长,因此我写了一个脚本可以根据历史记录,找出执行时间过长的作业,在监控中就可以及时发现这些作业并尽早解决,代码如下:   SELECT sj.name , ...

  6. [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 ...

  7. [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 ...

  8. [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 ...

  9. 如何找出标有"App Store 精华","Essentials"的所有软件?

    如何找出标有"App Store 精华","Essentials"的所有软件? 中国区: +"App Store 精华" site:http ...

随机推荐

  1. Python导入模块的几种方法

    Python 模块 Python 模块(Module),是一个 Python 文件,以 .py 结尾,包含了 Python 对象定义和Python语句. 模块让你能够有逻辑地组织你的 Python 代 ...

  2. matlab中figure 创建图窗窗口

    来源:https://ww2.mathworks.cn/help/matlab/ref/figure.html?searchHighlight=figure&s_tid=doc_srchtit ...

  3. SpringBoot整合Shiro+MD5+Salt+Redis实现认证和动态权限管理(上)----筑基中期

    写在前面 通过前几篇文章的学习,我们从大体上了解了shiro关于认证和授权方面的应用.在接下来的文章当中,我将通过一个demo,带领大家搭建一个SpringBoot整合Shiro的一个项目开发脚手架, ...

  4. 【Rsync实战】Rsync 备份的配置与管理

    一.Rsync 基本概述 rsync 是一款开源的备份工具,可以在不同主机之间进行同步,可实现全量备份与增量备份,因此非常适合用于架构集中式备份或异地备份等应用. rsync 官方地址: rsync ...

  5. linux的bootmem内存管理

    内核刚开始启动的时候如果一步到位写一个很完善的内存管理系统是相当麻烦的.所以linux先建立了一个非常简单的临时内存管理系统bootmem,有了这个bootmem就可以做简单的内存分配/释放操作,在b ...

  6. 多测师讲解接口测试 _报错_高级讲师肖sir

  7. 互不侵犯(洛谷P1896)

    题目:在N*N的棋盘里面放k个国王,使他们互不攻击,共有多少种摆放方案.国王能攻击到它上下左右,以及左上左下右上右下八个方向上附近的各一个格子,共8个格子. 输入输出:输入N,K,输出有几种放置方法. ...

  8. redhat系统服务器重启后提示An error occurred during the file system check.

    问题描述 浪潮一台NF8480M3外观红灯报警,鉴于无法登陆带外,只能对服务器进行断电重启操作 问题现象 重启后进入开机过程并报错,报错如下内容及图片如下所示,正常来说进入此界面后直接输入root密码 ...

  9. linux创建www用户组和用户

    linux创建www用户组和用户 wdcp中的nginx服务启动需要依赖www用户,因此若没有此用户就可能会启动失败.创建这个用户的方法: [root@bogon local]# id www [ro ...

  10. cgdb安装

    cgdb官网:http://cgdb.github.io/ 一.cgdb安装 可使用wget命令下载,wget  http://cgdb.me/files/cgdb-0.7.0.tar.gz 之后解压 ...