美文网首页
codewars(python)练习笔记十三:用*造一个Towe

codewars(python)练习笔记十三:用*造一个Towe

作者: 曹波波 | 来源:发表于2018-05-16 14:10 被阅读6次

    codewars(python)练习笔记十三:用*造一个Tower

    题目

    Build Tower

    Build Tower by the following given argument:
    number of floors (integer and always greater than 0).

    Tower block is represented as *

    Python: return a list;
    JavaScript: returns an Array;
    C#: returns a string[];
    PHP: returns an array;
    C++: returns a vector<string>;
    Haskell: returns a [String];
    Ruby: returns an Array;
    

    Have fun!

    题目大意:

    屏幕快照 2018-05-16 下午2.11.14.png

    Go challenge Build Tower Advanced once you have finished this :)

    我的解法:

    def tower_builder(n_floors):
        if n_floors <= 0:return []
        temp = []
        for i in range(0,n_floors):
            line = ''
            for j in range(0,n_floors*2-1):
                if (j < n_floors and i + j >= n_floors-1) or (j+1 > n_floors and j - n_floors < i):
                    line += '*'
                else:
                    line += ' '
            temp.append(line)
        return temp
    

    马蛋,这是大学刚开始学C的时候的题目,现在看,仍然不过时。但是我仍然不太会写。试了试,好歹先把题目解出来。

    最牛逼的解法:

    def tower_builder(n):
        return [("*" * (i*2-1)).center(n*2-1) for i in range(1, n+1)]
    

    其他解法一:

    def tower_builder(n_floors):
        if n_floors <= 0:return []
        count = 1
        result = []
        for i in range(1, n_floors + 1):
          stars = '*' * (2 * i - 1)
          space = ' ' * (n_floors - i)
          result.append(space + stars + space)
        return result
    

    其他解法二:

    def tower_builder(n_floors):
        tower = []
        spacing = n_floors - 1
        stars = 1
        for i in range(0, n_floors):
            tower.append(' ' * spacing + '*' * stars + spacing * ' ')
            stars += 2
            spacing -= 1
        return tower
    

    其他解法三:

    def tower_builder(n_floors):
        floors = []
        for i in range(n_floors):
            n_floors -= 1
            floors.append(' ' * n_floors + '*' * (i * 2 + 1) + ' ' * n_floors)
        return floors
    

    其他解法四:

    def tower_builder(n_floors):
        return ['{0}{1}{0}'.format(' ' * (n_floors - 1 - x), '*' * (1 + 2 * x)) for x in range(n_floors)]
    

    相关文章

      网友评论

          本文标题:codewars(python)练习笔记十三:用*造一个Towe

          本文链接:https://www.haomeiwen.com/subject/bpwudftx.html