7

I'm trying to implement add2strings, sub2strings, mult2strings functions in Python. They're all very easy if you just do int(string), but I want to do them without that and without importing another cheating thing like Decimal. My current idea is to use bytes.

Is there another way to do this?

8
  • 4
    I am very curious to know why you want to do this. Commented Jul 4, 2014 at 2:44
  • 2
    You could split string to get chars and then you can compare char with "0" to get digit 0, etc. Commented Jul 4, 2014 at 2:47
  • Coding practice. And I'm liking Python a lot more than C/C++ ATM. Commented Jul 4, 2014 at 2:47
  • @furas ah interesting idea! I suppose that would work. Commented Jul 4, 2014 at 2:49
  • 1
    This would be much better on CodeGolf.StackExchange.com Commented Jul 4, 2014 at 3:31

4 Answers 4

14

Refer to a basic atoi in C:

int myAtoi(char *str)
{
    int res = 0; // Initialize result

    // Iterate through all characters of input string and update result
    for (int i = 0; str[i] != '\0'; ++i)
        res = res*10 + str[i] - '0';

    // return result.
    return res;
}

Which translates into the Python:

def atoi(s):
    rtr=0
    for c in s:
        rtr=rtr*10 + ord(c) - ord('0')

    return rtr

Test it:

>>> atoi('123456789')
123456789   

If you want to accommodate an optional sign and whitespace the way that int does:

def atoi(s):
    rtr, sign=0, 1
    s=s.strip()
    if s[0] in '+-':
        sc, s=s[0], s[1:]
        if sc=='-':
            sign=-1

    for c in s:
        rtr=rtr*10 + ord(c) - ord('0')

    return sign*rtr

Now add exceptions and you are there!

Sign up to request clarification or add additional context in comments.

2 Comments

Nice idea, didn't realize I can just generalize the problems by making an atoi, doing arithmetic and then using an itoa. I was thinking hardware and planning on using carry bits.
Of course you would need to modify to handle whitespace and an optional + or - and the start of the string, but both are trivial to do.
2

This is really inefficient but:

>>> zero = ord("0")
>>> s = "1234"
>>> sum([x * 10**i for i, x in enumerate(map(lambda x: x - zero, map(ord, s))[::-1])])
1234

This is slightly better:

>>>> sum([x * 10**i for i, x in enumerate([ord(x) - zero for x in s[::-1]])])
1234

>>> atoi = lambda s: sum([x * 10**i for i, x in enumerate([ord(x) - zero for x in s[::-1]])])
>>> atoi("1234")
1234

Comments

2

My solution:

def str_to_int(input_str):
    if input_str[0] == "-":
        is_negative = True
        input_str = input_str[1:]
    else:
        is_negative = False

    output_int = 0

    for i in input_str:
        output_int = output_int * 10 + ord(i) - ord("0")

    if is_negative:
        output_int *= -1

    return output_int
print(str_to_int("123"))
print(str_to_int("-123"))

output:

123
-123

Comments

1

What about just iterating through all the integers, converting them to strings and comparing strings?

import exceptions
MAX_INT  = 1000
MIN_INT = -1000

def str2int(s):
  for i in range(MIN_INT,MAX_INT):
    if s == str(i):
      return i
  raise exceptions.OverflowError

def add2strings(s,t):
  return str(str2int(s)+str2int(t))

print add2strings("170","-300")
print add2strings("170","-1001")

This gives:

"-170"
Traceback (most recent call last):
  Line 15, in <module>
    print add2strings("170","-1001")
  Line 12, in add2strings
    return str(str2int(s)+str2int(t))
  Line 9, in str2int
    raise exceptions.OverflowError
OverflowError

2 Comments

Go home, you're drunk.
You can't tell me when to code and hic when not to code. hic Thats why is free maaaaan... free as in beer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.