Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #87481 > unrolled thread
| Started by | jonas.thornvall@gmail.com |
|---|---|
| First post | 2015-03-15 11:07 -0700 |
| Last post | 2015-03-15 20:15 +0100 |
| Articles | 11 — 6 participants |
Back to article view | Back to comp.lang.python
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
| From | jonas.thornvall@gmail.com |
|---|---|
| Date | 2015-03-15 11:07 -0700 |
| Subject | More or less code in python? |
| Message-ID | <8412cc81-fbd2-4590-9db3-12c65ba2ee35@googlegroups.com> |
<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;
}
base=16;
strOne="2,2";
strTwo="13,13";
arrOne=strOne.split(",");
arrTwo=strTwo.split(",");
arrOne.reverse();
arrTwo.reverse();
naiveAdd(base,arrOne,arrTwo);
document.write("Sum = ",out,"<BR>");
</SCRIPT>
[toc] | [next] | [standalone]
| From | Joel Goldstick <joel.goldstick@gmail.com> |
|---|---|
| Date | 2015-03-15 14:31 -0400 |
| Message-ID | <mailman.399.1426444300.21433.python-list@python.org> |
| In reply to | #87481 |
On Sun, Mar 15, 2015 at 2:07 PM, <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;
> }
> base=16;
> strOne="2,2";
> strTwo="13,13";
> arrOne=strOne.split(",");
> arrTwo=strTwo.split(",");
> arrOne.reverse();
> arrTwo.reverse();
> naiveAdd(base,arrOne,arrTwo);
> document.write("Sum = ",out,"<BR>");
> </SCRIPT>
> --
> https://mail.python.org/mailman/listinfo/python-list
I'm guessing less, if your question is whether this code would be
fewer lines in python. However, it would be nice if you formatted it
so that it is readable, and gave a description of what you think the
code is doing, or should be doing. Its an odd question in that python
generally runs on a server and javascript in a browser
--
Joel Goldstick
http://joelgoldstick.com
[toc] | [prev] | [next] | [standalone]
| From | jonas.thornvall@gmail.com |
|---|---|
| Date | 2015-03-15 11:56 -0700 |
| Message-ID | <1a02b56e-cce3-4485-9b13-bd801a65bd07@googlegroups.com> |
| In reply to | #87482 |
Den söndag 15 mars 2015 kl. 19:32:02 UTC+1 skrev Joel Goldstick:
> On Sun, Mar 15, 2015 at 2:07 PM, <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;
> > }
> > base=16;
> > strOne="2,2";
> > strTwo="13,13";
> > arrOne=strOne.split(",");
> > arrTwo=strTwo.split(",");
> > arrOne.reverse();
> > arrTwo.reverse();
> > naiveAdd(base,arrOne,arrTwo);
> > document.write("Sum = ",out,"<BR>");
> > </SCRIPT>
> > --
> > https://mail.python.org/mailman/listinfo/python-list
>
> I'm guessing less, if your question is whether this code would be
> fewer lines in python. However, it would be nice if you formatted it
> so that it is readable, and gave a description of what you think the
> code is doing, or should be doing. Its an odd question in that python
> generally runs on a server and javascript in a browser
>
> --
> Joel Goldstick
> http://joelgoldstick.com
It add two postive numbers working in any base you prefer. There is no restriction on the size of operands, it dependens upon the memory of your computer or string limitations in the implementation of ECMASCRIPT.(If there is).
Basicly the function doing addition in Python shell, but you can chose base you work in.
[toc] | [prev] | [next] | [standalone]
| From | jonas.thornvall@gmail.com |
|---|---|
| Date | 2015-03-15 12:00 -0700 |
| Message-ID | <53b202e7-327b-412a-a5e0-debca5c3b489@googlegroups.com> |
| In reply to | #87482 |
Den söndag 15 mars 2015 kl. 19:32:02 UTC+1 skrev Joel Goldstick:
> On Sun, Mar 15, 2015 at 2:07 PM, <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;
> > }
> > base=16;
> > strOne="2,2";
> > strTwo="13,13";
> > arrOne=strOne.split(",");
> > arrTwo=strTwo.split(",");
> > arrOne.reverse();
> > arrTwo.reverse();
> > naiveAdd(base,arrOne,arrTwo);
> > document.write("Sum = ",out,"<BR>");
> > </SCRIPT>
> > --
> > https://mail.python.org/mailman/listinfo/python-list
>
> I'm guessing less, if your question is whether this code would be
> fewer lines in python. However, it would be nice if you formatted it
> so that it is readable, and gave a description of what you think the
> code is doing, or should be doing. Its an odd question in that python
> generally runs on a server and javascript in a browser
>
> --
> Joel Goldstick
> http://joelgoldstick.com
I though it would be interesting doing comparissons in timing adding massive digits in different bases. Especially in Python.
[toc] | [prev] | [next] | [standalone]
| From | Paul Rubin <no.email@nospam.invalid> |
|---|---|
| Date | 2015-03-15 12:01 -0700 |
| Message-ID | <87h9tmt796.fsf@jester.gateway.sonic.net> |
| In reply to | #87484 |
jonas.thornvall@gmail.com writes: > I though it would be interesting doing comparissons in timing adding > massive digits in different bases. Especially in Python. Python has built-in bignums. Try "print 2**500".
[toc] | [prev] | [next] | [standalone]
| From | jonas.thornvall@gmail.com |
|---|---|
| Date | 2015-03-15 12:03 -0700 |
| Message-ID | <fc6ff381-b091-40bc-87e4-48032b1c14f6@googlegroups.com> |
| In reply to | #87485 |
Den söndag 15 mars 2015 kl. 20:01:36 UTC+1 skrev Paul Rubin: > jonas.thornvall@gmail.com writes: > > I though it would be interesting doing comparissons in timing adding > > massive digits in different bases. Especially in Python. > > Python has built-in bignums. Try "print 2**500". I know.
[toc] | [prev] | [next] | [standalone]
| From | jonas.thornvall@gmail.com |
|---|---|
| Date | 2015-03-15 12:09 -0700 |
| Message-ID | <dbdd7715-7418-49d1-9c52-e7cfdf16d733@googlegroups.com> |
| In reply to | #87485 |
Den söndag 15 mars 2015 kl. 20:01:36 UTC+1 skrev Paul Rubin: > jonas.thornvall@gmail.com writes: > > I though it would be interesting doing comparissons in timing adding > > massive digits in different bases. Especially in Python. > > Python has built-in bignums. Try "print 2**500". I will try implement the common operators + - * / pow sqrt operators mod floor and and a generic parser taking arguments in any base.
[toc] | [prev] | [next] | [standalone]
| From | Dave Angel <davea@davea.name> |
|---|---|
| Date | 2015-03-16 03:32 -0400 |
| Message-ID | <mailman.418.1426491168.21433.python-list@python.org> |
| In reply to | #87488 |
On 03/15/2015 03:09 PM, jonas.thornvall@gmail.com wrote: > Den söndag 15 mars 2015 kl. 20:01:36 UTC+1 skrev Paul Rubin: >> jonas.thornvall@gmail.com writes: >>> I though it would be interesting doing comparissons in timing adding >>> massive digits in different bases. Especially in Python. >> >> Python has built-in bignums. Try "print 2**500". > > I will try implement the common operators + - * / pow sqrt operators mod floor and and a generic parser taking arguments in any base. > Why on earth would you bother? Use the int(x, base) to convert a string in an arbitrary base to an int. Write a similar function to convert an int back into a string of the desired base. Of course, your strings aren't in a standard form, so you'd have to do some work to get those instead. Are you really intending to use a base beyond 36? Then to add two numbers, use + To multiply, use *, and so on. Assuming Python 3.x of course. if you're in Python 2, you'd use "long" rather than int. -- DaveA
[toc] | [prev] | [next] | [standalone]
| From | Chris Angelico <rosuav@gmail.com> |
|---|---|
| Date | 2015-03-16 18:36 +1100 |
| Message-ID | <mailman.419.1426491416.21433.python-list@python.org> |
| In reply to | #87488 |
On Mon, Mar 16, 2015 at 6:32 PM, Dave Angel <davea@davea.name> wrote: > Assuming Python 3.x of course. if you're in Python 2, you'd use "long" > rather than int. Not sure you need to bother. Even in Py2, you can use the int constructor to get a long. ChrisA
[toc] | [prev] | [next] | [standalone]
| From | Dave Angel <davea@davea.name> |
|---|---|
| Date | 2015-03-16 03:44 -0400 |
| Message-ID | <mailman.421.1426491913.21433.python-list@python.org> |
| In reply to | #87488 |
On 03/16/2015 03:36 AM, Chris Angelico wrote: > On Mon, Mar 16, 2015 at 6:32 PM, Dave Angel <davea@davea.name> wrote: >> Assuming Python 3.x of course. if you're in Python 2, you'd use "long" >> rather than int. > > Not sure you need to bother. Even in Py2, you can use the int > constructor to get a long. > Actually I was talking about the terminology. In Python 2.x, you'd be talking about longs to make it clear that they were (essentially) unlimited in size. For the code itself, I doubt if you'd want to make any distinction at all, as you say. -- DaveA
[toc] | [prev] | [next] | [standalone]
| From | Peter Otten <__peter__@web.de> |
|---|---|
| Date | 2015-03-15 20:15 +0100 |
| Message-ID | <mailman.400.1426446930.21433.python-list@python.org> |
| In reply to | #87481 |
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 ;)
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.python
csiph-web