题目:

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. 浅谈Prufer序列

    \(\text{Prufer}\)序列,是树与序列的一种双射. 构建过程: 每次找到一个编号最小的叶子节点\(Leaf\),将它删掉,并将它所连接的点的度数\(-1\),且加入\(\text{Pruf ...

  2. shell-变量输入内置read命令详解

    1. shell变量的输入 shell变量除了可以直接赋值或脚本传参外,还可以使用read命令从标准输入得. [语法格式] read [参数] [变量名] [常用参数] -p:指定读取值时的提示符: ...

  3. ASP.Net Core3.1 生成二维码填坑

    ASP.Net Core3.1 使用QrCode生成二维码 部署到Linux报错 The type initializer for 'System.DrawingCore.GDIPlus' threw ...

  4. day25 Pyhton学习 MD5加密.日志

    一.MD5加密 MD5是一种不可逆的加密算法. 它是可靠的. 并且安全的. 在python中我们不需要手写这一套算法. 只需要引入一个叫hashlib的模块就能搞定MD5的加密工作 import ha ...

  5. js产生任意2个区间内的随机整数

    var code = myRound(30,100); function myRound(begin,end){ var num = Math.round(Math.random()*(end-beg ...

  6. elasticsearch-安装-centos7- es7.5 搭建

        centos6 搭建 参考 https://www.cnblogs.com/php-linux/p/8758788.html   搭建linux虚拟机  https://www.cnblogs ...

  7. STM32芯片型号的命名规则

    意法半导体已经推出STM32基本型系列.增强型系列.USB基本型系列.增强型系列:新系列产品沿用增强型系列的72MHz处理频率.内存包括64KB到256KB闪存和20KB到64KB嵌入式SRAM.新系 ...

  8. Unity实现代码控制音频播放

    前言 很久没说过Unity了,现在说一下Unity用代码控制音频播放 准备工作 1.需要播放的音频 2.给需要加声音的对象加Audio Source组件 3.新建Play脚本,并绑定需要播放声音的对象 ...

  9. Scrapy中get和extract_first的区别

    在scrapy中,从xpath中取得selector对象后,需要取出需要的数据. 使用get以及getall获取的是带标签的数据 比如 <p>这是一段文字</p> 如果用get ...

  10. js 日期格式、内容合法、比较大小、表单提交验证

    1 <!DOCTYPE html> 2 <html> 3 <head> 4 <meta charset="utf-8"/> 5 &l ...