Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1747757
  • 博文数量: 297
  • 博客积分: 285
  • 博客等级: 二等列兵
  • 技术积分: 3006
  • 用 户 组: 普通用户
  • 注册时间: 2010-03-06 22:04
个人简介

Linuxer, ex IBMer. GNU https://hmchzb19.github.io/

文章分类

全部博文(297)

文章存档

2020年(11)

2019年(15)

2018年(43)

2017年(79)

2016年(79)

2015年(58)

2014年(1)

2013年(8)

2012年(3)

分类: Python/Ruby

2016-06-04 11:13:22

1. 罗马数字和数字的转化
下面两段代码出自

点击(此处)折叠或打开

  1. def to_roman(x):
  2.     anums = [1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1]
  3.     rnums = "M CM D CD C XC L XL X IX V IV I".split()
  4.     ret=[]
  5.     for a,r in zip(anums,rnums):
  6.         n,x=divmod(x,a)
  7.         ret.append(r*n)
  8.     return "".join(ret)

  9. def test_to_roman():
  10.     test=(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,25,30,40,
  11.         50,60,69,70,80,90,99,100,200,300,400,500,600,666,700,800,900,
  12.         1000,1009,1444,1666,1945,1997,1999,2000,2008,2010,2011,2500,
  13.         3000,3999)

  14.     for i in test:
  15.         print("number {} to roman is {}".format(i,to_roman(i)))

  16. #test_to_roman()

点击(此处)折叠或打开

  1. def to_arabic(x):
  2.     decoder=dict(zip('MDCLXVI',(1000,500,100,50,10,5,1)))
  3.     result=0
  4.     for r,r1 in zip(x,x[1:]):
  5.         rd,rd1=decoder[r],decoder[r1]
  6.         if rd < rd1:
  7.             result+=-rd
  8.         else:
  9.             result+=rd
  10.     return result+decoder[x[-1]]

  11. def test_to_arabic():
  12.     ll='MCMXC MMVIII MDCLXVI'.split()
  13.     for i in ll:
  14.         print(to_arabic(i))

  15. test_to_arabic()
下面的代码是我自己写的。 和别人的比,果然是差很多了。

点击(此处)折叠或打开

  1. def to_arabic2(s):
  2.     #s: sequence
  3.     decoder=dict(zip('MDCLXVI',(1000,500,100,50,10,5,1)))
  4.     result=0
  5.     for i in range(len(s)-1):
  6.         value=decoder.get(s[i])
  7.         vnext=decoder.get(s[i+1])
  8.         if value < vnext:
  9.             result+=-value
  10.         else:
  11.             result+=value
  12.     return result+decoder.get(s[-1])

  13. def test_to_arabic2():
  14.     ll='MCMXC MMVIII MDCLXVI'.split()
  15.     for i in ll:
  16.         print(to_arabic2(i))

  17. test_to_arabic2()
2. 一个puzzle ,出自: Puzzle:
A merchant has a 40 kg weight which he used in his shop. Once, it fell from his hands and was broken into 4 pieces. But surprisingly, now he can weigh any weight between 1 kg to 40 kg with the combination of these 4 pieces.
So question is, what are weights of those 4 pieces?
这个哥们的代码写的太通用了,还加上pieces. 即N个pieces,sum(n)==L, 同时可以表示1->L,很有趣的题目

点击(此处)折叠或打开

  1. import itertools as it
  2. def find_weights(weight,pieces):
  3.     full=range(1,weight+1)
  4.     all_nums=set(full)
  5.     comb=[x for x in it.combinations(full,pieces) if sum(x)==40]
  6.     funcs=(lambda x: 0, lambda x: x, lambda x: -x)
  7.     for c in comb:
  8.         sums=set()
  9.         for fmap in it.product(funcs,repeat=pieces):
  10.             s=sum(f(x) for x,f in zip(c,fmap))
  11.             if s>0:
  12.                 sums.add(s)
  13.                 if sums==all_nums:
  14.                     return c


  15. print(find_weights(40,5))


阅读(1222) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~