Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #87489

Re: More or less code in python?

Path csiph.com!usenet.pasdenom.info!news.redatomik.org!newsfeed.xs4all.nl!newsfeed2a.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <python-python-list@m.gmane.org>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.001
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'subject:code': 0.07; '"__main__":': 0.09; '__name__': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'python': 0.11; 'def': 0.12; 'itertools': 0.16; 'left,': 0.16; 'map(int,': 0.16; 'received:80.91.229.3': 0.16; 'received:dip0.t-ipconnect.de': 0.16; 'received:plane.gmane.org': 0.16; 'received:t-ipconnect.de': 0.16; 'subject:More': 0.16; 'subject:python': 0.16; 'wrote:': 0.18; 'import': 0.22; 'header:User-Agent:1': 0.23; 'skip:{ 20': 0.24; 'header:X-Complaints-To:1': 0.27; 'function': 0.29; 'rest': 0.29; 'code': 0.31; 'ball': 0.31; 'indentation': 0.31; 'languages': 0.32; 'possible': 0.36; 'subject:?': 0.36; 'two': 0.37; 'to:addr :python-list': 0.38; 'expect': 0.39; 'sure': 0.39; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'skip:z 20': 0.60; 'length': 0.61; 'email addr:gmail.com': 0.63; 'skip:n 10': 0.64; 'carry,': 0.84
X-Injected-Via-Gmane http://gmane.org/
To python-list@python.org
From Peter Otten <__peter__@web.de>
Subject Re: More or less code in python?
Date Sun, 15 Mar 2015 20:15:22 +0100
Organization None
References <8412cc81-fbd2-4590-9db3-12c65ba2ee35@googlegroups.com>
Mime-Version 1.0
Content-Type text/plain; charset="ISO-8859-1"
Content-Transfer-Encoding 7Bit
X-Gmane-NNTP-Posting-Host p57bd8834.dip0.t-ipconnect.de
User-Agent KNode/4.13.3
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.19
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.400.1426446930.21433.python-list@python.org> (permalink)
Lines 48
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1426446930 news.xs4all.nl 2885 [2001:888:2000:d::a6]:44072
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:87489

Show key headers only | View raw


jonas.thornvall@gmail.com wrote:

> <SCRIPT LANGUAGE="Javascript">
> 
> function naiveAdd(base,arrOne,arrTwo) {
> if (arrOne.length>=arrTwo.length) {length=arrOne.length;} else
> {length=arrTwo.length;} out="";
> remainder=0;
> for (i=0;i<length;i++){
> one=arrOne[i];
> two=arrTwo[i];
> one=parseInt(one);
> two=parseInt(two);
> if (isNaN(one)) one = 0;
> if (isNaN(two)) two = 0;
> sum=one+two+remainder;
> 
> if (sum>=base) { sum=sum-base; remainder=1;} else {remainder=0;}
> out=","+sum+out;
> }
> if (remainder==1) out=remainder+out;
> return out;
> }

As to length I expect both languages to end in the same ball park. Here's a 
possible Python implementation:

from itertools import zip_longest

def prepare(s):
    return map(int, reversed(s.split(",")))

def naive_add(base, left, right):
    result = []
    carry = 0
    for a, b in zip_longest(prepare(left), prepare(right), fillvalue=0):
        carry, rest = divmod(a + b + carry, base)
        result.append(rest)
    if carry:
        result.append(carry)
    return ",".join(map(str, reversed(result)))

if __name__ == "__main__":
    print(naive_add(16, "1,2", "13,14"))

The Python code for sure has better indentation ;)

Back to comp.lang.python | Previous | NextPrevious in thread | Find similar | Unroll thread


Thread

More or less code in python? jonas.thornvall@gmail.com - 2015-03-15 11:07 -0700
  Re: More or less code in python? Joel Goldstick <joel.goldstick@gmail.com> - 2015-03-15 14:31 -0400
    Re: More or less code in python? jonas.thornvall@gmail.com - 2015-03-15 11:56 -0700
    Re: More or less code in python? jonas.thornvall@gmail.com - 2015-03-15 12:00 -0700
      Re: More or less code in python? Paul Rubin <no.email@nospam.invalid> - 2015-03-15 12:01 -0700
        Re: More or less code in python? jonas.thornvall@gmail.com - 2015-03-15 12:03 -0700
        Re: More or less code in python? jonas.thornvall@gmail.com - 2015-03-15 12:09 -0700
          Re: More or less code in python? Dave Angel <davea@davea.name> - 2015-03-16 03:32 -0400
          Re: More or less code in python? Chris Angelico <rosuav@gmail.com> - 2015-03-16 18:36 +1100
          Re: More or less code in python? Dave Angel <davea@davea.name> - 2015-03-16 03:44 -0400
  Re: More or less code in python? Peter Otten <__peter__@web.de> - 2015-03-15 20:15 +0100

csiph-web