2016-11-16 47 views
-2

定义一个名为nested_increasing_additions(n)的函数,它接收一个正整数(n)返回如在以下实施例中示出的串如何做到这一点“如果n是3,则返回1 + .. 1 + 2 + .. 1 + 2 + 3 + ..`”

如果n3,函数应该返回字符串: 1+..1+2+..1+2+3+..

如果n5,函数应该返回字符串: 1+..1+2+..1+2+3+..1+2+3+4+..1+2+3+4+5..+

我的想法是,我可以使n列表[1,2,3]并使用while循环或for循环重复n次。对于第一个循环它返回1+..,对于第二个循环它以某种方式返回1+2..(我不知道)它在2处停止,这与重复时间相同。

我不知道我是否正确思考。需要一些帮助和解释!谢谢!

+1

你能证明你的代码? – Beri

+3

为什么你的第一个字符串以'..'结尾,但第二个字符串不是? “ – Keiwan

回答

1

你可以建立一个脚印整个字符串一步,每一步你已经添加最后记住:

def nested_increasing_additions(n): 
    complete_string = "" 
    add_string = "" 
    for i in range(1,n+1): 
     add_string += str(i) + "+" 
     complete_string += add_string + ".." 
    return complete_string 

print(nested_increasing_additions(1)) 
print(nested_increasing_additions(3)) 
print(nested_increasing_additions(5)) 

与python3输出是:

1+.. 
1+..1+2+..1+2+3+.. 
1+..1+2+..1+2+3+..1+2+3+4+..1+2+3+4+5+.. 
0
def nested_increasing_additions(n): 
    l=[''] 
    for i in range(1, n+1): 
     l.append(l[-1]+str(i)+'+') 
    return '..'.join(l[1:]) 

这将在末尾返回一个没有..的字符串。如果你想要的,只是做这些字符串结果return '..'.join(l[1:]) + '..'

4

连续评估在四面体数的序列。例如,对于输入5,输出计算结果为35。这是你需要球体的数目来构建边长的四面体5.

​​

要了解它如何与之和在问题,请注意,四面体的离散“体积”将等于从上到下的三角形“切片”的总和。

35 = 1 + 3 + 6 + 10 + 15 
    = 1 + (1+2) + (1+2+3) + (1+2+3+4) + (1+2+3+4+5) 

通过类似的说法,三角形数字由连续整数的切片组成。


请原谅数学,很难(但不是不可能)以适应封闭形式解成期望的输出格式。

def tetrahedral(n): 
    return n*(n+1)*(n+2)//6 


def string_generator(n): 
    x = tetrahedral(n) 
    n = N = 1 
    while x > 0: 
     while n <= N: 
      yield str(n) + '+' 
      n += 1 
     x -= N*(N+1)//2 
     n = 1 
     N += 1 
     yield '..' 


def nested_increasing_additions(n): 
    return ''.join(string_generator(n)) 
+0

”函数应该返回* string *“强调我的 –

+4

哈。哎呦。我得到了[nerd-sniped](https://xkcd.com/356/)! – wim

+0

@PatrickHaugh固定 – wim

0

可以使用这样的东西。

def nested_increasing_additions(n): 
    string = "" 
    for i in range(1,n+1): #1 
     for j in range(1,i+1): #2 
      string += str(j)+"+" #4 
     string += ".." #5 
    print(string) 

这里是nested_increasing_additions的打印输出(4) 1 + .. 1 + 2 + ... 1 + 2 + 3 + 1 .. + 2 + 3 + 4 + .. 我认为这是自解释性,没什么复杂的

0

假设你确实想要的".."在每个返回的字符串的结束和递归是好的,这里有一个解决方案:

def nested_increasing_additions(n): 
    if n == 1: 
     return "1+.." 
    else: 
     return nested_increasing_additions(n-1) + '%s+..' % '+'.join(str(i) for i in range(1, n+1)) 

print(nested_increasing_additions(3)) 
print(nested_increasing_additions(5)) 
type(nested_increasing_additions(1)) 

打印

1+..1+2+..1+2+3+.. 
1+..1+2+..1+2+3+..1+2+3+4+..1+2+3+4+5+.. 
<type 'str'> 


说明

  • 第一复位,并且当经由来自另一半减法中的参数值传递达到1如果(真)块结束递归调用。
  • 后半部分(else块)调用与n下一迭代减去1和当前迭代的字符串生成(如果n!=1)。
  • 复杂的看着代码'%s+..' % '+'.join(str(i) for i in range(1, n+1))只是由range函数生成的数字列表的串联,变成了串并"+.."
    • range(1, n+1)返回从1开始,直到n+1所以range(1,3)产量[1,2]整数列表。这通过加入,每个号码之间放置一个+
0

这个怎么样:

def nested_increasing_additions(n): 

    string = "" 
    new_string = "" 
    dot = ".." 

    for i in range(1, n+1): 
     new_string+=('{}+'.format(i)) 
     string = string+new_string+dot 
     print(string) 

    return (string) 

输出:

nested_increasing_additions(3)  

'1+..1+2+..1+2+3+..' 
相关问题