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

I'm just getting into Empire of Code and I've come across a problem that I don't understand at all. Could someone do an ELI5 for this? I'm not sure even where to start with making the function. This is the problem:
You are given a positive number as a string along with the radix for it. Your function should convert it into decimal form. The radix is less than 37 and greater than 1. The task uses digits and the letters A-Z for the strings.
Watch out for cases when the number cannot be converted. For example: "1A" cannot be converted with radix 9. For these cases your function should return -1.
Input: Two arguments. A number as string and a radix as an integer.
Output: The converted number as an integer.
Example:
convert("AF", 16) == 175
convert("101", 2) == 5
convert("101", 5) == 26
convert("Z", 36) == 35
convert("AB", 10) == -1
What exactly is it asking? I don't know enough about number bases to have even the slightest grip on this.

Our number system is base 10. There are 10 possible digits you can use to represent numbers, 0-9. Once you get to 9, you need an extra digit to represent a larger number (10).
Other systems are, of course, possible. Some famous ones:
Base 2 ("binary"): Only digits are 0 and 1. Once you get to 2 you need more digits (10).
Base 8 ("octal"): Only digits are 0-7. Once you get to 8 you need more digits (10).
There are also bases higher than 10. Because we don't have numbers for these normally we pull in letters to substitute. The most common one here is base 16 (aka "hexadecimal"), which uses 0-9 followed by A-F, where A=10, B=11, and so on out to F=15. Once you get to 16, you need more digits (10).
When we say "Base N", the "N" there is the radix (technical term from mathematics). You need the radix for cases where there is ambiguity. For example, I just showed you four different numbers that '10' can represent, depending on the base you're working in. (In fact, you may have noticed that if you follow the normal conventions as I have here, '10' always represents the number corresponding to the base you're working in: 2 in binary, 8 in octal, 16 in hex, etc.)
You can also make some inferences based on having too many digits. For example, "2" is not a valid number in base 2 (binary), because the only valid digits are 0 and 1. Likewise, "1A" cannot be represented in base 9 because necessarily the only valid digits there would be 0-8. The "A" doesn't become necessary until you hit at least base 11.
Hope that helps.

You can use the int() to solve this too. Looks like python has the in-built to convert str to base.
Convert hex string to int in Python

Related

Check if digits in a number can be rearranged to form a fibonacci number

This question is asked in a programming contest. I couldn't find any way other than generating all permutations. But the number of digits is upto 15 and no of permutations (15!) is very large. Is there any other way?
I know that if (5*N^2 + 4) or (5*N^2 - 4) is a perfect square, n is fibonacci.
You don't need to generate all permutations. Generate the fibonacci numbers of desired length (which will be less than 74 numbers, because 73rd fib. number is the highest with 15 digits) and then just check for those few whether they can be "constructed" from digits in the given number.

Input decimal values like 0.0047 in verilog

I have an array of decimal values like 0.0047, -45.34 etc. Is there a way I can add this in verilog and automatically view it's 16 bit converted value?
You can use 'real' but you can not synthesize it. You have to find a binary representation for your numbers either floating point or fixed point. You have to define a range for your numbers and also a precision as binary representation of real number is often an approximation.
I did some calculations. You have a positive and negative number so you need a sign bit. Leaves 15 bits for the values. You want to have at least 45, that requires 6 bits. leaves 9 bits for the fraction. The closest you can get to 0.0047 is then 0.0046875. Your range is then -63.998 .... +63.998

Convert decimal to whole number - but ignoring value

I know how to isolate the decimal using the TRUNC() function, as well as taking the original value and subtracting this truncated part.
And I can then multiply to get whole number.
But that only works if all my decimals are the same place. I want something that will get me the amount after the decimal point as a whole number, regardless of how many places.
eg: 12.2 would return 2
12.21 would return 21
With data in A1, consider:
=IF(ISERROR(FIND(".",A1)),A1,--MID(A1,(FIND(".",A1)+1),9999))
Naturally leading zeros in the output are dropped:

bin2dec for numbers longer than 10 bits in excel

I have an excel with 28 position binary numbers. I need to convert them to decimal numbers, but function bin2dec don't work with numbers longer than 10 bits. Can anyone help me with this?
Use the following formula to mimic a BIN2DEC function that coverts larger than 10 bits.
=SUMPRODUCT(--MID(A2,LEN(A2)+1-ROW(INDIRECT("1:"&LEN(A2))),1),(2^(ROW(INDIRECT("1:"&LEN(A2)))-1)))
Remember that Excel has a numerical precision of 15 digits. If you want 28 digits, format the cell as Text or preface the string of digits with a single tick (e.g. ') as a PrefixCharacter property.
   
I brute forced it with the math, may be inelegant, but for a 16 bit number where leading 0's will be displayed this works and can easily be adapted to longer strings
This works well if you are working with fixed length words, like verifying values in memory, BIT registers, etc.
16 bit
=BIN2DEC(LEFT(R5,8))*2^8+BIN2DEC(RIGHT(R5,8))
32 bit could be
=BIN2DEC(MID(R10,1,8))*2^24+BIN2DEC(MID(R10,9,8))*2^16+BIN2DEC(MID(R10,17,8))*2^8+BIN2DEC(MID(R10,25,8))
Again, this works if you have a fixed length input, leading zeros are displayed.

associativity of operations regarding floating points

I am trying to understand tthe associativity of operations when it comes to floating points.
In the lecture notes i have, the following is stated:
"suppose floating-point values store seven digit of accuracy.
Considee the problem of adding 11 numbers together, where one of the numbers is 10^7 and the other ten are 1.
If the small numbers (the 1s) are each added to the large number, one at a time, there is no effect on that number, because the small numbers occur in the eighth digit of the large number ". So here I understood that the result is 1,000,001.
"however, if the small numbers are first added together and the result is added to the large number, the result is a seven-digit accurancy 1.000001 * 10^7"
But both approaches seemed the same to me: we are adding the 10 numbers to the larger number.
Can someone please clarify this problem ?
Thank you
Let's go over the first method first. When the small numbers are added one by one to the large number, the following will happen ten times:
10,000,000 + 1 = 10,000,001
However since the floating-point values store only seven digits of accuracy this last digit, the eight digit, will be rounded in the seventh digit to zero. This operation will happen 10 times, and so the value will remain 10,000,000.
Next let's go over the second method. The 10 number 1's are added together first and so this will sum up to 10. When this is added to 10^7 the following will happen:
10,000,000 + 10 = 10,000,010
Since the floating-point values store seven digits of accuracy this number will remain!

Resources