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

    Join Date
    Nov 2012
    Posts
    32
    Rep Power
    2

    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...
  2. #2
  3. Commie Mutant Traitor
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Jun 2004
    Location
    Alpharetta, GA
    Posts
    1,806
    Rep Power
    1570
    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 Short Understanding the C/C++ Preprocessor
    Taming Python A 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
  4. #3
  5. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,841
    Rep Power
    480
    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!
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    32
    Rep 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 :]]
  8. #5
  9. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,841
    Rep Power
    480
    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!
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

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

    Join Date
    Nov 2012
    Posts
    32
    Rep 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!
  14. #8
  15. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,841
    Rep Power
    480
    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/j64-701/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!
  16. #9
  17. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,841
    Rep Power
    480
    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!
  18. #10
  19. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    32
    Rep Power
    2
    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! :]]

IMN logo majestic logo threadwatch logo seochat tools logo