根据某些条件匹配每个值的区域

  • 本文关键字:区域 条件 ruby compare match
  • 更新时间 :
  • 英文 :


我有这个输入表,显示与每个国家/地区代码相关的国家/地区代码和区域。

<!DOCTYPE html>
<html>
<table class=MsoNormalTable border=0 cellspacing=0 cellpadding=0 width=170 style='width:102.0pt;margin-left:-.05pt;border-collapse:collapse'>
  <tr style='height:.2in'>
    <td width=170 nowrap colspan=2 valign=bottom style='width:102.0pt;border:
  solid windowtext 1.0pt;background:#BF8F00;padding:0in 5.4pt 0in 5.4pt;
  height:.2in'>
      <p class=MsoNormal align=center style='margin-bottom:0in;margin-bottom:.0001pt;
  text-align:center;line-height:normal'><b><span style='color:black'>ZONES
  TABLE</span></b></p>
    </td>
  </tr>
  <tr style='height:.2in'>
    <td width=122 style='width:73.0pt;border:solid windowtext 1.0pt;border-top:
  none;background:#305496;padding:0in 5.4pt 0in 5.4pt;height:.2in'>
      <p class=MsoNormal align=center style='margin-bottom:0in;margin-bottom:.0001pt;
  text-align:center;line-height:normal'><b><span style='font-size:8.0pt;
  font-family:"Verdana",sans-serif;color:white'>COUNTRY CODE</span></b></p>
    </td>
    <td width=48 style='width:29.0pt;border-top:none;border-left:none;border-bottom:
  solid windowtext 1.0pt;border-right:solid windowtext 1.0pt;background:#305496;
  padding:0in 5.4pt 0in 5.4pt;height:.2in'>
      <p class=MsoNormal align=center style='margin-bottom:0in;margin-bottom:.0001pt;
  text-align:center;line-height:normal'><b><span style='font-size:8.0pt;
  font-family:"Verdana",sans-serif;color:white'>ZONE</span></b></p>
    </td>
  </tr>
  <tr style='height:.2in'>
    <td width=122 style='width:73.0pt;border:solid windowtext 1.0pt;border-top:
  none;background:#FAFAFA;padding:0in 5.4pt 0in 5.4pt;height:.2in'>
      <p class=MsoNormal align=center style='margin-bottom:0in;margin-bottom:.0001pt;
  text-align:center;line-height:normal'><span style='font-size:8.0pt;
  font-family:"Verdana",sans-serif;color:black'>237</span></p>
    </td>
    <td width=48 style='width:29.0pt;border-top:none;border-left:none;border-bottom:
  solid windowtext 1.0pt;border-right:solid windowtext 1.0pt;background:#FAFAFA;
  padding:0in 5.4pt 0in 5.4pt;height:.2in'>
      <p class=MsoNormal align=center style='margin-bottom:0in;margin-bottom:.0001pt;
  text-align:center;line-height:normal'><span style='font-size:8.0pt;
  font-family:"Verdana",sans-serif;color:black'>CMR</span></p>
    </td>
  </tr>
  <tr style='height:.2in'>
    <td width=122 style='width:73.0pt;border:solid windowtext 1.0pt;border-top:
  none;background:#FAFAFA;padding:0in 5.4pt 0in 5.4pt;height:.2in'>
      <p class=MsoNormal align=center style='margin-bottom:0in;margin-bottom:.0001pt;
  text-align:center;line-height:normal'><span style='font-size:8.0pt;
  font-family:"Verdana",sans-serif;color:black'>1721</span></p>
    </td>
    <td width=48 style='width:29.0pt;border-top:none;border-left:none;border-bottom:
  solid windowtext 1.0pt;border-right:solid windowtext 1.0pt;background:#FAFAFA;
  padding:0in 5.4pt 0in 5.4pt;height:.2in'>
      <p class=MsoNormal align=center style='margin-bottom:0in;margin-bottom:.0001pt;
  text-align:center;line-height:normal'><span style='font-size:8.0pt;
  font-family:"Verdana",sans-serif;color:black'>SXM2</span></p>
    </td>
  </tr>
  <tr style='height:.2in'>
    <td width=122 style='width:73.0pt;border:solid windowtext 1.0pt;border-top:
  none;background:#FAFAFA;padding:0in 5.4pt 0in 5.4pt;height:.2in'>
      <p class=MsoNormal align=center style='margin-bottom:0in;margin-bottom:.0001pt;
  text-align:center;line-height:normal'><span style='font-size:8.0pt;
  font-family:"Verdana",sans-serif;color:black'>124622</span></p>
    </td>
    <td width=48 style='width:29.0pt;border-top:none;border-left:none;border-bottom:
  solid windowtext 1.0pt;border-right:solid windowtext 1.0pt;background:#FAFAFA;
  padding:0in 5.4pt 0in 5.4pt;height:.2in'>
      <p class=MsoNormal align=center style='margin-bottom:0in;margin-bottom:.0001pt;
  text-align:center;line-height:normal'><span style='font-size:8.0pt;
  font-family:"Verdana",sans-serif;color:black'>BRB1</span></p>
    </td>
  </tr>
  <tr style='height:.2in'>
    <td width=122 style='width:73.0pt;border:solid windowtext 1.0pt;border-top:
  none;background:#FAFAFA;padding:0in 5.4pt 0in 5.4pt;height:.2in'>
      <p class=MsoNormal align=center style='margin-bottom:0in;margin-bottom:.0001pt;
  text-align:center;line-height:normal'><span style='font-size:8.0pt;
  font-family:"Verdana",sans-serif;color:black'>334</span></p>
    </td>
    <td width=48 style='width:29.0pt;border-top:none;border-left:none;border-bottom:
  solid windowtext 1.0pt;border-right:solid windowtext 1.0pt;background:#FAFAFA;
  padding:0in 5.4pt 0in 5.4pt;height:.2in'>
      <p class=MsoNormal align=center style='margin-bottom:0in;margin-bottom:.0001pt;
  text-align:center;line-height:normal'><span style='font-size:8.0pt;
  font-family:"Verdana",sans-serif;color:black'>FRA3</span></p>
    </td>
  </tr>
  <tr style='height:.2in'>
    <td width=122 style='width:73.0pt;border:solid windowtext 1.0pt;border-top:
  none;background:#FAFAFA;padding:0in 5.4pt 0in 5.4pt;height:.2in'>
      <p class=MsoNormal align=center style='margin-bottom:0in;margin-bottom:.0001pt;
  text-align:center;line-height:normal'><span style='font-size:8.0pt;
  font-family:"Verdana",sans-serif;color:black'>124624</span></p>
    </td>
    <td width=48 style='width:29.0pt;border-top:none;border-left:none;border-bottom:
  solid windowtext 1.0pt;border-right:solid windowtext 1.0pt;background:#FAFAFA;
  padding:0in 5.4pt 0in 5.4pt;height:.2in'>
      <p class=MsoNormal align=center style='margin-bottom:0in;margin-bottom:.0001pt;
  text-align:center;line-height:normal'><span style='font-size:8.0pt;
  font-family:"Verdana",sans-serif;color:black'>BRB3</span></p>
    </td>
  </tr>
  <tr style='height:.2in'>
    <td width=122 style='width:73.0pt;border:solid windowtext 1.0pt;border-top:
  none;background:#FAFAFA;padding:0in 5.4pt 0in 5.4pt;height:.2in'>
      <p class=MsoNormal align=center style='margin-bottom:0in;margin-bottom:.0001pt;
  text-align:center;line-height:normal'><span style='font-size:8.0pt;
  font-family:"Verdana",sans-serif;color:black'>124</span></p>
    </td>
    <td width=48 style='width:29.0pt;border-top:none;border-left:none;border-bottom:
  solid windowtext 1.0pt;border-right:solid windowtext 1.0pt;background:#FAFAFA;
  padding:0in 5.4pt 0in 5.4pt;height:.2in'>
      <p class=MsoNormal align=center style='margin-bottom:0in;margin-bottom:.0001pt;
  text-align:center;line-height:normal'><span style='font-size:8.0pt;
  font-family:"Verdana",sans-serif;color:black'>BRB45</span></p>
    </td>
  </tr>
  <tr style='height:.2in'>
    <td width=122 style='width:73.0pt;border:solid windowtext 1.0pt;border-top:
  none;background:#FAFAFA;padding:0in 5.4pt 0in 5.4pt;height:.2in'>
      <p class=MsoNormal align=center style='margin-bottom:0in;margin-bottom:.0001pt;
  text-align:center;line-height:normal'><span style='font-size:8.0pt;
  font-family:"Verdana",sans-serif;color:black'>1246</span></p>
    </td>
    <td width=48 style='width:29.0pt;border-top:none;border-left:none;border-bottom:
  solid windowtext 1.0pt;border-right:solid windowtext 1.0pt;background:#FAFAFA;
  padding:0in 5.4pt 0in 5.4pt;height:.2in'>
      <p class=MsoNormal align=center style='margin-bottom:0in;margin-bottom:.0001pt;
  text-align:center;line-height:normal'><span style='font-size:8.0pt;
  font-family:"Verdana",sans-serif;color:black'>BRB7</span></p>
    </td>
  </tr>
  <tr style='height:.2in'>
    <td width=122 style='width:73.0pt;border:solid windowtext 1.0pt;border-top:
  none;background:#FAFAFA;padding:0in 5.4pt 0in 5.4pt;height:.2in'>
      <p class=MsoNormal align=center style='margin-bottom:0in;margin-bottom:.0001pt;
  text-align:center;line-height:normal'><span style='font-size:8.0pt;
  font-family:"Verdana",sans-serif;color:black'>1876</span></p>
    </td>
    <td width=48 style='width:29.0pt;border-top:none;border-left:none;border-bottom:
  solid windowtext 1.0pt;border-right:solid windowtext 1.0pt;background:#FAFAFA;
  padding:0in 5.4pt 0in 5.4pt;height:.2in'>
      <p class=MsoNormal align=center style='margin-bottom:0in;margin-bottom:.0001pt;
  text-align:center;line-height:normal'><span style='font-size:8.0pt;
  font-family:"Verdana",sans-serif;color:black'>JAM</span></p>
    </td>
  </tr>
  <tr style='height:.2in'>
    <td width=122 style='width:73.0pt;border:solid windowtext 1.0pt;border-top:
  none;background:#FAFAFA;padding:0in 5.4pt 0in 5.4pt;height:.2in'>
      <p class=MsoNormal align=center style='margin-bottom:0in;margin-bottom:.0001pt;
  text-align:center;line-height:normal'><span style='font-size:8.0pt;
  font-family:"Verdana",sans-serif;color:black'>358</span></p>
    </td>
    <td width=48 style='width:29.0pt;border-top:none;border-left:none;border-bottom:
  solid windowtext 1.0pt;border-right:solid windowtext 1.0pt;background:#FAFAFA;
  padding:0in 5.4pt 0in 5.4pt;height:.2in'>
      <p class=MsoNormal align=center style='margin-bottom:0in;margin-bottom:.0001pt;
  text-align:center;line-height:normal'><span style='font-size:8.0pt;
  font-family:"Verdana",sans-serif;color:black'>FIN</span></p>
    </td>
  </tr>
  <tr style='height:.2in'>
    <td width=122 style='width:73.0pt;border:solid windowtext 1.0pt;border-top:
  none;background:#FAFAFA;padding:0in 5.4pt 0in 5.4pt;height:.2in'>
      <p class=MsoNormal align=center style='margin-bottom:0in;margin-bottom:.0001pt;
  text-align:center;line-height:normal'><span style='font-size:8.0pt;
  font-family:"Verdana",sans-serif;color:black'>33751</span></p>
    </td>
    <td width=48 style='width:29.0pt;border-top:none;border-left:none;border-bottom:
  solid windowtext 1.0pt;border-right:solid windowtext 1.0pt;background:#FAFAFA;
  padding:0in 5.4pt 0in 5.4pt;height:.2in'>
      <p class=MsoNormal align=center style='margin-bottom:0in;margin-bottom:.0001pt;
  text-align:center;line-height:normal'><span style='font-size:8.0pt;
  font-family:"Verdana",sans-serif;color:black'>FRA1</span></p>
    </td>
  </tr>
  <tr style='height:.2in'>
    <td width=122 style='width:73.0pt;border:solid windowtext 1.0pt;border-top:
  none;background:#FAFAFA;padding:0in 5.4pt 0in 5.4pt;height:.2in'>
      <p class=MsoNormal align=center style='margin-bottom:0in;margin-bottom:.0001pt;
  text-align:center;line-height:normal'><span style='font-size:8.0pt;
  font-family:"Verdana",sans-serif;color:black'>33679</span></p>
    </td>
    <td width=48 style='width:29.0pt;border-top:none;border-left:none;border-bottom:
  solid windowtext 1.0pt;border-right:solid windowtext 1.0pt;background:#FAFAFA;
  padding:0in 5.4pt 0in 5.4pt;height:.2in'>
      <p class=MsoNormal align=center style='margin-bottom:0in;margin-bottom:.0001pt;
  text-align:center;line-height:normal'><span style='font-size:8.0pt;
  font-family:"Verdana",sans-serif;color:black'>FRA2</span></p>
    </td>
  </tr>
  <tr style='height:.2in'>
    <td width=122 style='width:73.0pt;border:solid windowtext 1.0pt;border-top:
  none;background:#FAFAFA;padding:0in 5.4pt 0in 5.4pt;height:.2in'>
      <p class=MsoNormal align=center style='margin-bottom:0in;margin-bottom:.0001pt;
  text-align:center;line-height:normal'><span style='font-size:8.0pt;
  font-family:"Verdana",sans-serif;color:black'>599</span></p>
    </td>
    <td width=48 style='width:29.0pt;border-top:none;border-left:none;border-bottom:
  solid windowtext 1.0pt;border-right:solid windowtext 1.0pt;background:#FAFAFA;
  padding:0in 5.4pt 0in 5.4pt;height:.2in'>
      <p class=MsoNormal align=center style='margin-bottom:0in;margin-bottom:.0001pt;
  text-align:center;line-height:normal'><span style='font-size:8.0pt;
  font-family:"Verdana",sans-serif;color:black'>ANT</span></p>
    </td>
  </tr>
  <tr style='height:.2in'>
    <td width=122 style='width:73.0pt;border:solid windowtext 1.0pt;border-top:
  none;background:#FAFAFA;padding:0in 5.4pt 0in 5.4pt;height:.2in'>
      <p class=MsoNormal align=center style='margin-bottom:0in;margin-bottom:.0001pt;
  text-align:center;line-height:normal'><span style='font-size:8.0pt;
  font-family:"Verdana",sans-serif;color:black'>61</span></p>
    </td>
    <td width=48 style='width:29.0pt;border-top:none;border-left:none;border-bottom:
  solid windowtext 1.0pt;border-right:solid windowtext 1.0pt;background:#FAFAFA;
  padding:0in 5.4pt 0in 5.4pt;height:.2in'>
      <p class=MsoNormal align=center style='margin-bottom:0in;margin-bottom:.0001pt;
  text-align:center;line-height:normal'><span style='font-size:8.0pt;
  font-family:"Verdana",sans-serif;color:black'>AUS</span></p>
    </td>
  </tr>
  <tr style='height:.2in'>
    <td width=122 style='width:73.0pt;border:solid windowtext 1.0pt;border-top:
  none;background:#FAFAFA;padding:0in 5.4pt 0in 5.4pt;height:.2in'>
      <p class=MsoNormal align=center style='margin-bottom:0in;margin-bottom:.0001pt;
  text-align:center;line-height:normal'><span style='font-size:8.0pt;
  font-family:"Verdana",sans-serif;color:black'>230</span></p>
    </td>
    <td width=48 style='width:29.0pt;border-top:none;border-left:none;border-bottom:
  solid windowtext 1.0pt;border-right:solid windowtext 1.0pt;background:#FAFAFA;
  padding:0in 5.4pt 0in 5.4pt;height:.2in'>
      <p class=MsoNormal align=center style='margin-bottom:0in;margin-bottom:.0001pt;
  text-align:center;line-height:normal'><span style='font-size:8.0pt;
  font-family:"Verdana",sans-serif;color:black'>MUS</span></p>
    </td>
  </tr>
  <tr style='height:.2in'>
    <td width=122 style='width:73.0pt;border:solid windowtext 1.0pt;border-top:
  none;background:#FAFAFA;padding:0in 5.4pt 0in 5.4pt;height:.2in'>
      <p class=MsoNormal align=center style='margin-bottom:0in;margin-bottom:.0001pt;
  text-align:center;line-height:normal'><span style='font-size:8.0pt;
  font-family:"Verdana",sans-serif;color:black'>31</span></p>
    </td>
    <td width=48 style='width:29.0pt;border-top:none;border-left:none;border-bottom:
  solid windowtext 1.0pt;border-right:solid windowtext 1.0pt;background:#FAFAFA;
  padding:0in 5.4pt 0in 5.4pt;height:.2in'>
      <p class=MsoNormal align=center style='margin-bottom:0in;margin-bottom:.0001pt;
  text-align:center;line-height:normal'><span style='font-size:8.0pt;
  font-family:"Verdana",sans-serif;color:black'>NLD</span></p>
    </td>
  </tr>
  <tr style='height:.2in'>
    <td width=122 style='width:73.0pt;border:solid windowtext 1.0pt;border-top:
  none;background:#FAFAFA;padding:0in 5.4pt 0in 5.4pt;height:.2in'>
      <p class=MsoNormal align=center style='margin-bottom:0in;margin-bottom:.0001pt;
  text-align:center;line-height:normal'><span style='font-size:8.0pt;
  font-family:"Verdana",sans-serif;color:black'>65</span></p>
    </td>
    <td width=48 style='width:29.0pt;border-top:none;border-left:none;border-bottom:
  solid windowtext 1.0pt;border-right:solid windowtext 1.0pt;background:#FAFAFA;
  padding:0in 5.4pt 0in 5.4pt;height:.2in'>
      <p class=MsoNormal align=center style='margin-bottom:0in;margin-bottom:.0001pt;
  text-align:center;line-height:normal'><span style='font-size:8.0pt;
  font-family:"Verdana",sans-serif;color:black'>SGP</span></p>
    </td>
  </tr>
  <tr style='height:.2in'>
    <td width=122 style='width:73.0pt;border:solid windowtext 1.0pt;border-top:
  none;background:#FAFAFA;padding:0in 5.4pt 0in 5.4pt;height:.2in'>
      <p class=MsoNormal align=center style='margin-bottom:0in;margin-bottom:.0001pt;
  text-align:center;line-height:normal'><span style='font-size:8.0pt;
  font-family:"Verdana",sans-serif;color:black'>1721</span></p>
    </td>
    <td width=48 style='width:29.0pt;border-top:none;border-left:none;border-bottom:
  solid windowtext 1.0pt;border-right:solid windowtext 1.0pt;background:#FAFAFA;
  padding:0in 5.4pt 0in 5.4pt;height:.2in'>
      <p class=MsoNormal align=center style='margin-bottom:0in;margin-bottom:.0001pt;
  text-align:center;line-height:normal'><span style='font-size:8.0pt;
  font-family:"Verdana",sans-serif;color:black'>SXM4</span></p>
    </td>
  </tr>
  <tr style='height:.2in'>
    <td width=122 style='width:73.0pt;border:solid windowtext 1.0pt;border-top:
  none;background:#FAFAFA;padding:0in 5.4pt 0in 5.4pt;height:.2in'>
      <p class=MsoNormal align=center style='margin-bottom:0in;margin-bottom:.0001pt;
  text-align:center;line-height:normal'><span style='font-size:8.0pt;
  font-family:"Verdana",sans-serif;color:black'>1</span></p>
    </td>
    <td width=48 style='width:29.0pt;border-top:none;border-left:none;border-bottom:
  solid windowtext 1.0pt;border-right:solid windowtext 1.0pt;background:#FAFAFA;
  padding:0in 5.4pt 0in 5.4pt;height:.2in'>
      <p class=MsoNormal align=center style='margin-bottom:0in;margin-bottom:.0001pt;
  text-align:center;line-height:normal'><span style='font-size:8.0pt;
  font-family:"Verdana",sans-serif;color:black'>USA</span></p>
    </td>
  </tr>
  <tr style='height:.2in'>
    <td width=122 style='width:73.0pt;border:solid windowtext 1.0pt;border-top:
  none;background:#FAFAFA;padding:0in 5.4pt 0in 5.4pt;height:.2in'>
      <p class=MsoNormal align=center style='margin-bottom:0in;margin-bottom:.0001pt;
  text-align:center;line-height:normal'><span style='font-size:8.0pt;
  font-family:"Verdana",sans-serif;color:black'>124623</span></p>
    </td>
    <td width=48 style='width:29.0pt;border-top:none;border-left:none;border-bottom:
  solid windowtext 1.0pt;border-right:solid windowtext 1.0pt;background:#FAFAFA;
  padding:0in 5.4pt 0in 5.4pt;height:.2in'>
      <p class=MsoNormal align=center style='margin-bottom:0in;margin-bottom:.0001pt;
  text-align:center;line-height:normal'><span style='font-size:8.0pt;
  font-family:"Verdana",sans-serif;color:black'>BRB2</span></p>
    </td>
  </tr>
</table>
</html>

我在下表的 NUMBERS 列中有代码,我想从上表中搜索相应的区域。在下表中,列 ZONE 将是我试图为 NUMBERS 列中的值获取的输出。

<style type="text/css">
	table.tableizer-table {
		font-size: 12px;
		border: 1px solid #CCC; 
		font-family: Arial, Helvetica, sans-serif;
	} 
	.tableizer-table td {
		padding: 4px;
		margin: 3px;
		border: 1px solid #CCC;
	}
	.tableizer-table th {
		background-color: #BF8F00; 
		color: #FFF;
		font-weight: bold;
	}
.tableizer-table tr:nth-child(even){background-color: #f2f2f2}     
  .tableizer-table tr:first-child td {
      background-color: #305496; color: #FFF;
    }
    tr td:nth-child(3) {
  background: #DFDFDF;
}
tr:nth-child(1) td:nth-child(3) {
  background: #5D5B56;
}
</style>
<table class="tableizer-table">
<thead><tr class="tableizer-firstrow"><th>NUMBERS to SEARCH</th><th>ZONE EXPECTED</th><th>&nbsp;</th></tr></thead><tbody>
 <tr><td>NUMBERS</td><td>ZONE</td><td>NOTES about logic to match the zone</td></tr>
 <tr><td>18763044</td><td>JAM</td><td>CC=1876 belongs to JAM</td></tr>
 <tr><td>187635</td><td>JAM</td><td>CC=1876 belongs to JAM</td></tr>
 <tr><td>23092</td><td>MUS</td><td>CC=230 belongs to JAM</td></tr>
 <tr><td>3162</td><td>NLD</td><td>CC=31 belongs to NLD</td></tr>
 <tr><td>38050</td><td>NOT FOUND</td><td>There is no CC that has as prefix 38,380, 3805 etc</td></tr>
 <tr><td>33</td><td>FRA3</td><td>There are 3 CC that begins with 33, but 334 selected because is the shortest and belongs to FRA3</td></tr>
 <tr><td>49185</td><td>NOT FOUND</td><td>There is no CC that begins with 49,491, etc</td></tr>
 <tr><td>51078</td><td>NOT FOUND</td><td>There is no CC that begins with 51,510, etc</td></tr>
 <tr><td>1246</td><td>BRB7</td><td>There are 4 CC that begins with 1246, but there is one exact match, CC=1246 that belongs to ZONE BRB7</td></tr>
 <tr><td>1721</td><td>SXM2</td><td>There are 2 CC that begins with 1721, but the ZONE to select is that that appears first. In this case SXM2</td></tr>
</tbody></table>

我已经将表值以数组形式放置,到目前为止,我有下面的代码来检测数组 cc 的值以数字值开头,但输出很远从我的预期输出中,因为我不确定该怎么做。也许有人可以帮助我。提前谢谢。

cc=["237","1721","124622","334","124624","124","1246","1876","358","33751","33679","599","61","230","31","65","1721","1","124623"]
zones=["CMR","SXM2","BRB1","FRA3","BRB3","BRB45","BRB7","JAM","FIN","FRA1","FRA2","ANT","AUS","MUS","NLD","SGP","SXM4","USA","BRB2"]
numbers=["18763044","187635","23092","3162","38050","33","49185","51078","1246","1721"]
numbers.map{|n| 
    cc.find {|i|  i.start_with?(n) }
}
=> [nil, nil, nil, nil, nil, "334", nil, nil, "124622", "1721"]

经过一些澄清后完全改变了答案,这就是我到目前为止所做的。优化还算过得去。


首先,从头开始计算匹配的元素数量的方法。它仅适用于数字字符串:
def count_equal_digits_at_beginning(a, b)
  return a.size if a == b
  rabdiff = (a.reverse.to_i - b.reverse.to_i).abs
  rabdiff.digits.count - rabdiff.to_s.reverse.to_i.digits.count
end


其次,将[cc, zones]对映射到哈希,以便快速轻松地访问。我颠倒了,因为要求是保持密钥的第一次出现:
mapping_hash = cc.reverse.zip(zones.reverse).to_h


最后编写一个看起来匹配的代码,过滤和排序结果并从mapping_hash中选取。(经过编辑以优先考虑完全匹配和升序(
res = numbers.map.with_object([]) do |n, res|
  exact = cc.find { |c| c == n }
  if exact
    found = exact
  else
    tmp = cc.map { |c| [c, count_equal_digits_at_beginning(c, n)] }
    possibles = tmp.find_all { |c, count| count == tmp.max_by(&:last).last && count > 1 }
    good = possibles.sort_by(&:first).reverse.first
    found = good.first if good
  end
  res << [ n, mapping_hash[found] || "not found" ]
end.to_h

所以,最终的结果是

res #=> {"18763044"=>"JAM", "187635"=>"JAM", "23092"=>"MUS", "3162"=>"NLD", "38050"=>"not found", "33"=>"FRA1", "49185"=>"not found", "51078"=>"not found", "1246"=>"BRB7", "1721"=>"SXM2"}

相关内容

  • 没有找到相关文章

最新更新