一个简单的Python程序。包括数字的位数

2024-10-01 22:31:43 发布

您现在位置:Python中文网/ 问答频道 /正文

所以基本上,这个程序读取5个数字: 十、 是的,从,跳,直到

每个数字之间用空格隔开。举个例子:

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)。i、 e:X是2,我们是34,它也是一个BOOZ)。你知道吗

BANG是相同的,但是有Y。如果currentPos同时是BOOZ&;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并退出程序。否则,如果我们不能使用jump跳转到达until(如果startFrom+ n * jump == untiln是一个整数),那么我们需要打印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

我的程序是太乱了(我做了一个有很多如果循环。。包括ifcurrentPos==until,因此在该原因中,它不会打印最后一个打印项的逗号(,)。。但就像我说的,所有的一切都是如此混乱,国际单项体育联合会的条件是如此漫长和混乱,我只是把它全部删除,并决定在这里要求一个更好的解决方案。你知道吗

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


Tags: andfrom程序数字becanintuntil
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
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)

相关问题 更多 >

    热门问题