python 3.x - Coverting strings to decimal with a radix -


i'm getting empire of code , i've come across problem don't understand @ all. eli5 this? i'm not sure start making function. problem:

you given positive number string along radix it. function should convert decimal form. radix less 37 , greater 1. task uses digits , letters a-z strings.

watch out cases when number cannot converted. example: "1a" cannot converted radix 9. these cases function should return -1.

input: 2 arguments. number string , radix integer.

output: converted number integer.

example:

convert("af", 16) == 175 convert("101", 2) == 5 convert("101", 5) == 26 convert("z", 36) == 35 convert("ab", 10) == -1 

what asking? don't know enough number bases have slightest grip on this.

our number system base 10. there 10 possible digits can use represent numbers, 0-9. once 9, need digit represent larger number (10).

other systems are, of course, possible. famous ones:

  • base 2 ("binary"): digits 0 , 1. once 2 need more digits (10).
  • base 8 ("octal"): digits 0-7. once 8 need more digits (10).

there bases higher 10. because don't have numbers these pull in letters substitute. common 1 here base 16 (aka "hexadecimal"), uses 0-9 followed a-f, a=10, b=11, , on out f=15. once 16, need more digits (10).

when "base n", "n" there radix (technical term mathematics). need radix cases there ambiguity. example, showed 4 different numbers '10' can represent, depending on base you're working in. (in fact, may have noticed if follow normal conventions have here, '10' represents number corresponding base you're working in: 2 in binary, 8 in octal, 16 in hex, etc.)

you can make inferences based on having many digits. example, "2" not valid number in base 2 (binary), because valid digits 0 , 1. likewise, "1a" cannot represented in base 9 because valid digits there 0-8. "a" doesn't become necessary until hit @ least base 11.

hope helps.


Comments

Popular posts from this blog

gridview - Yii2 DataPorivider $totalSum for a column -

java - Suppress Jboss version details from HTTP error response -

Sass watch command compiles .scss files before full sftp upload -