Forums: » Register « |  Free Tools |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support |

New Free Tools on Dev Shed!

#1
November 9th, 2012, 12:16 PM
 Nightmareix35
Contributing User

Join Date: Nov 2012
Posts: 32
Time spent in forums: 8 h 42 m 41 sec
Reputation Power: 2

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
November 9th, 2012, 02:29 PM
 Schol-R-LEA
Commie Mutant Traitor

Join Date: Jun 2004
Location: Norcross, GA (again)
Posts: 1,798
Time spent in forums: 1 Month 2 Weeks 3 Days 1 h 39 m 19 sec
Reputation Power: 1569
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`
__________________
Rev First Speaker Schol-R-LEA;2 JAM LCF ELF KoR KCO BiWM TGIF
#define KINSEY (rand() % 7) λ Scheme is the Red Pill
Scheme in ShortUnderstanding the C/C++ Preprocessor
Taming PythonA Highly Opinionated Review of Programming Languages for the Novice, v1.1

FOR SALE: One ShapeSystem 2300 CMD, extensively modified for human use. Includes s/w for anthro, transgender, sex-appeal enhance, & Gillian Anderson and Jason D. Poit clone forms. Some wear. \$4500 obo. tverres@et.ins.gov

#3
November 9th, 2012, 02:58 PM
 b49P23TIvg
Contributing User

Join Date: Aug 2011
Posts: 4,132
Time spent in forums: 1 Month 3 Weeks 2 Days 5 h 47 m 38 sec
Reputation Power: 455
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!

#4
November 9th, 2012, 06:08 PM
 Nightmareix35
Contributing User

Join Date: Nov 2012
Posts: 32
Time spent in forums: 8 h 42 m 41 sec
Reputation Power: 2
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
November 9th, 2012, 07:27 PM
 b49P23TIvg
Contributing User

Join Date: Aug 2011
Posts: 4,132
Time spent in forums: 1 Month 3 Weeks 2 Days 5 h 47 m 38 sec
Reputation Power: 455
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
November 12th, 2012, 01:53 PM
 Nightmareix35
Contributing User

Join Date: Nov 2012
Posts: 32
Time spent in forums: 8 h 42 m 41 sec
Reputation Power: 2
I can't solve it...
Can someone post a full code of how this could be done?

#7
November 12th, 2012, 02:14 PM
 Nightmareix35
Contributing User

Join Date: Nov 2012
Posts: 32
Time spent in forums: 8 h 42 m 41 sec
Reputation Power: 2
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
November 12th, 2012, 02:33 PM
 b49P23TIvg
Contributing User

Join Date: Aug 2011
Posts: 4,132
Time spent in forums: 1 Month 3 Weeks 2 Days 5 h 47 m 38 sec
Reputation Power: 455
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
November 12th, 2012, 02:35 PM
 b49P23TIvg
Contributing User

Join Date: Aug 2011
Posts: 4,132
Time spent in forums: 1 Month 3 Weeks 2 Days 5 h 47 m 38 sec
Reputation Power: 455
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
November 12th, 2012, 02:54 PM
 Nightmareix35
Contributing User

Join Date: Nov 2012
Posts: 32
Time spent in forums: 8 h 42 m 41 sec
Reputation Power: 2
Quote:
 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! :]]

 Viewing: Dev Shed Forums > Programming Languages > Python Programming > Python problem; Read_numbers function