用算盘图形表示数字,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*****|

结果

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

时间: 2024-07-30 02:04:23

用算盘图形表示数字,Python实现的相关文章

给图片右上角加上红色的数字(python)

给图片右上角加上红色的数字(python) by 伍雪颖 from PIL import Image,ImageDraw,ImageFont def addNum(filePath): img = Image.open(filePath) size = img.size fontSize = size[1] / 4 draw = ImageDraw.Draw(img) ttFont = ImageFont.truetype("/Library/Fonts/arial.ttf", fon

每日一题 LeetCode 有效的数字 Python实现

有效的数字(简单题) class Solution:def isValid(self, s):""":type s: str:rtype: bool""" a=list(s) b=[] #存放左括号的栈 qc:list当做栈 c={'(':')','[':']','{':'}'} #字典存储 qc:key:value 键:值 for i in a: if i=='': return True elif i in c: #如果是字典中的键,即左括号

提取zabbix监控平台单台服务器图形并发邮件python实现

需求:希望每天邮件发出当天某台服务器的监控状态,如果某天都登陆zabbix截图很麻烦,而且并不能保证每天都准点操作,于是写了一段脚本实现自动抓取图片,并组装成html,通过定时邮件发送,实现日报自动化. 一.效果图: 二.代码: #!/usr/bin/env python # -*- coding: utf-8 -*- import MySQLdb import datetime import cookielib, urllib2,urllib import smtplib from email

pylab.show()没有显示图形图像(python的matplotlib画图包)

import pylabpylab.ion() x = pylab.arange( 0, 10, 0.1)y = pylab.sin(x)pylab.plot(x,y, 'ro-')pylab.show()  pylab.savefig('temp.png') ============================ # plotting with the pylab module from matplotlib # free from: http://matplotlib.sourceforg

【剑指Offer】数组中出现次数超过一半的数字 Python版

题目描述 给定一个数组,如果这个数组中有一个数字出现的次数超过数组长度的一半,请找出这个数字:如果不存在,则返回0. 思路分析 对于一个数组而言,满足题目要求的数字最多只有一个,可以采用数字相互抵消的思想.在遍历数组时,储存两个值now和count,now是当前数字,count是该数字的标记.当下一个数字与now相等时,标记count的值加1,如果不相等,则减1:当标记count的值变为0时,则将下一个数字的值用now来存储,并将count的值置为1,继续遍历完数组. 如果数组中存在出现次数超过

数组中只出现一次的数字-python

思路:用一个字典统计出每个数字出现的次数,然后遍历字典,找到只出现一次的 # -*- coding:utf-8 -*- class Solution: # 返回[a,b] 其中ab是出现一次的两个数字 def FindNumsAppearOnce(self, array): # write code here dic = {} for i in array: if i in dic: dic[i] += 1 else: dic[i] = 1 res = [] for i in dic: if d

剑指offer旋转数组的最小数字python

题目描述 把一个数组最开始的若干个元素搬到数组的末尾,我们称之为数组的旋转.输入一个非递减排序的数组的一个旋转,输出旋转数组的最小元素.例如数组{3,4,5,1,2}为{1,2,3,4,5}的一个旋转,该数组的最小值为1.NOTE:给出的所有元素都大于0,若数组大小为0,请返回0. 思路 旋转后的数组先递增,然后突然断层,让后又递增,所以,只要找到数组突然变小的那个数字即可. 代码 # -*- coding:utf-8 -*- class Solution: def minNumberInRot

42.和为S的两个数字(python)

题目描述 输入一个递增排序的数组和一个数字S,在数组中查找两个数,使得他们的和正好是S,如果有多对数字的和等于S,输出两个数的乘积最小的. 输出描述: 对应每个测试案例,输出两个数,小的先输出. 方法一:两层for循环,得到的就是乘积最小的 1 class Solution: 2 def FindNumbersWithSum(self, array, tsum): 3 # write code here 4 for i in range(0,len(array)-1): 5 for j in r

如何在DataGrid里面使用动态图形表示数字

[译者注]在本文中,作者提出了使用k-means算法来对图像进行色彩还原,介绍算法的步骤,同时应用在图像上,通过对比还原前后的图像,来证明k-means算法的有效性.以下为译文: k-means是机器学习中最著名.最广泛使用的算法之一.在这篇文章中,将使用k-means算法来减少图像上的颜色(但不减少像素),从而也减少了图像的大小.在这个领域不需要任何基础知识,因为可执行应用程序文件(大小为150MB,这是由于长时间的Spark依赖)已经提供了友好的用户界面.所以你可以很容易地用不同的图像来做实