• 用算盘图形表示数字,Python实现


     1 #########################################################################
     2 #                 10-row School abacus
     3 #                         by
     4 #                      Michael H
     5 #########################################################################
     6 #       Description partially extracted from from wikipedia 
     7 #
     8 #  Around the world, abaci have been used in pre-schools and elementary
     9 #
    10 # In Western countries, a bead frame similar to the Russian abacus but
    11 # with straight wires and a vertical frame has been common (see image).
    12 # Helps schools as an aid in teaching the numeral system and arithmetic
    13 #
    14 #         |00000*****   |     row factor 1000000000
    15 #         |00000*****   |     row factor 100000000
    16 #         |00000*****   |     row factor 10000000 
    17 #         |00000*****   |     row factor 1000000
    18 #         |00000*****   |     row factor 100000
    19 #         |00000*****   |     row factor 10000
    20 #         |00000*****   |     row factor 1000
    21 #         |00000****   *|     row factor 100     * 1
    22 #         |00000***   **|     row factor 10      * 2
    23 #         |00000**   ***|     row factor 1       * 3
    24 #                                        -----------    
    25 #                             Sum                123 
    26 #
    27 # Each row represents a different row factor, starting with x1 at the
    28 # bottom, ascending up to x1000000000 at the top row.     
    29 ######################################################################
    30 
    31 # TASK:
    32 # Define a procedure print_abacus(integer) that takes a positive integer
    33 # and prints a visual representation (image) of an abacus setup for a 
    34 # given positive integer value.
    35 # 
    36 # Ranking
    37 # 1 STAR: solved the problem!
    38 # 2 STARS: 6 < lines <= 9
    39 # 3 STARS: 3 < lines <= 6
    40 # 4 STARS: 0 < lines <= 3
    41 
    42 def print_abacus(value):
    43     row_number = 1
    44     while row_number <= 10:
    45         number = value // (10**(10-row_number))
    46         if number == 0:
    47             print '|' + '0'*5 + '*'*5 + ' '*3 + '|'
    48         elif number > 0 and number < 5:
    49             print '|' + '0'*5 + '*'*(5-number) + ' '*3 + '*'*number + '|'
    50         else:
    51             print '|' + '0'*(10-number) + ' '*3 + '0'*(number-5) + '*'*5 + '|'
    52         value -= number*(10**(10-row_number))
    53         row_number += 1
    54         
    55     
    56     
    57         
    58 
    59 ###  TEST CASES
    60 print "Abacus showing 0:"
    61 print_abacus(0)
    62 #>>>|00000*****   |
    63 #>>>|00000*****   |
    64 #>>>|00000*****   |
    65 #>>>|00000*****   |
    66 #>>>|00000*****   |
    67 #>>>|00000*****   |
    68 #>>>|00000*****   |
    69 #>>>|00000*****   |
    70 #>>>|00000*****   |
    71 #>>>|00000*****   |
    72 print "Abacus showing 12345678:"
    73 print_abacus(12345678)
    74 #>>>|00000*****   |
    75 #>>>|00000*****   |
    76 #>>>|00000****   *|
    77 #>>>|00000***   **|
    78 #>>>|00000**   ***|
    79 #>>>|00000*   ****|
    80 #>>>|00000   *****|
    81 #>>>|0000   0*****|
    82 #>>>|000   00*****|
    83 #>>>|00   000*****|
    84 print "Abacus showing 1337:"
    85 print_abacus(1337)
    86 #>>>|00000*****   |
    87 #>>>|00000*****   |
    88 #>>>|00000*****   |
    89 #>>>|00000*****   |
    90 #>>>|00000*****   |
    91 #>>>|00000*****   |
    92 #>>>|00000****   *|
    93 #>>>|00000**   ***|
    94 #>>>|00000**   ***|
    95 #>>>|000   00*****|

    结果

  • 相关阅读:
    Linux 系统目录 以及常见命令
    设计模式C++学习笔记之十三(Decorator装饰模式)
    设计模式C++学习笔记之二(Proxy代理模式)
    Linux 文件基本操作
    Linux 文件系统基本结构 以及命令行 管理
    Linux 入门 bash语句 第三课
    JMeter学习参数化User Defined Variables与User Parameters
    JMeter使用中遇到的问题:Jmeter Debug
    JMeter学习(九)FTP测试计划
    JMeter学习(八)JDBC测试计划-连接Oracle
  • 原文地址:https://www.cnblogs.com/jk123vip/p/3918981.html
Copyright © 2020-2023  润新知