[Haskell] CodeWars|Find the middle element
来源:互联网 发布:人工智能教学百度云 编辑:程序博客网 时间:2024/05/18 02:47
https://www.codewars.com/kata/545a4c5a61aa4c6916000755/train/haskell
题目翻译
本题中你需要写个函数,输入一个三元组,输出三个树中既不是最大的也不是最小的那个数字的下标。
样例
gimme (2, 3, 1) = 0gimme (5, 10, 14) = 1
题解
只使用maximum
和minimum
的方法,这是我写的一个比较奇怪的方法。。
gimme :: Ord a => (a, a, a) -> Intgimme (a, b, c) = 3 - snd (minimum l) - snd (maximum l) where l = zip [a, b, c] [0..]
当然正常人都会想到排序的。
gimme :: Ord a => (a, a, a) -> Intgimme (a, b, c) = snd . (!! 1) . sort $ zip [a,b,c] [0..]
还有更厉害的,不用库函数的方法:
gimme :: Ord a => (a, a, a) -> Intgimme (a, b, c) | b <= a && a <= c || c <= a && a <= b = 0 | otherwise = 1 + gimme (b, c, a)
阅读全文
0 0
- [Haskell] CodeWars|Find the middle element
- 1007. Find the middle element in a Linked List
- Middle-题目40:162. Find Peak Element
- [Haskell] CodeWars|Printer Errors
- [Haskell] CodeWars|Vowel Count
- [Haskell] CodeWars|Bouncing Balls
- [Haskell] CodeWars|Your order, please
- [Haskell] CodeWars|Who likes it?
- [Haskell] CodeWars|Sum of Digits
- [Haskell] CodeWars|Integers: Recreation One
- 菜圈的codewars(五),Find The Parity Outlier
- LeetCode find the peak element
- fwnx - find the middle node in the linked list ~~~
- Find the middle point of the linked list
- [Haskell] CodeWars|Growth of a Population
- [Haskell] CodeWars|Sum of odd numbers
- [Haskell] CodeWars|Build a pile of Cubes
- How to find middle element of LinkedList in Java in one pass
- 手把手教你写二叉查找树Binary Search Tree(1)
- iOS11人机交互指南-01 iPhone X概览
- 身份认证加salt
- Activity的四种启动模式讲解
- python--leetcode 535. Encode and Decode TinyURL
- [Haskell] CodeWars|Find the middle element
- [leetcode] House Robber [i ii iii]
- C# winform之属性 bindingNavigator
- bzoj1345(区间合并式的贪心)
- windows下nginx的安装及使用方法入门
- Codeforces Round #302 (Div. 2) D
- 669. Trim a Binary Search Tree Difficulty : Easy
- Linux创建ftp并设置权限以及忘记ftp帐号(密码)修改
- linux下静态库