Ruby中的序数

时间:2015-12-24 16:14:06

标签: ruby

我的代码可以完美地处理除了11,12和13以外的所有内容或者以最后两位数结尾的数字。

from multiprocessing import Pool
import random
import socket
import sys

remoteServer = sys.argv[1]
remoteServerIP  = socket.gethostbyname(remoteServer)

ports = random.sample(range(1,1000), 999)

def testport(num):
    sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    sock.settimeout(.01)
    result = sock.connect_ex((remoteServerIP, port))
    sock.close()
    if result == 0:
        return num
    return None

p = Pool()
openports = p.map(testport, ports)
openports = [prt for prt in openports if prt is not None]

任何帮助将非常感谢!谢谢。

1 个答案:

答案 0 :(得分:2)

这是一个特例。检查1位数后缀之前,请检查2位数字后缀。像这样的东西

def ordinal(n)
  ending = case n % 100
           when 11, 12, 13 then 'th'
           else
             case n % 10
             when 1 then 'st'
             when 2 then 'nd'
             when 3 then 'rd'
             else 'th'
             end
           end

  "This is #{n}#{ending} item"
end

ordinal(1) # => "This is 1st item"
ordinal(10) # => "This is 10th item"
ordinal(12) # => "This is 12th item"
ordinal(15) # => "This is 15th item"
ordinal(112) # => "This is 112th item"
ordinal(123) # => "This is 123rd item"