题目:

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. 排序算法:归并排序(Merge Sort)

    归并排序 归并排序采用了分治策略(divide-and-conquer),就是将原问题分解为一些规模较小的相似子问题,然后递归解决这些子问题,最后合并其结果作为原问题的解. 归并排序将排序数组A[1. ...

  2. 092 01 Android 零基础入门 02 Java面向对象 02 Java封装 01 封装的实现 03 # 088 01 Android 零基础入门 02 Java面向对象 02 Java封装 02 static关键字 02 static关键字(中)

    092 01 Android 零基础入门 02 Java面向对象 02 Java封装 01 封装的实现 03 # 088 01 Android 零基础入门 02 Java面向对象 02 Java封装 ...

  3. MCU(Micro Control Unit)中文名称为微控制单元

    参考:http://www.elecfans.com/dianzichangshi/mcu.html 什么是mcu_mcu是什么意思 标签:MCU(471)单片机(3098)微控制器(503) MCU ...

  4. notepad快捷使用

    1.快捷键 参考:https://www.php.cn/tool/notepad/428638.html notepad++是经常使用的一款编辑器软件,在编辑特殊文本的时候(html,java...) ...

  5. .net网站自动化部署-致两年前的遗留的问题

    又到一年国庆,终于有了难得的几天空闲,计划陪陪媳妇娃子,再把最近阅读的几本相关书总结梳理下.当然,计划总是美好的,于时接到了一个老朋友电话.大意是他搞了一个.net小网站,部署了4个节点,每次更新程序 ...

  6. 几个常用markdown工具的主要优缺点

    几个常用markdown工具的主要优缺点 最近对几个热门的markdown工具做了一个对比表 表格 脚注 图片和图床 平台 移动端 实时预览 收费 操作难度 导出功能 mweb 非常棒 预览正常显示 ...

  7. SpringSecurity原理

    一.认证的两种方式的介绍 1. 基于Session的认证方式 在之前的单体架构时代,我们认证成功之后都会将信息存入到Session中,然后响应给客户端的是对应的Session中数据的key,客户端会将 ...

  8. 笔记本键盘按U键却变成了4

    解答 笔记本键盘U盘变成了4,是因为你开启了小键盘功能.出现该问题,只要关闭小键盘功能即可,操作如下: 按住键盘下方的Fn,同时按住键盘顶部的F键中标有Numlk的键. 电脑屏幕出现解锁标志,小键盘功 ...

  9. C# 中 System.Range 结构体

    翻译自 John Demetriou 2020年4月6日 的文章 <C# 8 Is Introducing Ranges> 我们之前讨论过的 C# 中的一个特性 System.Index ...

  10. BigInteger和BigDecimal

    BigInteger BigInteger的作用是对整数做计算,一般的使用String类型初始化BigInteger,它除了封装了基本的加减乘除运算外还提供了signum(),abs()等函数,使用方 ...