If / else alternative

I am new to programming but I am doing a task for a job. I wrote the function so that the number of bytes the user entered is translated into a "human readable" format. Basically to return to the corresponding B, KB, MB, GB, TB.

However, the assignment indicates that we should try to write code without loops or if statements, but we can use an array.

I was wondering how I can do this ...

Here is 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"

      

+3


source to share


4 answers


Since this is homework, I will not post the whole code, I will just say that it can move in multiple lines.

take a look at the library math

specifically at pow

andlog

You can determine that you are using the following ranges:

>>> ranges = ['B', 'KB', 'MB', 'GB', 'TB']

      



Then

  • log

    from n

    and truncate the decimal.
  • Calculate size using math.pow

    and value from # 1
  • Print the size and search the array ranges

    using the number from # 1 to get the value "b, kb, mb, etc.".

Thus, you will not use any operators if/else

or for/while

.

+1


source


Without providing any code, since this is homework, the basic approach is that you store the data of the range (a heap might be a good solution) and then go through it to find an entry for the given range and fill in the rest of the data you need. Then you only have one version of the code:

nKB = n / 1024
print nKB,"KB"

      

With extracted data instead of hardcoded constants.



Update:

As DavidRobinson points out, you cannot use loops and any search method uses a loop implicitly or explicitly. So the alternative is to take your input and transform it into a form that can be used for a single persistent search over your chosen data structure. An array of logarithms is the way to go here and match data again replacing the constants in the snippet above.

0


source


A couple of thoughts; nor fulfill the requirements, but perhaps they will help your thinking

Method 1. Create a dictionary from lower / upper bounds to divisors and display the strings. Step through the dictionary to find the matching entry, then use the values.

Method 2: Divide the input by 1024 until it is less than 1024. Use number of operations to find the display string.

0


source


Just for fun. You can use the results of division by 1024 as dictionary keys. The dictionary can store the resulting data. Here's some python 3 code that solves the problem without loops or if statements:

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 it probably meant that it needed to be solved differently, for example using logarithms.

0


source







All Articles