[英]If/else alternative
I'm new to programming, but I'm doing a task for an assignment. 我是编程新手,但我正在完成一项任务。 I've written function so that the amount of Bytes that the user enters is translated into a "human readable" format.
我编写了函数,以便用户输入的字节数转换为“人类可读”格式。 Basically to return in the appropriate B,KB,MB,GB,TB.
基本上返回适当的B,KB,MB,GB,TB。
However, the assignment says we should try writing the code without loops or if statements, but we can use an array. 但是,赋值说我们应该尝试编写没有循环或if语句的代码,但我们可以使用数组。
I was wondering how I might do this... 我想知道我怎么做这个......
Here's my current code: 这是我目前的代码:
def memory_size(n):
if n < 1024:
print n,"B"
if 1024 <= n < 1048576:
nKB = n / 1024
print nKB,"KB"
if 1048576 <= n < 1073741824:
nMB = (n / 1024) / 1024
print nMB,"MB"
if 1073741824 <= n < 1.099511628*(10**12):
nGB = ((n / 1024) / 1024) / 1024
print nGB,"GB"
if 1.099511628*(10**12) <= n < 1.125899907*(10**15):
nTB =(((n / 1024) / 1024) / 1024) / 1024
print nTB,"TB"
Because this is a homework, I'm not going to post a whole code, just going to say that it can feat in a few lines. 因为这是一个家庭作业,我不打算发布整个代码,只是说它可以在几行中壮举。
take a look at math
library specifically to pow
and log
看看
math
库专门给pow
和log
You can define you ranges like this: 你可以定义这样的范围:
>>> ranges = ['B', 'KB', 'MB', 'GB', 'TB']
Then 然后
log
of n
and truncate decimal. n
log
和截断小数。 math.pow
and the value from #1 math.pow
和#1中的值计算大小 ranges
array by using number from #1 to get a value "b, kb, mb and so on". ranges
数组以获取值“b,kb,mb等”。 This way you will not use any if/else
statements or for/while
loops. 这样您就不会使用任何
if/else
语句或for/while
循环。
Without giving you code, because this is homework, the basic approach is that you store the range data (a heap might be a nice solution), and then search through it to find the entry for the given range, and have that entry store the other data you need. 没有给你代码,因为这是作业,基本的方法是你存储范围数据(堆可能是一个很好的解决方案),然后搜索它以找到给定范围的条目,并让该条目存储您需要的其他数据。 Then you, only have one version of the code like this:
然后,你只有一个版本的代码,如下所示:
nKB = n / 1024
print nKB,"KB"
With the retrieved data in place of the hardcoded constants. 使用检索到的数据代替硬编码常量。
Update: 更新:
As DavidRobinson points out, you can't use loops, and any method of searching implicitly or explicitly uses a loop. 正如DavidRobinson指出的那样,你不能使用循环,任何隐式或显式搜索方法都使用循环。 So, the alternative is to take your input data, and transform it into a form which can be used for a single, constant-time lookup against your chosen datastructure.
因此,另一种方法是获取输入数据,并将其转换为可用于对所选数据结构进行单次,恒定时间查找的形式。 An array of logarithms is the way to go here, and again map to the data which replaces constants in the above snippet.
一个对数数组是这里的方法,并再次映射到替换上述代码段中的常量的数据。
Couple thoughts; 几个想法; neither fulfill the requirements, but maybe they'll help your thinking
既不满足要求,也许它们会帮助你思考
Method 1: Create a dictionary from lower/upper bounds to divisors and display strings. 方法1:创建从下限/上限到除数和显示字符串的字典。 Loop through the dictionary to find the appropriate entry, then use the values.
循环遍历字典以查找适当的条目,然后使用值。
Method 2: divide the input by 1024 until it is less than 1024. Use the number of operations to look up the display string. 方法2:将输入除以1024,直到小于1024.使用操作数查找显示字符串。
Just for fun. 纯娱乐。 You can use results of the division on 1024 as dictionary keys.
您可以使用1024上的除法结果作为字典键。 Dictionary can store the resulting data.
字典可以存储结果数据。 Here some python 3 code, which solves the task without loops or if statements:
这里有一些python 3代码,它解决了没有循环或if语句的任务:
def memory_size(n):
is_b = int(n >= 1)
is_kb = int(n / 1024 >= 1)
is_mb = int(n / 1024**2 >= 1)
is_gb = int(n / 1024**3 >= 1)
is_tb = int(n / 1024**4 >= 1)
array = {}
array[1, 0, 0, 0, 0] = (0, 'B')
array[1, 1, 0, 0, 0] = (1, 'KB')
array[1, 1, 1, 0, 0] = (2, 'MB')
array[1, 1, 1, 1, 0] = (3, 'GB')
array[1, 1, 1, 1, 1] = (4, 'TB')
power, name = array[is_b, is_kb, is_mb, is_gb, is_tb]
print(n / 1024**power, name)
memory_size(100)
memory_size(1024)
memory_size(1048576)
memory_size(1073741824)
memory_size(1099511627776)
But, of course, likely meant that it must be solved in another way, for example using logarithms. 但是,当然,可能意味着它必须以另一种方式解决,例如使用对数。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.