鱼C论坛

 找回密码
 立即注册
查看: 3421|回复: 12

[技术交流] 鱼C论坛Python精英挑战赛(第三季02期)

[复制链接]
发表于 2017-9-11 11:56:35 | 显示全部楼层 |阅读模式
本帖最后由 jerryxjr1220 于 2017-9-17 23:34 编辑

第三届鱼C论坛精英挑战赛开赛咯!为了增加趣味性,本届比赛增加了“新玩法”-- “押宝玩法”,“竞猜玩法”和“擂主玩法”。

规则:

1. 押宝玩法:进入“押宝”竞猜帖,购买主题(5鱼币)参与“押宝”,最终“押宝”获胜者将平分奖池的奖金并额外获取10鱼币奖励。猜错者将不返还“押宝”的鱼币。若本届比赛无人“押宝”成功,奖金将累计到下次比赛。

2. 竞猜玩法:直接在比赛帖的下方进行投票,凡事“竞赛”获胜者,将奖励5鱼币。竞猜无门槛,人人都可以参与。竞猜以后,请在本帖留个言,方便领取奖励。

3. 擂主玩法:上一期挑战成功的鱼油成为挑战赛的擂主,擂主有优先权提议下一期的赛题,一届挑战赛共分5期,同一届中当擂主最长的鱼油有额外奖励。

本期擂主:@小锟

本期赛题:算法题


题目:无穷序列中的位置

在由“123456789101112131415...”开始的无穷序列中,“456”在该序列中的位置是3(第一位的序号为0)。
相同的,“789”在该序列中的位置就是6。
那么“454”在该序列中的位置呢?由于该序列是无穷序列,可以一直拓展下去,所以当序列拓展到...434445464748...时,就可以发现“454”在该序列的第79位。
同理,“91”在该序列的第8位;“9100”在该序列的第188位。
“123456798”在该序列的第1000000071位。
“123459876”在该序列的第1000027773位。

请设计一个函数findposition(n), 输出n在该无穷序列中的位置。
def findposition(n):
    "Your code here"
    return index_of_the_string_with_starting_of_n
备注:输出位数可能会超过10亿位,注意运算效率。

要求:

程序运算正确,执行效率最高的即为获胜者。

竞猜:
“910000000”在该序列的第几位?

比赛截止日期:9月16日24时,竞猜和押宝截止日期为9月15日

@小甲鱼 @冬雪雪冬 @~风介~ @SixPy

单选投票, 共有 8 人参与投票
您所在的用户组没有投票权限

本帖被以下淘专辑推荐:

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2017-9-11 11:59:51 | 显示全部楼层
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2017-9-11 17:20:06 | 显示全部楼层
本帖最后由 gunjang 于 2017-9-13 17:01 编辑

我能说答案是68888888(选项3)吗。。。555无法投票
该问题分成两个部分,找出数字x的位置和把序列分成连续的x,x+1,x+2...
第一个很简单,因为1-9共9个数字长度为1,10-99共90个数字长度为2。。。很有规律
第二个如果是普通的连续序列,或者只是长度为1的序列,还好判断,但是考虑到99100序列中截取的部分如9100就很难,故采用通配符?*代表1个和多个字符进行匹配。。。(正则表达式水平不够,大神应该可以直接用regex进行模式判断吧),容易出错,还缺少测试代码。。
凑合吧
  1. #http://bbs.fishc.com/thread-96256-1-1.html
  2. import math
  3. def get10nindex(n):
  4.         r = 0
  5.         for i in range(1, n+1):
  6.                 r += (10**i - 10**(i-1)) * i
  7.         return r

  8. def getXindex(x):
  9.         n = int(math.log10(x))
  10.         return get10nindex(n) + (x-10**n-1+1)*(n+1)

  11. #Wildcard: question mark(?): one character
  12. #star(*) any number of characters
  13. def match(source, startindex, x):
  14.         #return True or False and startindex
  15.         x = str(x)
  16.         for i in range(len(x)):
  17.                 if source[startindex+i] == '?':
  18.                         continue
  19.                 if source[startindex+i] == '*':
  20.                         return True, len(source)
  21.                 if source[startindex+i] != x[i]:
  22.                         return False, 0
  23.         return True, startindex+len(x)

  24. #9932 ==> 3199,3200 => 3199,3
  25. def trymatch(newseq, sublen, qmcnt):
  26.         #9932 => ..9932* => ..99  , 32*  => ??00, 32* #99+1=100[1:3]
  27.         a = '?'*qmcnt+str(int(newseq[qmcnt:sublen])+1)[qmcnt-sublen:]#[:sublen-qmcnt]
  28.         b = newseq[sublen:]
  29.         #exp: a,b=99,100,have difficent length
  30.         print('trymath', a,'vs',b)
  31.         r = ''
  32.         for i in range(sublen):
  33.                 if a[i] == b[i]:
  34.                         r += a[i]
  35.                 else:
  36.                         if a[i]=='?':
  37.                                 r += b[i]
  38.                         elif b[i] == '*':
  39.                                 return True, r+a[i:]
  40.                         else:
  41.                                 return False, ''
  42.         return True, r

  43. def getMinXinSequence(sequence):
  44.         sequence = str(sequence)
  45.         x = int(sequence)
  46.         offset = 0
  47.         if len(sequence) == 1:
  48.                 return x, offset
  49.         if len(sequence) == 2:
  50.                 #56 78 => 5 7
  51.                 if int(sequence[1]) == int(sequence[0])+1:
  52.                         return int(sequence[0]), 0
  53.                 #53 =>3|53|6 or 53|54
  54.                 t = (x%10)*10 + x//10
  55.                 if x < t:
  56.                         return x, 0
  57.                 else:
  58.                         return t, 1

  59.         #length >= 3
  60.         #wildchar
  61.         #. match one char/digit
  62.         #* match any chars/digits
  63.         for sublen in range(1, len(sequence)+1):
  64.                 for qmcnt in range(sublen):
  65.                         newseq = '?'*qmcnt + sequence + '*'
  66.                         if qmcnt == 0:
  67.                                 if sublen == len(sequence): #n=sequence
  68.                                         continue
  69.                                 n = int(sequence[:sublen])
  70.                                 index = sublen
  71.                                 while True:
  72.                                         n += 1
  73.                                         matched, index = match(newseq, index, n)
  74.                                         if not matched:
  75.                                                 break
  76.                                         else:
  77.                                                 if index >= len(sequence):
  78.                                                         return int(sequence[:sublen]), 0 # matched
  79.                         else:
  80.                                 if len(sequence) + qmcnt > 2*sublen:
  81.                                         n = int(sequence[sublen-qmcnt:sublen-qmcnt+sublen])
  82.                                         #check prev number
  83.                                         matched, index        = match(newseq, 0, n-1)
  84.                                         if not matched:
  85.                                                 continue
  86.                                         index = sublen-qmcnt+sublen
  87.                                         t = n
  88.                                         while True:
  89.                                                 t += 1
  90.                                                 matched, index        = match(sequence, index, t)
  91.                                                 if not matched:
  92.                                                         break
  93.                                                 else:
  94.                                                         if index >= len(sequence):
  95.                                                                 return n-1, qmcnt # matched
  96.                                 else:
  97.                                         #too complex ,such as 23|4523|46  =>2345, 2346
  98.                                         #1|2312|4 or 12|312|4 => 123,124 qmcnt=1,2, sublen=3
  99.                                         # 129,130(2913)
  100.                                         matched, secondnum = trymatch(newseq, sublen, qmcnt)
  101.                                         if matched:
  102.                                                 n = int(secondnum)-1
  103.                                                 if x < n:
  104.                                                         return x, 0
  105.                                                 else:
  106.                                                         return n, qmcnt
  107.         return x, offset


  108. def findposition(n):
  109.     minx, offset = getMinXinSequence(n)
  110.     return getXindex(minx)+offset

  111. # print(5, getMinXinSequence('5'))       
  112. # print(12345, getMinXinSequence('12345'))
  113. # print(122123124, getMinXinSequence('122123124'))
  114. # print(122123124126, getMinXinSequence('122123124126'))
  115. # print(54, getMinXinSequence('54'))        #45 , 1
  116. # print(56, getMinXinSequence('56'))        #5,incorrect
  117. # t1 = '122123124' #==> 122
  118. # print(t1[2:], getMinXinSequence(t1[2:])) #==> 122,2

  119. # print(312, getMinXinSequence('312')) #==> 123,124 => 123,2
  120. #print(9932, getMinXinSequence('9932')) #==> 3199,3200 => 3199,3 or 2|993,2|994
  121. #print(9931, getMinXinSequence('9931')) #==> 3199,3200 => 3199,3 or 1|993,1|994

  122. #print(45, getXindex(45)) #79
  123. #print(123456798, getXindex(123456798)) #1000000071
  124. #print(123459876, getXindex(123459876)) #1000027773

  125. #print(findposition(789)) #6
  126. #print(findposition(454)) #79
  127. #print(getXindex(10000000)-1) #
  128. print(findposition(9100)) #9100=>99,100=>99,1=>189
复制代码

评分

参与人数 1荣誉 +5 鱼币 +5 贡献 +2 收起 理由
jerryxjr1220 + 5 + 5 + 2 答题奖励

查看全部评分

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2017-9-12 11:33:19 | 显示全部楼层
本帖最后由 小锟 于 2017-9-16 16:15 编辑

9100这个地方特殊点太多,9991000用了re
798100101.这地方理不清 ,我只能用查找rfind找,有可能有bug的问题 ,具体已经注释


  1. import re

  2. def findposition(a):
  3.     a = str(a)
  4.     index = -1
  5.     #length = len(a)
  6.     if a not in '12345678910':
  7.         fina = 0
  8.         x = lambda i: i * 9 * (10 ** (i - 1))

  9.         if '91' in a:
  10.             #print(a)
  11.             patter = re.compile(r'(.*?)([9][9]*)([1][0]*)(.*)')
  12.             result = re.findall(patter, a)
  13.             #print(result)
  14.             result9 = result[0][1]
  15.             result0 = result[0][2]
  16.             result_9 = result[0][0]
  17.             result_0 = result[0][3]
  18.             maxnum = max(int(result9), int(result0))
  19.             if (not result_9) and (not result_0):
  20.                 #maxnum = max(int(result9), int(result0))
  21.              #   print(maxnum)
  22.                 a = str(maxnum)
  23.                 if '9' not in a:
  24.                     fina = len(result9)
  25.                 #index = a.find('910')
  26.                 #a,fina= a[index + 1:],len(a[: index + 1])
  27.                 #fina = len(a[: index + 1])
  28.             else:
  29.                 #可能有bug的地方
  30.                 txt = ''.join([str(i) for i in list(range(1,maxnum*10))])
  31.                 index = txt.rfind(a)





  32.         length = len(a)
  33.         s = 0
  34.         for i in range(1, length):
  35.             s += x(i)
  36.         s += (int(a) - 10 ** (length - 1)) * length
  37.         return s - fina if index == -1 else index
  38.     return '12345678910'.find(a[0])



  39. print(findposition(123456798))
  40. print(findposition(98991000))
  41. print(findposition(798991001011))
  42. print(findposition(698991001011))
  43. print(findposition(910000000))
  44. print(findposition(991))
复制代码

最后测试了下,发现连续数过不了测试 11121314


评分

参与人数 1荣誉 +5 鱼币 +5 贡献 +2 收起 理由
jerryxjr1220 + 5 + 5 + 2 答题奖励

查看全部评分

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2017-9-13 14:03:19 | 显示全部楼层
本帖最后由 小剑剑 于 2017-9-14 13:21 编辑
  1. '''
  2.     对于无穷序列12345678910111213....(Infi)
  3.     给出任意的字符串(s),返回它在无穷序列第一次出现的索引
  4.    
  5.     思路:
  6.         1 s由至少3个数的不同部分组成
  7.         从s的左边开始假设第一个完整的数字为x位
  8.         再遍历每一个可能的开始位置
  9.         返回这个数字,和偏移量
  10.         2 有两部分组成

  11. '''

  12. def func(s):
  13.     l=len(s)
  14.     '''
  15.         处理第一个字符为0的情况
  16.     '''
  17.     if s[0]!='0':#第一个不是零,则最多可以去int(s)
  18.         answer=(int(s),0)
  19.     else:#第一个是零,则最多可以去int('1'+s)
  20.         answer=(int('1'+s),-1)
  21.     nl=1
  22.     '''
  23.     下面这部分为第一中情况,当s第一位是完整数字的第一位时有可能会是两个部分组成但是没影响
  24.     '''
  25.     while nl<l:
  26.         for i in range(nl):
  27.             if i+nl<l:
  28.                 #数字首位不能为0
  29.                 iszero=False
  30.                 for j in range(i,l,nl):
  31.                     if s[j]=='0':
  32.                         iszero=True
  33.                         break
  34.                 #有数字首位为0跳过
  35.                 if iszero:
  36.                     continue
  37.                 f=int(s[i:i+nl])
  38.                 if i and str(f-1)[-1:-1*i:-1]!=s[i-1::-1]:
  39.                     continue
  40.                 nums=s[0:i+nl]
  41.                 t=f
  42.                 while len(nums)<l:
  43.                     t+=1
  44.                     nums+=str(t)
  45.                 if (nums[0:l]==s) and (f<answer[0]):
  46.                     answer=(f,i)
  47.         nl+=1
  48.     '''
  49.     下面是情况2
  50.     '''
  51.     # if(l&1) and (int(s[0:l//2])+1)==int(s[l//2:l]):#处理前后两个数的长度不一样的的情况 即9999 10000
  52.     if '1' in s and s[0]=='9':
  53.         temp=s.index('1')
  54.         subs=s[0:temp]
  55.         exnine=True
  56.         for i in '123456780':#前面只有9
  57.             if i in subs:
  58.                 exnine=False
  59.                 break
  60.         if  exnine:
  61.             for i in '123456789':#后面只有0或者没有
  62.                 if i in s[temp+1:]:
  63.                     exnine=False
  64.                     break
  65.         if exnine :
  66.             if (l-temp-1)<=temp:
  67.                 tempanswer=(int(s[0:temp])+1,temp)
  68.             else:
  69.                 tempanswer=(int(s[temp:]),temp)
  70.             if tempanswer[0]<answer[0]:
  71.                 answer=tempanswer
  72.             return answer
  73.     #排除9999100000的情况后,奇数偶数的nl最小值不一样
  74.     if(l&1):
  75.         nl=l//2+1
  76.     else:
  77.         nl=l//2
  78.     #nl一直取值到l
  79.     while nl<l:
  80.         #第一个数字向前偏移的范围为0~2nl-L
  81.         for i in range(2*nl-l+1):
  82.             if (s[nl-i]=='0'):
  83.                 continue
  84.             h=int(s[0:nl-i])#前面一部分
  85.             h+=1#加一之后这部分和下一个数字的对应位的数字一样
  86.             x=l-nl#x 为交叉位
  87.             if str(h)[0:x]==s[l-x:l]:#第一个数交叉部分在前面 第二个在后面 交叉部分相同则数字符合
  88.                 t=int(s[nl-i:l-x]+s[0:nl-i])+1#组合出数字
  89.                 if (t<answer[0]):
  90.                     answer=(t,nl-i)
  91.         nl+=1
  92.     if len(str(answer[0]))<l:
  93.         return answer
  94.     #数字长为l时
  95.     for i in range(1,l):
  96.         if s[i]=='0':
  97.             continue
  98.         if i<len(str(int(s[0:i])+1)):
  99.             tempanswer=(int(s[i:])*10**i,i)
  100.         else:
  101.             tempanswer=(int(s[i:]+s[0:i]),i)
  102.         if answer[0]>tempanswer[0]:
  103.             answer=tempanswer
  104.     return answer



  105. def func2(answer):
  106.     num=answer[0]
  107.     l=len(str(num))
  108.     count=0
  109.     for i in range(0,l-1):
  110.         count+=9*10**i*(i+1)
  111.     count+=(num-10**(l-1))*(l)
  112.     return count-answer[1]

  113. def findposition(n):
  114.     print(func2(func(str(n))))
复制代码

感觉这种思路太繁琐

点评

我很赞同!: 5.0
我很赞同!: 5
测试通过,time=0.001s,程序很高效!  发表于 2017-9-17 23:30

评分

参与人数 1荣誉 +5 鱼币 +5 贡献 +2 收起 理由
jerryxjr1220 + 5 + 5 + 2 答题奖励

查看全部评分

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2017-9-14 13:20:35 | 显示全部楼层
发现个bug,改了一下,辛苦大大再测一次了
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2017-9-14 14:23:24 From FishC Mobile | 显示全部楼层
小剑剑 发表于 2017-9-14 13:20
发现个bug,改了一下,辛苦大大再测一次了

截止日期前,你们可以随意改,之后统一测试。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2017-9-14 17:10:59 | 显示全部楼层
本帖最后由 gunjang 于 2017-9-14 21:18 编辑

重新修改下,原来有不少bug,修复了同一个序列有多个解和进位的问题
  1. #http://bbs.fishc.com/thread-96256-1-1.html
  2. import math
  3. def get10nindex(n):
  4.         r = 0
  5.         for i in range(1, n+1):
  6.                 r += (10**i - 10**(i-1)) * i
  7.         return r

  8. def getXindex(x):
  9.         n = int(math.log10(x))
  10.         return get10nindex(n) + (x-10**n-1+1)*(n+1)

  11. #Wildcard: question mark(?): one character
  12. #star(*) any number of characters
  13. def match(source, index, x):
  14.         #return True or False and next index
  15.         x = str(x)
  16.         for i in range(len(x)):
  17.                 if source[index+i] == '?':
  18.                         continue
  19.                 if source[index+i] == '*':
  20.                         return True, len(source)
  21.                 if source[index+i] != x[i]:
  22.                         return False, 0
  23.         return True, index+len(x)

  24. def matchContinueN(source, index, x):
  25.         while True:
  26.                 matched, index = match(source, index, x)
  27.                 if not matched:
  28.                         return False
  29.                 else:
  30.                         if index >= len(source)-1: #matched
  31.                                 return True
  32.                 x += 1

  33. #9932 ==> 3199,3200 => 3199,2
  34. def trymatch(newseq, sublen, qmcnt):
  35.         #9932 => ..9932* => ..99  , 32*  => ??00, 32* #99+1=100[1:3]
  36.         a = '?'*qmcnt+str(int(newseq[qmcnt:sublen])+1)[qmcnt-sublen:]#[:sublen-qmcnt]
  37.         b = newseq[sublen:]
  38.         #exp: a,b=99,100,have difficent length
  39.         r = ''
  40.         for i in range(sublen):
  41.                 if a[i] == b[i]:
  42.                         r += a[i]
  43.                 else:
  44.                         if a[i]=='?':
  45.                                 r += b[i]
  46.                         elif b[i] == '*':
  47.                                 return True, r+a[i:]
  48.                         else:
  49.                                 return False, ''
  50.         return True, r

  51. def getMinXinSequence(sequence): #return minx and offset(sometimes x is incomplete)
  52.         sequence = str(sequence)
  53.         x = int(sequence)
  54.         offset = 0
  55.         if len(sequence) == 1:
  56.                 return x, offset

  57.         #length >= 2
  58.         founded = False       
  59.         for sublen in range(1, len(sequence)+1):
  60.                 #2312 have five continue Arrays
  61.                 #123,124 2231,2232  1223,1224  3122,3123  2312
  62.                 # ^^ ^^   ^^^ ^       ^^ ^^       ^ ^^^   ^^^^
  63.                 #123 is minimum
  64.                 for qmcnt in range(sublen): #qustion mark count
  65.                         #wildchar
  66.                         #? match one char/digit
  67.                         #* match any chars/digits
  68.                         newseq = '?'*qmcnt + sequence + '*'
  69.                         if qmcnt == 0:
  70.                                 if sublen == len(sequence): #n=sequence
  71.                                         minx = x
  72.                                         offset = 0
  73.                                         founded = True
  74.                                         continue

  75.                                 n = int(sequence[:sublen])
  76.                                 if matchContinueN(newseq, sublen, n+1):
  77.                                         if not founded:
  78.                                                 minx = n
  79.                                         else:
  80.                                                 minx = min(n, minx)
  81.                                         offset = 0                                                               
  82.                                         founded = True

  83.                         else:
  84.                                 if len(sequence) + qmcnt > 2*sublen:
  85.                                         #second number
  86.                                         n = int(sequence[sublen-qmcnt: sublen-qmcnt+sublen])
  87.                                         if matchContinueN(newseq, 0, n-1):
  88.                                                 if not founded:
  89.                                                         minx = n-1
  90.                                                 else:
  91.                                                         minx = min(n-1, minx)
  92.                                                 offset = qmcnt                                                               
  93.                                                 founded = True

  94.                                 else:
  95.                                         #too complex ,such as 23|4523|46  =>2345, 2346
  96.                                         #1|2312|4 or 12|312|4 => 123,124 qmcnt=1,2, sublen=3
  97.                                         # 129,130(2913)
  98.                                         #9100 => 99,100=>offset =1 but return 2
  99.                                         matched, secondnum = trymatch(newseq, sublen, qmcnt)
  100.                                         if matched:
  101.                                                 n = int(secondnum)-1
  102.                                                 if not founded:
  103.                                                         minx = n
  104.                                                 else:
  105.                                                         minx = min(n, minx)
  106.                                                 offset = len(str(n))-(sublen-qmcnt) #not return n, qmcnt,because #(9100) 99,100,the sublen is 2,3, qmcnt is 2 but offset is 1
  107.                                                 founded = True                                               
  108.                 if founded:
  109.                         return minx, offset

  110. def findposition(n):
  111.     minx, offset = getMinXinSequence(n)
  112.     #print(minx, offset)
  113.     return getXindex(minx)+offset

  114. print(9100, findposition(9100)) #9100=>99,100=>99,1=>188

  115. print(getMinXinSequence(2312)) #123,1
  116. print(getMinXinSequence(99100101))
  117. print(getMinXinSequence(99))
复制代码

点评

我很赞同!: 1.0
我很赞同!: 1
测试123456798和123459876,发现结果有误差,都差了8位  发表于 2017-9-17 23:25
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2017-9-17 09:59:05 | 显示全部楼层
凑个热闹~
先发个 低效版
  1. import itertools as itr

  2. def findposition(n):
  3.     s = str(n)
  4.     l = len(s)
  5.     ls=''
  6.     for i in itr.count(1):
  7.         i = str(i)
  8.         ls += i
  9.         try:
  10.             idx = ls.index(s, -(l+len(i)))
  11.             if idx>-1:
  12.                 return idx
  13.         except:
  14.             pass
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2017-9-17 12:44:14 | 显示全部楼层
高效率版
匆忙之作,有点乱~
  1. import re

  2. def 单体(n):
  3.     i = len(str(n))
  4.     sm = sum(9*10**x*(x+1)for x in range(i-1))
  5.     a = n-10**(i-1)
  6.     idx = sm+a*i
  7.     return idx

  8. def 验证(i, n, s):
  9.     ln = len(s)
  10.     lst = [str(i)for i in range(n-ln//i-1, n+ln//i+1)]
  11.     tmp = ''.join(lst)
  12.     try:
  13.         ix = tmp.index(s)
  14.     except ValueError:
  15.         return None
  16.    
  17.     idx = 单体(int(lst[0])) + ix
  18.     return idx

  19. def 跨段(s):
  20.     rslt = re.findall('(9+)(10*)', s)
  21.     if len(rslt)==1:
  22.         i,n = max(((len(x),int(x))for x in rslt[0]), key=lambda t:t[1])
  23.         idx = 验证(i, n, s)
  24.         if idx is not None:
  25.             return idx

  26. def 等差1(s):
  27.     for i in range(2,len(s)):
  28.         rslt = re.findall(r'(?=(\d{%d})(\d{%d}))' %(i,i), s)
  29.         if rslt:
  30.             for n,m in rslt:
  31.                 if int(m)-int(n) == 1:
  32.                     idx = 验证(i, int(n), s)
  33.                     if idx is not None:
  34.                         return idx


  35. def 同数(s):
  36.     rslt = re.findall(r'(?=(([1-9])\d+?)\2)', s)
  37.     if rslt:
  38.         for n,m in rslt:
  39.             idx = 验证(len(n), int(n), s)
  40.             if idx is not None:
  41.                 return idx
  42.    

  43. def findposition(n):
  44.     "Your code here"
  45.     s = str(n)
  46.     try:
  47.         return ''.join(str(i)for i in range(1, 110)).index(s)
  48.     except ValueError:
  49.         pass
  50.    
  51.     return 跨段(s) or 等差1(s) or 同数(s) or 单体(n)

  52. if __name__ == '__main__':
  53.     loc = {
  54.         '123456798': 1000000071,
  55.         '9100': 188,
  56.         '123459876': 1000027773,
  57.         '456': 3,
  58.         '91': 8,
  59.         '789': 6,
  60.         '454': 79,
  61.         '910000000':68888888,
  62.         '9100009':5348889
  63.     }

  64.     data = [456, 789, 91, 454, 9100, 123456798, 123459876, 910000000, 9100009]
  65.     for n in data:
  66.         print(n,findposition(n), loc.get(str(n), None),sep=', ')
复制代码


结果:
  1. 456, 3, 3
  2. 789, 6, 6
  3. 91, 8, 8
  4. 454, 79, 79
  5. 9100, 188, 188
  6. 123456798, 1000000071, 1000000071
  7. 123459876, 1000027773, 1000027773
  8. 910000000, 68888888, 68888888
  9. 9100009, 5348889, 5348889
复制代码

点评

我很赞同!: 5.0
我很赞同!: 5
测试通过,程序非常高效,不愧是大神!  发表于 2017-9-17 23:28
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-3-29 03:06

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表