题目:

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. 创建好maven项目以后发现无法创建scala文件

    今天创建了一个maven项目  然后准备创建scala文件的时候发现没有Scala 然后只好上网上找方法了 下面是一种解决方法 1.点击file,选择settings 进去之后,选择build  进去 ...

  2. 左叶子之和(sum-of-left-leaves)

    LeetCode题目--左叶子之和(sum-of-left-leaves) 计算给定二叉树的所有左叶子之和. 示例: 3 / \ 9 20 / \ 15 7 在这个二叉树中,有两个左叶子,分别是 9 ...

  3. day67:Vue:es6基本语法&vue.js基本使用&vue指令系统

    目录 Vue前戏:es6的基本语法 1.es6中的let特点 1.1.局部作用域 1.2.不存在变量提升 1.3.不能重复声明 1.4.let声明的全局变量不从属于window对象,var声明的全局变 ...

  4. boost之multiprecision

    multiprecision boost中提供的高精度库,支持高精度整型,浮点型等.并且提供统一的接口模板,只需要指定对应的后端类型即可实现对应类型的高精度计算: boost::multiprecis ...

  5. Jenkins集成appium自动化测试(Windows篇)

    一,引入问题 自动化测试脚本绝大部分用于回归测试,这就需要制定执行策略,如每天.代码更新后.项目上线前定时执行,才能达到最好的效果,这时就需要进行Jenkins集成. 不像web UI自动化测试可以使 ...

  6. kafka-消费者测试

    1. 在窗口1创建一个producer,topic为test,broker-list为zookeeper集群ip+端口   /usr/local/kafka/bin/kafka-console-pro ...

  7. JS获取DropDownList选择项的值

    var dropDownList= document.getElementById("<%=DropDownListID.ClientID %>");//获取DropD ...

  8. react基础准备知识

    1.模块化:将重复的(可复用的)代码抽离为单个模块 2.组件化:将重复的 (可复用的) 的前端页面元素抽离单个组件 * Vue中实现组件化:1.Vue.component() 2.Vue.extend ...

  9. 详解GaussDB(DWS) explain分布式执行计划

    摘要:本文主要介绍如何详细解读GaussDB(DWS)产生的分布式执行计划,从计划中发现性能调优点. 前言 执行计划(又称解释计划)是数据库执行SQL语句的具体步骤,例如通过索引还是全表扫描访问表中的 ...

  10. Linux文件的查找之find命令处理动作

    查找到文件之后的处理动作 例如:找出来系统中比较大超过10G的并且存放时间超过一年的log文件并删除 find / -name ".log" -size +10G -mtime + ...