37
\$\begingroup\$

Write a script that writes to standard output, or your language's equivalent, 100 lines of (valid) Java code that begins with:

class Tuple1 {public Object _0;}
class Tuple2 {public Object _0,_1;}
class Tuple3 {public Object _0,_1,_2;}
class Tuple4 {public Object _0,_1,_2,_3;}
class Tuple5 {public Object _0,_1,_2,_3,_4;}
class Tuple6 {public Object _0,_1,_2,_3,_4,_5;}
class Tuple7 {public Object _0,_1,_2,_3,_4,_5,_6;}
class Tuple8 {public Object _0,_1,_2,_3,_4,_5,_6,_7;}
class Tuple9 {public Object _0,_1,_2,_3,_4,_5,_6,_7,_8;}
class Tuple10 {public Object _0,_1,_2,_3,_4,_5,_6,_7,_8,_9;}
class Tuple11 {public Object _0,_1,_2,_3,_4,_5,_6,_7,_8,_9,_10;}
class Tuple12 {public Object _0,_1,_2,_3,_4,_5,_6,_7,_8,_9,_10,_11;}

The last line should begin with class Tuple100.

This is , so the shortest code in bytes wins!

\$\endgroup\$
  • 6
    \$\begingroup\$ I accept the challenge! In fact, I'm going to do this in Java! \$\endgroup\$ – Ashwin Gupta Feb 4 '16 at 0:41
  • \$\begingroup\$ Can I write a function or does it have to be a full program? \$\endgroup\$ – Ashwin Gupta Feb 4 '16 at 0:42
  • 3
    \$\begingroup\$ I'm new to the site so asked for a script, but I've been hearing it's customary to allow functions, and that makes a lot of sense for Java, to avoid public static void main(String[] args) so definitely give a function if you like. \$\endgroup\$ – Ray Toal Feb 4 '16 at 3:40
  • 1
    \$\begingroup\$ Great! Thank you Ray! \$\endgroup\$ – Ashwin Gupta Feb 4 '16 at 4:01
  • \$\begingroup\$ It's not anywhere near small enough, but I think this a perfect place to share my n-tuple generator I wrote in Java: github.com/kenzierocks/Tuplocity. It generates fully generic tuples! \$\endgroup\$ – Octavia Togami Feb 4 '16 at 4:57

61 Answers 61

1
\$\begingroup\$

𝔼𝕊𝕄𝕚𝕟, 44 chars / 55 bytes

⩥ṥⓜᵖ`э£ēÕ>rĕĠ#⦃$⦄ {public Object ⦃⩤$ⓜ⍘_+$⦄;}

Try it here (Firefox only).

Still amazing even without compression.

Explanation

⩥ṥⓜᵖ`э£ēÕ>rĕĠ#⦃$⦄ {public Object ⦃⩤$ⓜ⍘_+$⦄;} // implicit: ṥ=input
⩥ṥⓜ                                          // generate [0,100) to map over
    ᵖ                                         // push to stack:
     `э£ēÕ>rĕĠ#⦃$⦄                             // class Tuple[insert mapitem here]
                  {public Object ⦃⩤$ⓜ⍘_+$⦄;} // {public Object [insert [0,mapitem] here preceded by semicolons per item];}
                                             // implicit stack output
\$\endgroup\$
1
\$\begingroup\$

JavaScript (ES6), 85 bytes

s='_0';for(i=1;i<101;s+=',_'+i++)console.log(`class Tuple${i} {public Object ${s};}`)
\$\endgroup\$
  • 1
    \$\begingroup\$ Very nice! Could you add an explanation? Also, I believe you could save a single byte by moving s+=',_'+i++ into the last bit of the for. \$\endgroup\$ – Nic Hartley May 20 '16 at 1:25
  • \$\begingroup\$ Thanks! The for loop prints out each line whilst adding to the _0,_1,_2... string (stored in s) at the end of each iteration. Uses ES6 template strings to save a few bytes :P \$\endgroup\$ – dtkaias May 20 '16 at 5:21
  • \$\begingroup\$ (I meant edit an explanation into your question. Answers with them tend to be better-received) \$\endgroup\$ – Nic Hartley May 20 '16 at 14:18
1
\$\begingroup\$

Pip, 53 51 bytes

LhP["class Tuple"++i" {public Object "'_.,iJ',";}"]

Try it online!

Explanation:

                        h is preinitialized to 100 and i to 0 (implicit)
L h                     Loop 100 times:
 P [                    Print the following list (with trailing newline):
    "class Tuple"
    ++i                 Increment i here so that its values run from 1 to 100
    " {public Object "
    ('_ . ,i) J ',      Range(i), concatenate "_" before each element, join on ","
    ";}"
   ]                    By default, lists are concatenated when printed
\$\endgroup\$
  • \$\begingroup\$ Idea: Maybe name your language so that it doesn't get confused with the pip utility \$\endgroup\$ – LegionMammal978 Apr 24 '16 at 9:54
  • \$\begingroup\$ @LegionMammal978 Yeah, I realized that problem after the fact... but I think now it is what it is. (When I came up with the acronym "Pip Isn't Python," it seemed so perfect...) \$\endgroup\$ – DLosc Apr 25 '16 at 22:42
1
\$\begingroup\$

PowerShell, 63 bytes

1..100|%{"class Tuple$_ {public Object _$(0..--$_-Join",_");}"}

Try it online!

Two byte improvement of bulletprooffool's answer, who seems to be an abandoned account now.

\$\endgroup\$
0
\$\begingroup\$

Perl5, 77 Bytes

$"=", ";for(0..100){push@o,"_$_";print"class Tuple$_\{public Object @o;\}\n"}

Also

for(0..100){$o.=$o?", _$_":"_$_";print"class Tuple$_\{public Object $o;\}\n"}
\$\endgroup\$
  • \$\begingroup\$ Welcome to Programming Puzzles and Code Golf! Please put your language together with the byte count in the title and format the code correctly (see my edit). Hope your have a great time here! :) \$\endgroup\$ – Denker Feb 3 '16 at 8:37
  • \$\begingroup\$ You don't have to put a backslash for the first accolade in the print : print"class Tuple$_{public Object $o;\}\n"} \$\endgroup\$ – Paul Picard Feb 3 '16 at 16:22
  • \$\begingroup\$ You have an off-by-one error, the first line should be Tuple1 … _0;. The simple fix would be to put the push after the print and to make the print conditional on $_. …;for(0..100){$_&&print …;push@o,"_$_"} \$\endgroup\$ – Brad Gilbert b2gills Feb 3 '16 at 17:23
0
\$\begingroup\$

Java 1.8, 163 Bytes

interface T{static void main(String[]a){String o="";for(int i=0;i<100;){o+=(i>0?",":"")+"_"+i;System.out.println("class Tuple"+(++i)+" {public Object "+o+";}");}}}

Ungolfed

interface T{
static void main(String[]a){
    String o="";
    for(int i=0;i<100;){
        o+=(i>0?",":"")+"_"+i;
        System.out.println("class Tuple"+(++i)+" {public Object "+o+";}");
    }
}
}
\$\endgroup\$
  • 1
    \$\begingroup\$ Unless stated otherwise in the challenge, you can always use functions instead of full prorgams. So youcan save all that static void main... stuff by using a Java8-lambda which implictly returns the result. \$\endgroup\$ – Denker Feb 3 '16 at 10:58
  • \$\begingroup\$ Old habits die hard ;) \$\endgroup\$ – Denham Coote Feb 3 '16 at 10:59
0
\$\begingroup\$

AWK, 84 Bytes

BEGIN{m=" {public Object _0";for(i=1;i<101;){print"class Tuple"i m";}";m=m",_"i++}}

I do like playing with AWK.

If the script were placed in a file, tuple.awk, and called with a 1 line input, the BEGIN tag could be removed... e.g. echo 1|awk -f tuple.awk

Not sure if any other golfing could be done to it.

\$\endgroup\$
0
\$\begingroup\$

Clojure, 103 chars

(map(fn[n](prn(str"class Tuple"n" {public Object "(join","(map #(+"_"%)(range n)))";}")))(range 1 101))
\$\endgroup\$
0
\$\begingroup\$

Common Lisp, 106 bytes

(let(y)(dotimes(x 100)(push x y)(format t"class Tuple~d {public Object ~{_~d~^,~};}~&"(1+ x)(reverse y))))

Try it online!

\$\endgroup\$
0
\$\begingroup\$

Java 8, 184 194 bytes

(just to add void r() as mentioned in the comments)

void r(){IntStream.rangeClosed(1,100).mapToObj(i->IntStream.range(0,i).mapToObj(j->"_"+j).collect(Collectors.joining(",","class Tuple"+i+" {public Object ",";}"))).forEach(System.out::println);}

Ungolfed

void r(){ IntStream.rangeClosed(1, 100)
    .mapToObj(i -> IntStream.range(0, i).mapToObj(j -> "_" + j)
        .collect(Collectors.joining(",", "class Tuple" + i + " {public Object ", ";}")))
    .forEach(System.out::println); }
\$\endgroup\$
  • \$\begingroup\$ This code isn't even in a function or anything... You need to add like void r(){ around it } \$\endgroup\$ – Ashwin Gupta Feb 4 '16 at 4:02
  • \$\begingroup\$ Np. Why is your "," seperate from your "_" if the are right next to each other when you print? You may be able to cut some bytes by using this string: ",_" \$\endgroup\$ – Ashwin Gupta Feb 4 '16 at 4:43
  • \$\begingroup\$ @AshwinGupta "," is used as a delimiter for a Stream<String> of _0 _1 _2.... If I'm going to used ",_" as the delimiter, the mapToObj() conversion is longer (String::valueOf), and I'll need an additional _ at the end of " {public Object " to handle that. \$\endgroup\$ – h.j.k. Feb 4 '16 at 5:35
  • 1
    \$\begingroup\$ oh okay I didn't know that. I'm a begginer programmer really. I just noticed. \$\endgroup\$ – Ashwin Gupta Feb 4 '16 at 5:36
0
\$\begingroup\$

Java, 166 Bytes

void m(){int c=0;while(c!=100){System.out.print("class Tuple"+(c+1)+" {public Object ");for(int i=0;i<c+1;i++)System.out.print(",_"+i);System.out.print(";}\n");c++;}}

Not bad for java, I mean calling main() alone is like 10+ bytes.. (plus long print statements)

(Non-Competetive) Java w/ Quick-Print! 137 bytes

import t.Q;
void m(){int c=0;while(c!=100){Q.p("class Tuple"+(c+1)+" {public Object ");for(int i=0;i<c+1;i++)Q.p(",_"+i);Q.p(";}\n");c++;}}

This one uses a quick library I have been thinking about making for a while(and just made) for shortening the long java print statements. It saves 29 bytes and that was just with 3 system.out.print() calls in the original. I've marked this one as non-competetive since the lib was made after the challenge was posted. Also, I don't know if we are allowed to use cheap little tricks like this and I felt bad about it. (Although in a way its fair since java has the longest print statements of any language I know).

\$\endgroup\$
0
\$\begingroup\$

Brachylog, 99 bytes

0:99eI+1=J,[J]:"class Tuple~w {public Object "w,(I{:0reI,(?I,F";";?'I,F","):"w~_"cr:[I]rw\};"}
"w\)
\$\endgroup\$
0
\$\begingroup\$

Scala, 119 bytes

I'm new to Scala and to codegolf, so nothing fancy.

for(i<-1 to 100){print("class Tuple"+i+" {public Object "); for(j<-0 to i-2) print("_"+j+","); print("_"+(i-1)+";}\n")}

Try online

More readable version:

for (i <- 1 to 100) {
  print("class Tuple" + i + " {public Object ")
  for (j <- 0 to i - 2) print("_" + j + ",")
  print("_" + (i - 1) + ";}\n")
}
\$\endgroup\$
0
\$\begingroup\$

Haskell, 131 bytes

Call j.

import Data.List
j=mapM_(\n->putStrLn("class Tuple"++show n++" {public Object _"++intercalate",_"(map show[0..n-1])++";}"))[1..100]
\$\endgroup\$
0
\$\begingroup\$

BlitzMax, 81 bytes

While i<100s$:+",_"+i i:+1Print"class Tuple"+i+" {public Object "+s[1..]+";}"Wend

That's the whole program. Non-Strict and without a Framework, so I can use variables without declaration and Print without having to import StandardIO. The String slicing operator comes in handy here, since it allows me to easily remove the superfluous comma at the beginning of each Object list.

Ungolfed version

While i < 100
    s$ :+ ",_" + i
    i :+ 1
    Print "class Tuple" + i + " {public Object " + s[1..] + ";}"
Wend
\$\endgroup\$
0
\$\begingroup\$

Haskell, 102 bytes

putStr$[0..99]>>= \a->"class Tuple"++show(a+1)++" {public Object _0"++([1..a]>>=(",_"++).show)++";}\n"

As a program (107 bytes):

main=putStr$[0..99]>>= \a->"class Tuple"++show(a+1)++" {public Object _0"++([1..a]>>=(",_"++).show)++";}\n"

Explanation

putStr$ -- print it all
  [0..99]>>= \a->                 -- For each a from 0 to 99, make the
                                  -- following string, then concat
                                  -- it all together

    "class Tuple" ++ show (a + 1) -- class name (starts at Tuple1)

    ++" {public Object _0"        -- All Tuple classes start with a _0

    ++([1..a]>>=(",_"++).show)    -- If we're on TupleA,
                                  -- concat together ",_1", ",_2", etc.
                                  -- up to "_A"

    ++";}\n"
\$\endgroup\$
0
\$\begingroup\$

Python 3, 96 bytes

r=range
for i in r(1,101):print('class Tuple%d {public Object _0'%i,*r(1,i),sep=',_',end=';}\n')

Joining its expanded positional arguments from *range(1,i) using a custom separator ,_ and ending each line with ;}, Python 3's print() function happily does the job almost entirely on its own.

\$\endgroup\$
0
\$\begingroup\$

jq, 80 characters

(78 characters code + 2 characters command line option.)

range(1;13)|"class Tuple\(.) {public Object \([range(.)|"_\(.)"]|join(","));}"

Sample run:

bash-4.3$ bin/jq -nr 'range(1;101)|"class Tuple\(.) {public Object \([range(.)|"_\(.)"]|join(","));}"' | head
class Tuple1 {public Object _0;}
class Tuple2 {public Object _0,_1;}
class Tuple3 {public Object _0,_1,_2;}
class Tuple4 {public Object _0,_1,_2,_3;}
class Tuple5 {public Object _0,_1,_2,_3,_4;}
class Tuple6 {public Object _0,_1,_2,_3,_4,_5;}
class Tuple7 {public Object _0,_1,_2,_3,_4,_5,_6;}
class Tuple8 {public Object _0,_1,_2,_3,_4,_5,_6,_7;}
class Tuple9 {public Object _0,_1,_2,_3,_4,_5,_6,_7,_8;}
class Tuple10 {public Object _0,_1,_2,_3,_4,_5,_6,_7,_8,_9;}

On-line test (Passing -r through URL is not supported – check Raw Output yourself.)

\$\endgroup\$
0
\$\begingroup\$

Javascript ES6, 128 122 chars

for(s="class Tuple1 {public Object _0;}",q=0;q++<100;s=s.replace(/\d+(?!\d|,)( )?/g,(m,s)=>[q+1+s][+s]||m+",_"+q))alert(s)
\$\endgroup\$
0
\$\begingroup\$

Kotlin, 92 Bytes

fun j(){var s="_0";for(i in 1..100){println("class Tuple$i {public Object $s;}");s+=",_"+i}}

Try it online!

Ungolfed

fun j(){
    var s = "_0"
    for(i in 1..100){
        println("class Tuple$i {public Object $s;}")
        s+=",_"+i
    }   
}
\$\endgroup\$
0
\$\begingroup\$

Tcl, 107 bytes

time {set s "class Tuple[incr i] \{public Object \{"
set j 0
time {set s $s\_$j\;
incr j} $i
puts $s\}} 100

Try it online!

Tcl, 117 bytes

proc P n {set i 0;time {append s _$i\;;incr i} $n;set s}
time {puts "class Tuple[incr i] {public Object [P $i]}"} 100

Try it online!

\$\endgroup\$
0
\$\begingroup\$

Perl 5, 68 bytes

$"=',_';@d=0..$_-1,say"class Tuple$_ {public Object _@d;}"for 1..100

Try it online!

\$\endgroup\$
0
\$\begingroup\$

V, 47 bytes

iclass Tuple1 {public Object _0;}99ñÙ$hi,_Û

Try it online!

\$\endgroup\$
0
\$\begingroup\$

MATL, 59 bytes

100:"'class Tuple%i {public Object %s}'@'_%i,'@:qYD59O(3$YD

EDITS

  • (May 16, 2016): to adapt to recent changes in the language, O needs to be replaced by J in the above code.
  • (September 29, 2017): the order of the inputs to YD has changed, which requires some modifications in the code.

The following link includes these changes.

Try it online!

\$\endgroup\$
0
\$\begingroup\$

C++, 139 137 bytes

-2 bytes thanks to Zacharý

#include<iostream>
auto&p=printf;void j(){int i=1,k;for(;i<101;++i){p("class T%d{",i);for(k=0;k<i;++k)p("public Object v%d;",k);p("}");}}
\$\endgroup\$
  • \$\begingroup\$ Woah, nice golf! If you're straying from the source to generate equivalent valid java code (using public Object v0;public Object v1; instead of public Object _0,_1;, removing the space before {) then can't you change p("}\n") to p("}")? That raises another question, are you allowed to stray from the source? \$\endgroup\$ – Zacharý Sep 30 '17 at 22:43
  • \$\begingroup\$ @Zacharý actually, the code outputed is class T2{public Object v0;public Object v1}, so... I may try to use some printf magic to erase a full loop \$\endgroup\$ – HatsuPointerKun Sep 30 '17 at 23:28
  • \$\begingroup\$ Secondly, why are using <iostream> instead of <cstdio> if you're just using printf? Unless I'm missing something, that may work. \$\endgroup\$ – Zacharý Oct 5 '17 at 16:49
0
\$\begingroup\$

ink, 87 bytes

-(i)class Tuple{i} \{public Object <>
~temp k=-1
-(j)~k++
_{k}{k<i:,->j};\}
{i<100:->i}

Try it online!

\$\endgroup\$
0
\$\begingroup\$

C# (Visual C# Interactive Compiler), 110 109 bytes

int i;for(;i++<100;)Write($"class Tuple{i} {{public Object _{string.Join(",_",Enumerable.Range(0,i))};}}\n");

Saved a byte thanks to @dana

Try it online!

\$\endgroup\$
  • \$\begingroup\$ 109 - declare i at the top level ;) \$\endgroup\$ – dana Apr 3 at 5:14
0
\$\begingroup\$

Forth (gforth), 97 bytes

: f 101 1 do cr ." class Tuple"i 1 .r ." {public Object _0"i 1 ?do ." ,_"i 1 .r loop ." ;}"loop ;

Try it online!

Code Explanation

: f                           \ start a new word definition
  101 1 do                    \ start a loop from 1 to 100
    cr                        \ output a new line
    ." class Tuple"i 1 .r     \ output "class TupleN" where N is the loop index
    ." {public Object _0"     \ output "{public Object _0"
    i 1 ?do                   \ start a loop from 1 to the outer loop index
      ." ,_"i 1 .r            \ output ",_N" where N is the inner loop index
    loop                      \ end the inner loop
    ," ;}"                    \ output ";}"
  loop                        \ end the outer loop
 ;                            \ end the word definition
\$\endgroup\$
0
\$\begingroup\$

05AB1E, 43 bytes

тFNÝ„,_ý'_ì'Œº™'‚ÆN>'„à"ÿ Tupleÿ {ÿ ÿ ÿ;}"=

Try it online!

\$\endgroup\$
0
\$\begingroup\$

Python 2, 93 bytes

i=1;exec'print"class Tuple%s {public Object _"%i+",_".join(map(str,range(i)))+";}";i+=1;'*100

Try it online!

I rarely get to use exec in code golf, so I wasn't going to pass on this one!

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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