好想中乐透啊,Ruby 30 天刷题修行篇第十四话

嗨我是A Fei,连续好几天都十一点多回家,真的是累翻,先来看看今天的题目:


Time to win the lottery!

Given a lottery ticket (ticket), represented by an array of 2-value arrays, you must find out if you've won the jackpot.

Example ticket:

[ [ 'ABC', 65 ], [ 'HGR', 74 ], [ 'BYHT', 74 ] ]
To do this, you must first count the 'mini-wins' on your ticket. Each subarray has both a string and a number within it. If the character code of any of the characters in the string matches the number, you get a mini win. Note you can only have one mini win per sub array.

Once you have counted all of your mini wins, compare that number to the other input provided (win). If your total is more than or equal to (win), return 'Winner!'. Else return 'Loser!'.

All inputs will be in the correct format. Strings on tickets are not always the same length.


一个字符串和一个数字
题目出了个乐透的小游戏,它提供一组阵列,里面包了好几张「乐透票」,是由一个字串和一个数字组合而成,如果字串中有任何字元的编码与数字匹配,将获得小奖。注意,每子阵列只能获得一次小奖。计算完赢得小奖的次数後,将该数字与另一组数字「win」比较,如果您的总数大於或等於「win」,则回传 Winner,反之回传 Loser。

def bingo(ticket,win)
  mini_win = 0
  for subarray in ticket
    if subarray[0].chars.map { |n| n.bytes }.flatten.include?(subarray[1])
    mini_win += 1
    end  
  end
  mini_win >= win ? "Winner!" : "Loser!" 
end

对比最佳解答:

def bingo(ticket, win)
  ticket.count { |string, code| string.include?(code.chr) } >= win ? 'Winner!' : 'Loser!'
end

<<:  【第二十九天 - 系统分析 题目分析】

>>:  Day17 - 语音辨识神级工具-Kaldi part2

Day-28 手把手的手写辨识模型 0x3:CNN is the end?模型大哉问

快到结尾了,再让笔者水一篇 XDD,今天我们来聊聊 CNN 会不会是深度学习领域的最後呢? CNN...

Day 12. Hashicorp Vault: HA with Consul

Hashicorp Vault: HA with Consul 在建置Vault时,因为以有使用Co...

Android:在Fragment禁用返回键最简易的方法

大部分文章介绍的禁用返回键方法都只适用於Activity: public boolean onKey...

从 IT 技术面细说 Search Console 的 27 组数字 KPI (14) :检索统计

在 KPI 的层级中,提到在分成几种 Level 之前,有列出三组最重要的 Search Conso...

网路的小技巧-2

//兴趣记录一下~希望退休以後可以回味,各位别嫌弃,感谢各位!! //// //VLAN重要性,实作...