Python中的一个简单程序。涉及数字的数字

时间:2013-11-28 02:18:28

标签: python algorithm

所以基本上,这个编程读取5个数字: X,Y,startFrom,jump,直到

用空格分隔每个数字。一个例子:

3 4 1 1 14
X = 3
Y = 4
1 = startFrom
jump = 1
until = 14

为了做到这一点,我使用了:

    #get X, Y, startFrom, jump, until
    parameters = raw_input()

    parametersList = parameters.split()

    X = int(parametersList[0])
    Y = int(parametersList[1])
    #start from startFrom 
    startFrom = int(parametersList[2])
    #jumps of <jump>
    jump = int(parametersList[3])
    #until (and including) <until>
    until = int(parametersList[4])

程序输出一个链(或者你想称之为),我们称之为BOOZBANG,当BOOZ为X时(如果数字中存在)(即X是2,我们是23,所以这是一个BOOZ)。为了检查(我使用map(int, str(currentPos)),当我的currentPos(我们的号码)最初基本上是startFrom时,随着我们的进展(每次添加跳转),它越来越接近接近until),或者如果X除以currentPos(X%num == 0。即:X是2而我们是34,它也是BOOZ)。

BANG是相同的,但有Y.如果currentPos同时是BOOZ&amp; BANG,输出为BOOZ-BANG

startFrom, startFrom+ jump, startFrom+2*jump, startFrom+3*jump, ..., until

我们知道读取的数字是int类型,但我们需要确保它们对游戏有效。 X和Y必须在1到9之间。否则,我们打印(已读取所有5个数字):X and Y must be between 1 and 9并退出编程。 另外,跳转不能为0.如果是,我们打印jump can't be 0并退出编程。否则,如果我们无法使用until跳转jump startFrom+ n * jump == until n是一个int号),那么我们需要打印can't jump from <startFrom> to <until>并退出前卫。 我的算法太乱了,有很多ifs和什么不是,所以我也想帮助那个)

因此,对于我们的第一个示例(3 4 1 1 14),输出应为:

1,2,BOOZ,BANG,5,BOOZ,7,BANG,BOOZ,10,11,BOOZ-BANG,BOOZ,BANG

另一个例子:

-4 -3 4 0 19

输出:

X and Y must be between 1 and 9
juump can't be 0

另一:

5 3 670 7 691

输出:

BOOZ,677,BANG,691

另一:

0 3 4 -5 24

输出:

X and Y must be between 1 and 9
can't jump from 4 to 24

另一:

3 4 34 3 64

输出:

BOOZ-BANG,BOOZ,BANG,BOOZ-BANG,BANG,BANG,BANG,55,58,61,BANG

我的编程太麻烦了(我做了一个带有大量ifs的while循环..包括if currentPos==until因此导致它不会打印最后一个打印项目的逗号(,)等等。但就像我说的那样,所有这些都是如此混乱,ifs的条件如此长而且凌乱,以至于我把它全部删除了,并决定在这里寻求更好的解决方案。

谢谢你们 我希望它足够清楚

2 个答案:

答案 0 :(得分:2)

我的版本没有if:)

parameters = raw_input()
sx, sy, sstartfrom, sjump, suntil = parameters.split()
x = "0123456789".index(sx)
y = "0123456789".index(sy)
startfrom = int(sstartfrom)
jump = int(sjump)
until = int(suntil)
for i in range(startfrom, until+jump, jump):
    si = str(i)
    booz = sx in si or i%x == 0
    bang = sy in si or i%y == 0
    print [[si, 'BANG'],['BOOZ','BOOZ-BANG']][booz][bang]

获取逗号的最简单方法是将循环移动到生成器

def generator():
    for i in range(startfrom, until+jump, jump):
        si = str(i)
        booz = sx in str(i) or i%x == 0
        bang = sy in str(i) or i%y == 0
        yield [[si, 'BANG'],['BOOZ','BOOZ-BANG']][booz][bang]

print ",".join(generator())

示例输出

$ echo 3 4 1 1 14 | python2 boozbang.py
1,2,BOOZ,BANG,5,BOOZ,7,BANG,BOOZ,10,11,BOOZ-BANG,BOOZ,BANG
$ echo 5 3 670 7 691 | python2 boozbang.py 
BOOZ,677,BANG,691
$ echo 3 4 34 3 64 | python2 boozbang.py 
BOOZ-BANG,BOOZ,BANG,BOOZ-BANG,BANG,BANG,BANG,55,58,61,BANG

答案 1 :(得分:0)

def CheckCondition(number, xOrY):
    return (xOrY in str(number)) or not (number % xOrY)

def SomeMethod(X, Y, start, jump, end):
    for i in range(start, end, jump):
        isPassX = CheckCondition(i, X)
        isPassY = CheckCondition(i, Y)
        if isPassX and isPassY:
            print "BOOZ-BANG"
        elif isPassX:
            print "BOOZ"
        elif isPassY:
            print "BANG"
        else:
            print i



def YourMethod():
    (X, Y, start, jump, end) = (3, 4, 1, 1, 14)
    if (X not in range(1, 10) or Y not in range(1, 10)):
        print "X and Y must be between 1 and 9"
    if jump <= 0:
        print "juump can't be less than 0"

    SomeMethod(X, Y, start, jump, end)