1. No Profile Picture
Contributing User
Devshed Newbie (0 - 499 posts)

Join Date
Nov 2012
Posts
32
Rep Power
6

I have this one question I really don't know where to start...

Write a function in Python named read_number(s,b) which receives two inputs: a string "s" and an integer "b" in range(2,37).
The function must return as output the real positive number which representation in base "b" is "s".

123.45
1000.375
92

Notes: the use of the functions float and int isn't allowed. The decimal dot represents the fraction part of the number.

I've tried so many ways but I don't seem to get it right...
2. As a hint, I will tell you that it requires a stack - or a recursive function - to do this using the best known algorithm for it. The reason for this is that you want to multiply each digit's integer value by the base raised to the number of digits remaining in the string, and add that to the integer value of the rest of the string.

I would recommend working out the terminating conditions first, though.

Also, you will find that in ASCII (and hence in UTF-8), you can get the integer value of a digit by taking the encoding value (the 'code point') and subtracting 0x30.

Code:
`digit = ord(s[0]) - 0x30`
3. Please post one of your attempts, nightmarie, if you can't fix it with this handy information:

sum of digit*base**(-digit_count to right of decimal point)

noting that the first digit to the right of the decimal point gets a digit_count of 1, and the first and second digits to the left of the decimal point get respective counts of -1 and -2 .

(If I'm wrong then it's neither handy nor information.)
4. No Profile Picture
Contributing User
Devshed Newbie (0 - 499 posts)

Join Date
Nov 2012
Posts
32
Rep Power
6
Considering your replies I think I might have understood the requirement of this question the wrong way...

I am kind of new to programming and I don't have much wide-range knowledge about ASCII codes and such. At college we did do a similar task but int and float functions were allowed, and that's basically what puts me to blank.

I reviewed all of the given material but none seem to be relevant to solving this. I know about recursive functions though, how can it help? I would really appreciate some more help. I need to hand in a huge project of sub-programs and functions and this is the last one to do.

Thanks in advance guys, much appreciated :]]
5. if "ab.cd" is in base 16,
Code:
```digit base10_value
┌─┬──┐
│a│10│
├─┼──┤
│b│11│
├─┼──┤
│c│12│
├─┼──┤
│d│13│
└─┴──┘
value = 10*pow(16,1) + 11*pow(16,0) + 12*pow(16,-1) + 13*pow(16,-2)```
Yes, what I've written is not the most efficient way to compute the result. But it works.
6. No Profile Picture
Contributing User
Devshed Newbie (0 - 499 posts)

Join Date
Nov 2012
Posts
32
Rep Power
6
I can't solve it...
Can someone post a full code of how this could be done?
7. No Profile Picture
Contributing User
Devshed Newbie (0 - 499 posts)

Join Date
Nov 2012
Posts
32
Rep Power
6
In fact never mind!
I had a little bug that bugged me for a lot BUGGING hours!

I left the base variable set on a static 10 instead of using the dynamic value which kept giving me the same result over all kind of inputs!

It works! thanks for the help guys!
8. OK, I'll tell you. Install j. http://www.jsoftware.com
Figure out the command to invoke jconsole (note this is NOT a java console). Replace the assigned value of j in the script below, and you'll have a working version of read_number.
Code:
```'''
True
True
True
'''

j = r'/usr/local/j64-701/bin/jconsole'

import subprocess

command = r'''%s -js 'echo 18j8 ": %fb%s' "exit 0"'''%(j,base,string)
r = float(subprocess.check_output(command, shell=True))
return r```
9. By the way, if you had posted your code we wouldn't have felt so strongly that we were finishing your homework for you and might have been even more helpful.
10. No Profile Picture
Contributing User
Devshed Newbie (0 - 499 posts)

Join Date
Nov 2012
Posts
32
Rep Power
6
Originally Posted by b49P23TIvg
By the way, if you had posted your code we wouldn't have felt so strongly that we were finishing your homework for you and might have been even more helpful.

I know what you mean! :]] But I assure to you that I, in no way possible, was taking advantage of anyone. What's in it for me anyway!

In fact, i'm glad no one posted a full code. It is much more satisfying to do it on your own! My journey is still long forward and I still got much to learn (OH YEAH!)

so thank you! :]]