原题:

Description:

Bob is preparing to pass IQ test. The most frequent task in this test is to find out which one of the given numbers differs from the others. Bob observed that one number usually differs from the others in evenness. Help Bob — to check his answers, he needs a program that among the given numbers finds one that is different in evenness, and return a position of this number.

找出字符串数字中不同的数字的索引

IQ.Test("2 4 7 8 10") => 3 // Third number is odd, while the rest of the numbers are even IQ.Test("1 2 1 1") => 2 // Second number is even, while the rest of the numbers are odd

public static int Test(string numbers)
{
//var numList = numbers.Split(' ').ToList();
//var evenNum = numList.Where(o => int.Parse(o) % 2 == 0).ToList();
//if (evenNum.Count == 1)
//{
// return numList.FindIndex(o => o == evenNum.First()) + 1;
//}
//else
//{
// var oddNum = numList.Where(o => !evenNum.Contains(o)).ToList();
// return numList.FindIndex(o => o == oddNum.First()) + 1;
//}
var numList = numbers.Split(' ').Select((n, i) => new { Value =int.Parse(n), Index = i });
var evenNum = numList.Where(o => o.Value % 2 == 0);
var oddNum = numList.Where(o => o.Value % 2 == 1);
return evenNum.Count() == 1 ? evenNum.First().Index + 1 : oddNum.First().Index + 1;
}

注释部分自己写的,其余部分个人感觉最优解  技术点:https://msdn.microsoft.com/zh-cn/library/bb548891.aspx      Enumerable.Select<TSource,TResult>

codewars 题目笔记的更多相关文章

  1. Codewars练习笔记·1 - 6.23

    Codewars地址:https://www.codewars.com/ 笔记资料来源:JavaScript高级程序设计. 欢迎和大家一起来讨论~   基础练习(1):   我的解答为: class ...

  2. CodeWars题目筛选

    http://www.codewars.com/kata/search/csharp?q=&r%5B%5D=-8&xids=completed&beta=false 语言选择C ...

  3. css 题目笔记(本文收集了一些个人觉得比较有意思的css题目,欢迎大家给出自己的解答)

    1.本文收集了一些个人觉得比较有意思的css题目,欢迎大家给出自己的解答 P标签的最大宽度不可以大于H2标签文字宽度的10% 这里应该是P标签的最大宽度由前面的匿名内联元素宽度(就是大字号文字宽度)决 ...

  4. Objextive-C几道小题目笔记

    //掷骰子题,掷骰子100次,输出每个号出现的次数 void one() { for (int i=1; i<=100; i++) { int a = arc4random() % 6 +1; ...

  5. 算法笔记(c++)--桶排序题目

    算法笔记(c++)--桶排序 记得题目是排序,输入n个1-1000的数字然后去重然后排序. 桶排序没毛病 #include<iostream> using namespace std; i ...

  6. Codewars笔记

    说明:以下内容均来自codewars网站,列举的试题我都做过且通过,并以此记录来学习python.   1,需求:将大小写互相转换,非字母的字符保留 我的代码: def to_alternating_ ...

  7. codewars遇到的比较有意思的题目

    题目要求是编写一个函数用来检测一个字符串,字符串是一系列单词组成,每个单词间用空格隔开,不用考虑空字符串的情况,返回长度最小的那个单词的长度. 博主刚入门PHP,技术还很菜,没有想出来,看了其他人的解 ...

  8. ORACLE_笔记_练习题目

    一.plsql用法网址及时复习 extract()函数----用于截取年.月.日.时.分.秒 https://www.cnblogs.com/xqzt/p/4477239.html case when ...

  9. codewars贪吃蛇算法题目

    有这样一个题目: Given an n x n array, return the array elements arranged from outermost elements to the mid ...

随机推荐

  1. TZOJ 2519 Regetni(N个点求三角形面积为整数总数)

    描述 Background Hello Earthling. We're from the planet Regetni and need your help to make lots of mone ...

  2. linux下查看动态链接库so文件的依赖的相关组建

    我们很多c程序在windows下是以dll形式展现的,在linux则是以so 形式展现的. windows一般不会因为编译dll文件的编译器版本不同而出先dll文件不能执行. 但是linux下,不同版 ...

  3. mysql数据库分区和分表

    转载自 https://www.cnblogs.com/miketwais/articles/mysql_partition.html https://blog.csdn.net/vbirdbest/ ...

  4. Centos7下面配置客户端OpenVPN

    安装 openvpn yum install -y openvpn vim 配置默认的 daemon 文件 vim /usr/lib/systemd/system/openvpn@.service [ ...

  5. 关于Android UI 优化

    之前项目为了同时兼容tv和手机端的UI,使用了百分比布局来动态计算控件的宽高,这种适配方案只关心屏幕的宽高(分辨率),与屏幕的像素密度无关. 在新的项目里也使用了这种方案.但是由于项目的运行硬件计算能 ...

  6. 5个数组Array方法: indexOf、filter、forEach、map、reduce使用实例

    ES5中,一共有9个Array方法 Array.prototype.indexOf Array.prototype.lastIndexOf Array.prototype.every Array.pr ...

  7. vue自定义滚动条

    参照element-ui的el-scroll自己实现了一个自定义组件,代码如下: <template> <div class="c-scroll-box" ref ...

  8. 关于js中操作数组的一些方法

    网上找的通篇看了一遍讲的很透收藏了!  转自(https://www.cnblogs.com/blogs-8888/p/6518683.html) 1.锁定数组的长度(让数组的长度变成只读). 1 2 ...

  9. win10无法访问服务器上的共享文件夹怎么设置,提示:你不能访问此共享文件夹,因为你组织的安全策略阻止未经身份验证的来宾访问

    此问题需要修改Win10 网络策略 按window+R键输入gpedit.msc 来启动本地组策略编辑器. 依次找到“计算机配置-管理模板-网络-Lanman工作站”这个节点,在右侧内容区可以看到“启 ...

  10. virtualbox装个 ubuntu

    好久没弄 虚拟机了,感觉好不习惯 根据网上的步骤装了一ubuntu 启动报错 virtualbox intel_rapl: no valid rapl domains ...... 搜索到了一片中文博 ...