November 9th, 2012, 12:16 PM

Python problem; Read_numbers function
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".
>>>read_number("123.45",10)
123.45
>>>read_number("3e8.6",16)
1000.375
>>>read_number("10q",30)
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...
November 9th, 2012, 02:29 PM

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 UTF8), 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
November 9th, 2012, 02:58 PM

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.)
[code]
Code tags[/code] are essential for python code and Makefiles!
November 9th, 2012, 06:08 PM

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 widerange 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 subprograms and functions and this is the last one to do.
Thanks in advance guys, much appreciated :]]
November 9th, 2012, 07:27 PM

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.
[code]
Code tags[/code] are essential for python code and Makefiles!
November 12th, 2012, 01:53 PM

I can't solve it...
Can someone post a full code of how this could be done?
November 12th, 2012, 02:14 PM

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!
November 12th, 2012, 02:33 PM

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:
'''
>>> 123.45 == read_number("123.45",10)
True
>>> 1000.375 == read_number("3e8.6",16)
True
>>> 926 == read_number("10q",30)
True
'''
j = r'/usr/local/j64701/bin/jconsole'
import subprocess
def read_number(string,base):
command = r'''%s js 'echo 18j8 ": %fb%s' "exit 0"'''%(j,base,string)
r = float(subprocess.check_output(command, shell=True))
return r
[code]
Code tags[/code] are essential for python code and Makefiles!
November 12th, 2012, 02:35 PM

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.
[code]
Code tags[/code] are essential for python code and Makefiles!
November 12th, 2012, 02:54 PM

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! :]]