-6
\$\begingroup\$

i've been dabbling with a hypothetical language... now, i want to add a feature/mechanism that can easily format data in as few codes as possible.

now, i need to compare my hypothetical language feature with how it's going to be written in other languages. in particular, would it have lesser code? so i just decided i would have a sample problem and see how this is coded in different languages. problem is, i'm only familiar with C-like languages (C/C++, Java, C#, J#,.. ), Python and some Perl.

so could you guys help me out and write codes, as short as possible (no pseudo code) in different languages for the problem below. be as clever as you want and don't even think of efficiency. we're looking more of how we can describe what needs to be accomplished as short as possible. very declarative. or maybe there's already a languages that does this very well??

transform this:

((a0, a1, a2), (b0, b1, b2), (c0, c1, c2))

to this:

(a0, (b0, c0)), (a1, (b1, c1)), (a2, (b2, c2))

the parenthesization just denotes nested arrays or list, whichever way you want to look at it. also, there are no types, so don't have to worry about that.

\$\endgroup\$
6
  • 1
    \$\begingroup\$ Welcome to Programming Puzzles & Code Golf! This site is for programming contests / challenges, not general programming questions. For those, you may want to try Stack Overflow, but be sure to read their help center before posting this there to make sure that your question will be on-topic. Thanks! \$\endgroup\$
    – Doorknob
    Aug 30, 2014 at 12:37
  • 1
    \$\begingroup\$ It can be tricky to find a home for a question. e.g. Stack Overflow typically looks for specific programming questions; I'm a bit surprised that this Q doesn't qualify, but this site wants puzzles. Consider cstheory.stackexchange.com or programmers.stackexchange.com . The latter is very good for "white-boarding" ideas. \$\endgroup\$ Aug 30, 2014 at 16:09
  • 6
    \$\begingroup\$ I think this is the best place to ask this question. \$\endgroup\$ Aug 30, 2014 at 23:49
  • 4
    \$\begingroup\$ I think this question is not asked well enough to be answered. The specification for what has to be done is pretty much non-existent. \$\endgroup\$
    – Ingo Bürk
    Aug 31, 2014 at 0:43
  • 2
    \$\begingroup\$ It should be reworded to have the emphasis on the challenge and have the context placed in note at the end. \$\endgroup\$ Sep 2, 2014 at 15:33

2 Answers 2

3
\$\begingroup\$

Python

The language feature you're describing is typically known as zip. Assuming the array is named a:

zip(a[0],zip(*a[1:]))

Sample usage:

a = (('a0', 'a1', 'a2'), ('b0', 'b1', 'b2'), ('c0', 'c1', 'c2'))
print zip(a[0],zip(*a[1:]))

Output:

[('a0', ('b0', 'c0')), ('a1', ('b1', 'c1')), ('a2', ('b2', 'c2'))]

If the last two values of each don't need to be in a list:

zip(*a)

would suffice.

\$\endgroup\$
2
  • \$\begingroup\$ it was so long ago since i coded in python... and so little did i know of its library that i've never seen "zip" before... i'll soon look into the documentation for this. and by "soon look into", i mean look into it after a few days (that's just how i roll)... but there certainly are things i could pick up from this. thnx. \$\endgroup\$
    – Dehbop
    Sep 1, 2014 at 1:22
  • \$\begingroup\$ any other inputs?? guys? \$\endgroup\$
    – Dehbop
    Sep 1, 2014 at 1:23
2
\$\begingroup\$

Python 3

I know you already have a Python answer, but here's some code that can be easily translated to other languages:

tup = [[a0, a1, a2], [b0, b1, b2], [c0, c1, c2]]
end = []

for j in range(len(tup[0])):
    end.append([tup[0][j]])
    end[j].append([])
    for i in range(1, len(tup)):
        end[j][1].append(tup[i][j])

...And since you've tagged this as I've golfed my code from 206 characters to 157:

t=[[a0,a1,a2],[b0,b1,b2],[c0,c1,c2]]
e=[];l=len;r=range
for j in r(l(t[0])):
 e.append([t[0][j]]);e[j].append([])
 for i in r(1,l(t)):e[j][1].append(t[i][j])
\$\endgroup\$
4
  • \$\begingroup\$ no offense, but nested loops? that is just typical programming. \$\endgroup\$
    – Dehbop
    Sep 1, 2014 at 10:58
  • \$\begingroup\$ maybe you've mistaken it to be opposite day, today, coz i said short and clever. \$\endgroup\$
    – Dehbop
    Sep 1, 2014 at 11:15
  • \$\begingroup\$ ^ just keeping the whole thread alive and going. no real offense intended. \$\endgroup\$
    – Dehbop
    Sep 1, 2014 at 11:24
  • \$\begingroup\$ now, any other inputs?? \$\endgroup\$
    – Dehbop
    Sep 1, 2014 at 11:25

Not the answer you're looking for? Browse other questions tagged or ask your own question.