zoukankan      html  css  js  c++  java
  • 10 Row Abacus

    输入一个数字,按规定输出符号排列。此题的原型为算盘,用符号排列模拟算盘的显示。具体要求如下

    #########################################################################
    #                 10-row School abacus
    #                         by
    #                      Michael H
    #########################################################################
    #       Description partially extracted from from wikipedia 
    #
    #  Around the world, abaci have been used in pre-schools and elementary
    #
    # In Western countries, a bead frame similar to the Russian abacus but
    # with straight wires and a vertical frame has been common (see image).
    # Helps schools as an aid in teaching the numeral system and arithmetic
    #
    #         |00000*****   |     row factor 1000000000
    #         |00000*****   |     row factor 100000000
    #         |00000*****   |     row factor 10000000 
    #         |00000*****   |     row factor 1000000
    #         |00000*****   |     row factor 100000
    #         |00000*****   |     row factor 10000
    #         |00000*****   |     row factor 1000
    #         |00000****   *|     row factor 100     * 1
    #         |00000***   **|     row factor 10      * 2
    #         |00000**   ***|     row factor 1       * 3
    #                                        -----------    
    #                             Sum                123 
    #
    # Each row represents a different row factor, starting with x1 at the
    # bottom, ascending up to x1000000000 at the top row.     
    ######################################################################
    
    # TASK:
    # Define a procedure print_abacus(integer) that takes a positive integer
    # and prints a visual representation (image) of an abacus setup for a 
    # given positive integer value.
    # 
    # Ranking
    # 1 STAR: solved the problem!
    # 2 STARS: 6 < lines <= 9
    # 3 STARS: 3 < lines <= 6
    # 4 STARS: 0 < lines <= 3

    最终的判定是以实现效果所写代码的行数,可见这里是要我们在练习基本语法的同时,去思考最紧凑的方法。

    对于算盘来说,在初始状态每一列都是相同的,且输入数字的权重和列数是对应的,从对实际问题的映射和评分要求考虑,下面的代码十分吻合

    def print_abacus(value):
        for c in ((10 - len(str(value))) * '0' + str(value)):
            print('|00000*****|'[ : -1 - int(c)] + '   ' + '|00000*****|'[-1 - int(c) : ])

    但是一种更为容易想到,使用简单逻辑判断的方法如下

    def print_abacus(value):
        num_len = len(str(value))
        for i in range(10 - num_len):
            print('|00000*****   |')
        for c in str(value):
            c_int = int(c)
            if(c_int > 5):
                print('|' + (10 - c_int) * '0' + '   ' + (c_int - 5) * '0' + '*****|')
            else:
                print('|00000' + (5 - c_int) * '*' + '   ' + c_int * '*' + '|')
            
            

    题目及解答参考来自Udacity的Intro to Computer Science课程:https://classroom.udacity.com/courses/cs101/lessons/48689154/concepts/6986185940923

  • 相关阅读:
    【ANT】时间戳
    【ANT】java项目生成文件示例
    【ANT】description元素和属性
    【ANT】一个简单的ANT生成文件build.xml
    JMeter循环控制器循环次数使用变量控制注意事项
    JMeter获取CSV文件行数
    JMeter执行顺序
    JMeter脚本java代码String数组要写成String[] args,不能写成String args[],否则报错。
    1-眼界与思维
    19-python 自己建立词库并实现文章汉语词频统计
  • 原文地址:https://www.cnblogs.com/qingkai/p/8756794.html
Copyright © 2011-2022 走看看