21
\$\begingroup\$

Use any programming language to display numbers between 1 and 99 (including both) in such a way, so that:

  • the numbers are separated by single space,
  • if a number is divisible by 3, it should be in parentheses,
  • if a number is divisible by 4, it should be in square brackets,
  • if a number is divisible by both 3 and 4, it should be in both parentheses and square brackets (with square brackets closer to the number).

Your program should display exactly:

1 2 (3) [4] 5 (6) 7 [8] (9) 10 11 ([12]) 13 14 (15) [16] 17 (18) 19 [20] (21) 22 23 ([24]) 25 26 (27) [28] 29 (30) 31 [32] (33) 34 35 ([36]) 37 38 (39) [40] 41 (42) 43 [44] (45) 46 47 ([48]) 49 50 (51) [52] 53 (54) 55 [56] (57) 58 59 ([60]) 61 62 (63) [64] 65 (66) 67 [68] (69) 70 71 ([72]) 73 74 (75) [76] 77 (78) 79 [80] (81) 82 83 ([84]) 85 86 (87) [88] 89 (90) 91 [92] (93) 94 95 ([96]) 97 98 (99)
\$\endgroup\$
  • 6
    \$\begingroup\$ Related \$\endgroup\$ – ETHproductions Nov 10 '18 at 14:48
  • 3
    \$\begingroup\$ Can we output each entry on a new line, or must the output be all on one line? \$\endgroup\$ – ETHproductions Nov 10 '18 at 14:56
  • 4
    \$\begingroup\$ Can the output end with a space. A few answers seem to assume so. \$\endgroup\$ – Dennis Nov 11 '18 at 16:14

46 Answers 46

1
\$\begingroup\$

Python 2, 68 bytes

i=0
exec"i+=1;t,f=i%3<1,i%4<1;print'('*t+'['*f+`i`+']'*f+')'*t,;"*99

Try it online!

\$\endgroup\$
1
\$\begingroup\$

Pyth, 25 bytes

jdmj]W!%d4dc2*"()"!%d3S99

Try it online here.

jdmj]W!%d4dc2*"()"!%d3S99   
  m                   S99   Map [1-99], as d, using:
                   %d3        d mod 3
                  !           Logical not, effectively maps 0 to 1, all else to 0
             *"()"            Repeat "()" the above number of times
           c2                 Chop into two equal parts - maps "()" to ["(",")"] , and "" to ["",""]
                                The above is result {1}
       %d4                    d mod 4
      !                       Logical not the above
    ]W    d                   If the above is truthy, [d], else d
                                The above is result {2}
   j                          Join {1} on string representation of {2}
jd                          Join the result of the map on spaces, implicit print
\$\endgroup\$
1
\$\begingroup\$

Python 3, 95 bytes

print(*[(i if i%4else f"[{i}]")if i%3else(f"({i})"if i%4else f"([{i}])")for i in range(1,100)])

Try it online!

\$\endgroup\$
1
\$\begingroup\$

APL(NARS), 46 chars, 92 bytes

{{0=3∣w:1⌽')(',⍵⋄⍵}{0=4∣w:1⌽'][',⍵⋄⍵}⍕w←⍵}¨⍳99

litte test:

  {{0=3∣w:1⌽')(',⍵⋄⍵}{0=4∣w:1⌽'][',⍵⋄⍵}⍕w←⍵}¨⍳14
1 2 (3) [4] 5 (6) 7 [8] (9) 10 11 ([12]) 13 14 
\$\endgroup\$
1
\$\begingroup\$

Python 2, 68 bytes

for i in range(1,100):print("(%s)","%s")[i%3>0]%("[%s]"%i,i)[i%4>0],

Try it online!

Ends with a trailing space.

\$\endgroup\$
1
\$\begingroup\$

Stax, 19 17 bytes

ä┐r►U^îⁿ_╤▌,│☻£╡▬

Run and debug it

\$\endgroup\$
1
\$\begingroup\$

C (gcc), 76 bytes

Surprisingly, this straight-forward solution was shorter than the "clever" ones I could come up with.

f(i){for(i=0;i++<99;)printf(i%3?i%4?"%d ":"[%d] ":i%4?"(%d) ":"([%d]) ",i);}

Try it online!

\$\endgroup\$
1
\$\begingroup\$

Perl 5, 51 bytes

say map{$o=$_%4?$_:"[$_]";($_%3?$o:"($o)").$"}1..99

Try it online!

\$\endgroup\$
1
\$\begingroup\$

Clojure, 104 bytes

(apply print(map #(do(def s(if(=(rem % 4)0)(str"["%"]")%))(if(=(rem % 3)0)(str"("s")")s))(range 1 100)))

Try it online!

\$\endgroup\$
1
\$\begingroup\$

CJam, 70 bytes

99,:){X4%0={'[Xs']++}{X}?}fX]{Xs'[/_,(=']/0=i3%0={'(Xs')++}{X}?}fX]S*

Try it out online! (Permalink)


Explanation

99                                                                    Push 99 to stack
  ,                                                                   Make a 0..98 array
   :)                                                                 Increment every value in array
     {                   }fX                                          For X in array at top of stack
                        ?                                             If...
      X4%0=                                                           Condition: X mod 4 = 0
           {        }                                                 Is true, then:
            '[                                                        Push "["
              Xs                                                      Push X as a string
                ']                                                    Push "]"
                  ++                                                  Concatenate to "[X]"
                     { }                                              Is false, then:
                      X                                               Push X
                            ]                                         Capture all in an array
                             {                                 }fX    For X in array at top of stack
                                                              ?       If...
                              Xs                                      X as a string
                                '[/                                   Split by "["
                                   _,(=                               Take last element after split
                                       ']/                            Split by "]"
                                          0=                          Take first element after split
                                            i                         Top element as an integer
                              Xs'[/_,(=']/0=i                         The number X minus any brackets
                                             3%0=                     Condition: X mod 3 = 0
                                                 {        }           Is true, then:
                                                  '(                  Push "("
                                                    Xs                Push the original X as a string
                                                      ')              Push ")"
                                                        ++            Concatenate to "(X)"
                                                           { }        If false, then:
                                                            X         Push X
                                                                  ]   Capture all in an array
                                                                   S* Separate by a space

This does not output a trailing space.

\$\endgroup\$
1
\$\begingroup\$

Japt, 30 bytes

Lo1@=X%4?X:"[{X}]"X%3?U:"({U})

Try it online!

\$\endgroup\$
1
\$\begingroup\$

PHP, 59 bytes

for(;$i<100;$s=++$i%4?$i:"[$i]")echo$i%3|!$s?$s:"($s)"," ";

Try it online!

\$\endgroup\$
1
\$\begingroup\$

C# (Visual C# Interactive Compiler), 71 bytes

int i;for(;i<99;)Write("{0:;;(}{1:;;[}{2}{1:;;]}{0:;;)} ",++i%3,i%4,i);

Try it online!

-9 bytes thanks to @ASCIIOnly!

Leveraging the semicolon (The ";" section separator) to create a custom format string.

\$\endgroup\$
  • \$\begingroup\$ :( close, also I think it'd be fine just adding a space after every term like most answers are doing \$\endgroup\$ – ASCII-only Feb 22 at 14:04
  • \$\begingroup\$ Good call on adding a space after every number. Sometimes I take things too literally :) I'll update later. \$\endgroup\$ – dana Feb 22 at 14:10
1
\$\begingroup\$

Attache, 56 bytes

Echo@@({~Join^^S@_'Mask[4'3|_,["["'"]","("'")"]]}=>1:99)

Try it online!

Explanation

Echo@@({~Join^^S@_'Mask[4'3|_,["["'"]","("'")"]]}=>1:99)
       {                                        }=>1:99  over each number _, 1 to 99:
                        4'3|_                                check if 4 or 3 divide _
                   Mask[     ,["["'"]","("'")"]]             keep the respective brackets
             ^^S@_'                                          fold over this list (seed = S[_]):
        ~Join                                                joining elements by the iterator
Echo@@(                                                ) print strings separated by spaces

Alternatives

58 bytes: Echo@@({~Join^^S@_'Mask[[4,3]|_,["["'"]","("'")"]]}=>1:99)

59 bytes: Print@@({~Join^^S@_'Mask[[4,3]|_,["["'"]","("'")"]]}=>1:99)

76 bytes: Print@@(&/S@{Mask[1'3'4'12|_,Split@$"${_} (${_}) [${_}] ([${_}])"] }=>1:99)

\$\endgroup\$
1
\$\begingroup\$

Javascript, 114 96 bytes

[...Array(99)].map(_=>['[4]','(3)'].reduce((a,r)=>n%r[1]<1?r.replace(r[1],a):a,++n),n=0).join` `

Try it Online!

Thanks to Conor O'Brien for helping me improve it :-)

\$\endgroup\$
  • 1
    \$\begingroup\$ If you haven't heard it yet, welcome to PPCG! You can squeeze out a few more bytes: n%r[1]==0 can become n%r[1]<1 (-1 bytes); .join(' ') to .join` ` (-2 bytes); you can use [...Array(99)].map(_=>...,n=0) to make the iteration section a bit more compact. You can overall golf around 20 bytes from this answer. Try it online!. \$\endgroup\$ – Conor O'Brien Feb 22 at 17:20
  • \$\begingroup\$ Some good tips, thanks @ConorO'Brien! Will add the improvements \$\endgroup\$ – Coert Grobbelaar Feb 23 at 21:00
  • \$\begingroup\$ Damn, both .join` ` and adding ,n=0 to the map parameters are tricks I've never seen. Love learning new things :-) \$\endgroup\$ – Coert Grobbelaar Feb 23 at 21:24
  • \$\begingroup\$ Anytime! Glad I could help! :D \$\endgroup\$ – Conor O'Brien Feb 24 at 4:38
0
\$\begingroup\$

ink, 61 bytes

VAR j=0
-(i)~j="{i%4:{i}|[{i}]}"
{i%3:{j}|({j})} <>{i<99:->i}

Try it online!

Explanation

VAR j=0                       // Declare a variable called j.
-(i)~j="{i%4:{i}|[{i}]}"      // The i label keeps track of how many times it's been visited. Set j to that value, or that value surrounded by brackets, depending o nwhat it is.
{i%3:{j}|({j})} <>{i<99:->i}  // Print j or (j), depending on i. Use glue (<>) to not print a newline. Go back to the i label if it's been visited fewer than 99 times.
\$\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.