What Python looks like
Mensanator
mensanator at aol.com
Mon Aug 4 16:18:11 EDT 2008
On Aug 4, 2:06 pm, iu2 <isra... at elbit.co.il> wrote:
> Hi,
>
> This is a little bit strange post, but I'm curious...
>
> I learned Python from its tutorial step by step, and practicing
> writing small scripts.
> I haven't seen a Python program before knowing Python.
>
> I'm curious, what did Python code look like to those of you who have
> seen a bunch of Python code for the first time before knowing Python?
To me, when I started transitioning from perl to Python, Python
Python from 2002
--------------------------------------------------------------
import sys
import string
# Python Collatz tester
j = string.atol(sys.argv[1])
i = 2**(6*j-1)-1
print i,"\n"
r1 = 0
r2 = 0
count = 0
while i>1:
z = divmod(i,2)
if z[1]==0:
i = z[0]
r1 = r1 + 1
if z[1]>0:
i = i*3 + 1
r2 = r2 + 1
print "[1]",r1
print "[2]",r2,"\n"
--------------------------------------------------------------
looked just like perl, but without the braces (which seemed a
lot more important than the $s).
Perl from 2002
--------------------------------------------------------------
use Math::BigInt ':constant';
$j = @ARGV[0];
$i = 2**(6*$j-1)-1;
print "$i\n";
$r1 = 0;
$r2 = 0;
while ($i>1) {
if ($i =~ /.*?[0,2,4,6,8]$/) {
$i = $i/2;
$r1++;
}
else {
$i = $i*3 + 1;
$r2++;
}
}
print "[1] ",$r1;
print " [2] ",$r2,"\n";
--------------------------------------------------------------
>
> (I can tell, for example, that seeing perl for the first time looked
> like C with many $$$,
I wouldn't say that, the $s are minor, big thing is the declarations
and pointers.
C from 2005 (not complete program)
--------------------------------------------------------------
long collatz (mpz_ptr r)
{
mpz_t result, twee, twoo, cee;
mpz_init (result);
mpz_init_set_ui (cee, 1);
mpz_init_set_ui (twee, 3);
mpz_init_set_ui (twoo, 2);
long rule1 = 0;
long rule2 = 0;
long f;
while (mpz_cmp (r, cee) > 0)
{
f = mpz_scan1 (r, 0);
if (f>0) /* even */
{
mpz_tdiv_q_2exp (result, r, f);
rule1 = rule1 + f;
}
else /* odd */
{
mpz_set (result, cee);
mpz_addmul (result, r, twee);
rule2++;
}
mpz_swap (r, result);
}
printf ("\nRule1: %8d Rule2: %8d\n\n", rule1, rule2);
return rule1 + rule2;
}
--------------------------------------------------------------
> I could see "if" and "for" and "while" but they
> were meaningless.
Maybe you looked at a crappy example.
> Or Lisp for the first time looked like many words,
> no operators,
Aren't some of the words operators? I never used used Lisp,
but I did dabble in Scheme and have no trouble identifying the
operators (although not the overall program).
Scheme from 2004
--------------------------------------------------------------
(define n 1)
(define collatz
(lambda (n)
(if (even? n)
(/ n 2)
(+ 1 (* n 3))
)))
(define sequence
(lambda (n)
(do ((count 0 (+ count 1)))
((= n 1) (display "stopping: ") (display count))
(set! n (collatz n)) (display n) (display " ")
)))
--------------------------------------------------------------
> how could that make a program???)
You have to think differently with functional languages.
The functional snobs say you'll never "get" it once your
mind has been poisoned by imperative languages.
>
> Thanks
More information about the Python-list
mailing list