zsh-users
 help / color / mirror / code / Atom feed
* Looking for devious one or two liner solutions to a problem
@ 2007-10-10 23:37 Kenneth Lareau
  2007-10-11  2:45 ` Clint Adams
  2007-10-11  3:35 ` Bart Schaefer
  0 siblings, 2 replies; 8+ messages in thread
From: Kenneth Lareau @ 2007-10-10 23:37 UTC (permalink / raw)
  To: zsh-users

At my workplace, we have a given problem we give potential engineers to see how their
programming and thought process skills are; it's a relatively simple problem:

   Given a version string of the form a.b.c.d (e.g. 5.3.20.4) and an index of 0-3
   (representing the position of a given number in the version string, zero-based),
   write a function that will take the two as parameters and return a new version
   string which has the number in the index position given incremented by one and
   all successive numbers set to 0.  Examples:

        5.3.20.4, 0  ->  6.0.0.0
        8.14.0.5  1  ->  8.15.0.0
        3.9.42.6  3  ->  3.9.42.7

In Perl, I was able to come up with the very simple function below:

   sub bump_version {
       my ($vers, $ind) = @_;

       @vers = split /\./, $vers;
       $vers[$ind] += 1;
       for ($i = $ind + 1; $i < 4; $i++) {
           $vers[$i] = 0;
       }

       return join '.', @vers;
   }

I've come up with something very similar in zsh, but I'm extremely curious;
is there anyone out there who knows zsh well enough to create a highly compressed
version of this function that would fit on a single 80 character line (or perhaps
two such lines)?  While I've been using zsh for many years, I haven't delved deep
enough into it to be able to figure anything out, but if someone else could, I'd
be very appreciative to see it!

Ken

-- 
Ken Lareau
elessar@numenor.org


^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: Looking for devious one or two liner solutions to a problem
  2007-10-10 23:37 Looking for devious one or two liner solutions to a problem Kenneth Lareau
@ 2007-10-11  2:45 ` Clint Adams
  2007-10-11  3:01   ` Atom Smasher
  2007-10-11  3:35 ` Bart Schaefer
  1 sibling, 1 reply; 8+ messages in thread
From: Clint Adams @ 2007-10-11  2:45 UTC (permalink / raw)
  To: Kenneth Lareau; +Cc: zsh-users

On Wed, Oct 10, 2007 at 04:37:33PM -0700, Kenneth Lareau wrote:
> version of this function that would fit on a single 80 character line (or perhaps

I think this does it in 80 exactly.

f(){j=(${(s:.:)1});((j[$2+1]++));for ((i=$2+2;i<5;i++)) j[i]=0;print ${(j:.:)j}}


^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: Looking for devious one or two liner solutions to a problem
  2007-10-11  2:45 ` Clint Adams
@ 2007-10-11  3:01   ` Atom Smasher
  0 siblings, 0 replies; 8+ messages in thread
From: Atom Smasher @ 2007-10-11  3:01 UTC (permalink / raw)
  To: zsh-users

On Wed, 10 Oct 2007, Clint Adams wrote:

> f(){j=(${(s:.:)1});((j[$2+1]++));for ((i=$2+2;i<5;i++)) j[i]=0;print ${(j:.:)j}}
==============

looks like you've got the job ;)


-- 
         ...atom

  ________________________
  http://atom.smasher.org/
  762A 3B98 A3C3 96C9 C6B7 582A B88D 52E4 D9F5 7808
  -------------------------------------------------

 	"The more laws, the less justice."
 		-- Marcus Tullius Cicero De Officiis



^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: Looking for devious one or two liner solutions to a problem
  2007-10-10 23:37 Looking for devious one or two liner solutions to a problem Kenneth Lareau
  2007-10-11  2:45 ` Clint Adams
@ 2007-10-11  3:35 ` Bart Schaefer
  2007-10-11  3:42   ` Bart Schaefer
                     ` (2 more replies)
  1 sibling, 3 replies; 8+ messages in thread
From: Bart Schaefer @ 2007-10-11  3:35 UTC (permalink / raw)
  To: Kenneth Lareau, zsh-users

On Oct 10,  4:37pm, Kenneth Lareau wrote:
} Subject: Looking for devious one or two liner solutions to a problem
}
} In Perl, I was able to come up with the very simple function below:
} 
}    sub bump_version {
}        my ($vers, $ind) = @_;
} 
}        @vers = split /\./, $vers;
}        $vers[$ind] += 1;
}        for ($i = $ind + 1; $i < 4; $i++) {
}            $vers[$i] = 0;
}        }
} 
}        return join '.', @vers;
}    }

At the risk of turning this into a golf match ...

    sub bump_version {
      my @a = split(/\./, $_[0]);
      splice(@a, $_[1], 4, $a[$_[1]]+1, 0, 0, 0);
      join('.', @a[0..3]);
    }

In zsh you can do the same:

    bump_version() {
      local -a a; a=(${(s:.:)1})
      a[$2+1,4]=($[$a[$2+1]+1] 0 0 0)
      print ${(j:.:)${a[1,4]}}
    }

If you dispense with "local -a a;" that's 72 characters for the body,
and sticking f(){...} around it adds 5 for 77.

f(){a=(${(s:.:)1});a[$2+1,4]=($[$a[$2+1]+1] 0 0 0);print ${(j:.:)${a[1,4]}}}

If you assume "setopt ksharrays" you can cut it to 75:

f(){a=(${(s:.:)1});a[$2,3]=($[${a[$2]}+1] 0 0 0);print ${(j:.:)${a[0,3]}}}

And if you return the new version in $a (since you've already made it
non-local) and therefore don't bother printing it, 71 characters:

f(){a=(${(s:.:)1});a[$2,3]=($[${a[$2]}+1] 0 0 0);a=${(j:.:)${a[0,3]}}}


^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: Looking for devious one or two liner solutions to a problem
  2007-10-11  3:35 ` Bart Schaefer
@ 2007-10-11  3:42   ` Bart Schaefer
  2007-10-11  9:02     ` Peter Stephenson
  2007-10-11  4:03   ` Kenneth Lareau
  2007-10-11  6:13   ` Kenneth Lareau
  2 siblings, 1 reply; 8+ messages in thread
From: Bart Schaefer @ 2007-10-11  3:42 UTC (permalink / raw)
  To: Kenneth Lareau, zsh-users

On Oct 10,  8:35pm, Bart Schaefer wrote:
}
} And if you return the new version in $a (since you've already made it
} non-local) and therefore don't bother printing it, 71 characters:
} 
} f(){a=(${(s:.:)1});a[$2,3]=($[${a[$2]}+1] 0 0 0);a=${(j:.:)${a[0,3]}}}

Hmm, I may have just found a parser bug.  That doesn't work; you need
one more space or sem between the assignment and the closing brace:

f(){a=(${(s:.:)1});a[$2,3]=($[${a[$2]}+1] 0 0 0);a=${(j:.:)${a[0,3]}};}


Afterthought:  Of course the next interview question is to show them
a string like that and ask "What does this do?"


^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: Looking for devious one or two liner solutions to a problem
  2007-10-11  3:35 ` Bart Schaefer
  2007-10-11  3:42   ` Bart Schaefer
@ 2007-10-11  4:03   ` Kenneth Lareau
  2007-10-11  6:13   ` Kenneth Lareau
  2 siblings, 0 replies; 8+ messages in thread
From: Kenneth Lareau @ 2007-10-11  4:03 UTC (permalink / raw)
  To: zsh-users

Hmm, for some reason, both Clint and Bart's solutions are giving me the following:

   zsh: unknown file attribute

when I try to utilize the function.  I am running zsh 4.2.1 on Solaris 10 x86,
though I have to wonder if I have some option set that might be causing this?

Ken

-- 
Ken Lareau
elessar@numenor.org


^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: Looking for devious one or two liner solutions to a problem
  2007-10-11  3:35 ` Bart Schaefer
  2007-10-11  3:42   ` Bart Schaefer
  2007-10-11  4:03   ` Kenneth Lareau
@ 2007-10-11  6:13   ` Kenneth Lareau
  2 siblings, 0 replies; 8+ messages in thread
From: Kenneth Lareau @ 2007-10-11  6:13 UTC (permalink / raw)
  To: zsh-users

Okay, I take my last message back - I forgot that one doesn't use parentheses
when actually utilizing a function in zsh.  Thanks for the different solutions,
folks, they were greatly appreciated!

Ken

-- 
Ken Lareau
elessar@numenor.org


^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: Looking for devious one or two liner solutions to a problem
  2007-10-11  3:42   ` Bart Schaefer
@ 2007-10-11  9:02     ` Peter Stephenson
  0 siblings, 0 replies; 8+ messages in thread
From: Peter Stephenson @ 2007-10-11  9:02 UTC (permalink / raw)
  To: zsh-users

On Wed, 10 Oct 2007 20:42:56 -0700
Bart Schaefer <schaefer@brasslantern.com> wrote:
> On Oct 10,  8:35pm, Bart Schaefer wrote:
> } f(){a=(${(s:.:)1});a[$2,3]=($[${a[$2]}+1] 0 0 0);a=${(j:.:)${a[0,3]}}}
> 
> Hmm, I may have just found a parser bug.  That doesn't work; you need
> one more space or sem between the assignment and the closing brace:
> 
> f(){a=(${(s:.:)1});a[$2,3]=($[${a[$2]}+1] 0 0 0);a=${(j:.:)${a[0,3]}};}

I have a vague memory of that being deliberate: the closing brace must
start a separate word on the command line.  This is going back years,
however (I think to Zoltan's period in charage).  It's possible I'm
remembering some other change to do with braces.

-- 
Peter Stephenson <pws@csr.com>                  Software Engineer
CSR PLC, Churchill House, Cambridge Business Park, Cowley Road
Cambridge, CB4 0WZ, UK                          Tel: +44 (0)1223 692070


^ permalink raw reply	[flat|nested] 8+ messages in thread

end of thread, other threads:[~2007-10-11  9:04 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2007-10-10 23:37 Looking for devious one or two liner solutions to a problem Kenneth Lareau
2007-10-11  2:45 ` Clint Adams
2007-10-11  3:01   ` Atom Smasher
2007-10-11  3:35 ` Bart Schaefer
2007-10-11  3:42   ` Bart Schaefer
2007-10-11  9:02     ` Peter Stephenson
2007-10-11  4:03   ` Kenneth Lareau
2007-10-11  6:13   ` Kenneth Lareau

Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/zsh/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).