首页 > 代码库 > 用算盘图形表示数字,Python实现

用算盘图形表示数字,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 but11 # with straight wires and a vertical frame has been common (see image).12 # Helps schools as an aid in teaching the numeral system and arithmetic13 #14 #         |00000*****   |     row factor 100000000015 #         |00000*****   |     row factor 10000000016 #         |00000*****   |     row factor 10000000 17 #         |00000*****   |     row factor 100000018 #         |00000*****   |     row factor 10000019 #         |00000*****   |     row factor 1000020 #         |00000*****   |     row factor 100021 #         |00000****   *|     row factor 100     * 122 #         |00000***   **|     row factor 10      * 223 #         |00000**   ***|     row factor 1       * 324 #                                        -----------    25 #                             Sum                123 26 #27 # Each row represents a different row factor, starting with x1 at the28 # bottom, ascending up to x1000000000 at the top row.     29 ######################################################################30 31 # TASK:32 # Define a procedure print_abacus(integer) that takes a positive integer33 # and prints a visual representation (image) of an abacus setup for a 34 # given positive integer value.35 # 36 # Ranking37 # 1 STAR: solved the problem!38 # 2 STARS: 6 < lines <= 939 # 3 STARS: 3 < lines <= 640 # 4 STARS: 0 < lines <= 341 42 def print_abacus(value):43     row_number = 144     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 += 154         55     56     57         58 59 ###  TEST CASES60 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*****|

 

结果