Hacking Perl Code

Membuat kode yang lebih singkat dari kode terdahulu. Have fun…

#! /usr/bin/perl
$m=4; $n=4;
for ($y=0; $y < $n ; $y++) { for ($x=0 ; $x < $m ; $x++) {
print $y*$m + $x . “:”;
if ($y!=0) { $neighbor = ($y-1) * $m + $x; print ” $neighbor “; } #N
if (!($x==($m-1))) { $neighbor = $y * $m + ($x + 1); print ” $neighbor “; } #E
if (!($y==($n-1))) { $neighbor = ($y+1) * $m + $x; print ” $neighbor “; } #S
if ($x!=0) { $neighbor = $y * $m + ($x – 1); print ” $neighbor “; } #W
print “\n”; } }

Iklan

21 pemikiran pada “Hacking Perl Code

  1. #! /usr/bin/perl

    $m=4;
    $n=4;

    for ($y=0; $y < $n ; $y++)
    {
    for ($x=0 ; $x < $m ; $x++)
    {
    print $y*$m + $x . “:”;
    if ($y!=0)
    {
    $neighbor = ($y-1) * $m + $x; print ” $neighbor “;
    } #N
    if (!($x==($m-1)))
    {
    $neighbor = $y * $m + ($x + 1); print ” $neighbor “;
    } #E
    if (!($y==($n-1)))
    {
    $neighbor = ($y+1) * $m + $x; print ” $neighbor “;
    } #S
    if ($x!=0)
    {
    $neighbor = $y * $m + ($x – 1); print ” $neighbor “;
    } #W
    print “\n”;
    }
    }

  2. Pak itu koding C ato perl? hehehe…
    Imo, ini lebih perl-ish dan kebaca (un-tested):


    my ($m, $n) = (4, 4);
    my $neighbor;

    for my $y (0..$n-1) {
    for my $x (0..$m-1) {
    $neighbor = ($y != 0) ? ($y-1) * $m + $x
    : ($x != $m-1) ? $y * $m + ($x + 1)
    : ($y != $n-1) ? ($y+1) * $m + $x
    : ($x != 0) ? $y * $m + ($x -1)
    : q{};

    print $neighbor . "\n";
    }
    }

  3. Pak itu koding C ato perl? hehehe…
    Imo, ini lebih perl-ish dan kebaca (un-tested):

    my ($m, $n) = (4, 4);
    my $neighbor;

    for my $y (0..$n-1) {
    for my $x (0..$m-1) {
    $neighbor = ($y != 0) ? ($y-1) * $m + $x
    : ($x != $m-1) ? $y * $m + ($x + 1)
    : ($y != $n-1) ? ($y+1) * $m + $x
    : ($x != 0) ? $y * $m + ($x -1)
    : q{};

    print $neighbor . “\n”;
    }
    }

  4. he he he… ok pak, saya coba revisi nih:


    #! /usr/bin/python

    m, n = 5, 7
    process = lambda x, y, z: z.append(y) if x else None

    for i in range(m*n):
    y, x, lst = i / m, i % n, []
    process(y != 0, (y-1) * m + x, lst)
    process((x!=(m-1)), y * m + (x + 1), lst)
    process((y!=(n-1)),(y+1) * m + x, lst)
    process((x != 0),y * m + (x - 1), lst)
    print "%d: %s" % (i, " ".join(str(key) for key in lst))

  5. Ikutan deh nyumbang pake Java:
    public static void main(String[] args) {
    for (int m = 4, n = 4, y = 0; y < n; y++) {
    for (int x = 0; x < m; x++) {
    System.out.print((y * m + x) + “:”);
    System.out.print((y != 0 ? ” ” + ((y – 1) * m + x) : “”));
    System.out.print((x != m – 1 ? ” ” + (y * m + (x + 1)) : “”));
    System.out.print((y != n – 1 ? ” ” + ((y + 1) * m + x) : “”));
    System.out.print((x != 0 ? ” ” + (y * m + (x – 1)) : “”) + “\n”);
    }
    }
    }
    }

  6. Saya rapiin kodenya supaya terbaca, seperti ini nih. Kayaknya paling gampang dibaca, namanya juga Perl.

    $m=4; $n=4;
    for $y (0..$n-1) {
    for $x (0..$m-1) {
    print $y*$m + $x . “:”;
    print ” “, ($y-1) * $m + $x, ” ” if ($y!=0);
    print ” “, $y * $m + ($x + 1), ” ” if (!($x==($m-1)));
    print ” “, ($y+1) * $m + $x, ” ” if (!($y==($n-1)));
    print ” “, $y * $m + ($x – 1), ” ” if ($x!=0);
    print “\n”;
    }
    }

Tinggalkan Balasan

Isikan data di bawah atau klik salah satu ikon untuk log in:

Logo WordPress.com

You are commenting using your WordPress.com account. Logout /  Ubah )

Foto Google+

You are commenting using your Google+ account. Logout /  Ubah )

Gambar Twitter

You are commenting using your Twitter account. Logout /  Ubah )

Foto Facebook

You are commenting using your Facebook account. Logout /  Ubah )

Connecting to %s