The Pragmatic Programmer

Tulisan iseng kalo ada waktu.
See also: Other Geeks@INDC

Just a Little Puzzle

There is a company that ask their candidates to answer a puzzle before applying for their opening job. You can find the puzzle here.

Well, I just want to trying to have a nostalgia with my past time when I was in college. So, I wrote 3 type of the solutions using JavaScript, here they are:

<script class="Solution1" language="javascript">
 
var agroup = ""
  var nextCounterToPrint = 5;
  var currentCounter = 0; //just for increasing speed

  for(var i = 1; i <= 1000; i++){
    agroup += i.toString();
    currentCounter++;
    if(currentCounter == nextCounterToPrint){
      nextCounterToPrint = (nextCounterToPrint == 5) ? 3 : 5;
      currentCounter = 0;
      if(!isFirstTime(i)){
        agroup = "*" + agroup;
      }
      document.write(agroup);
      agroup = "" 
    }
  }

  function isFirstTime(i){
    return (i == 5);
  }
</script >

<script class="LineBreak" language="javascript">
  document.write("<BR>");
</script >

<script class="Solution2" language="javascript">
 
var printed = 0;
  var toggled = false;
  for(var i = 1; i <= 1000; i++){
    document.write(i);
    printed++;
    if(printed == 5 && !toggled)
    {
     
document.write("*");
      printed = 0;
      toggled = true;
    }
    else if(printed == 3 && toggled && i < 1000){
     
document.write("*");
     
printed = 0;
      toggled = false; 
    }
  }

</script >

<script class="LineBreak" language="javascript">
   document.write("<BR>");
</script >

<script class="Solution3" language="javascript">
var lists = [5,8,13,16,21,24,29,32,37,40,45,48,53,56,61,64,69,72,77,80,85,88,93,96,101,104,109,112,117,120,125,128,133,136,141,144,149,152,157,160,165,168,173,176,181,184,189,192,197,200,205,208,213,216,221,224,229,232,237,240,245,248,253,256,261,264,269,272,277,280,285,288,293,296,301,304,309,312,317,320,325,328,333,336,341,344,349,352,357,360,365,368,373,376,381,384,389,392,397,400,405,408,413,416,421,424,429,432,437,440,445,448,453,456,461,464,469,472,477,480,485,488,493,496,501,504,509,512,517,520,525,528,533,536,541,544,549,552,557,560,565,568,573,576,581,584,589,592,597,600,605,608,613,616,621,624,629,632,637,640,645,648,653,656,661,664,669,672,677,680,685,688,693,696,701,704,709,712,717,720,725,728,733,736,741,744,749,752,757,760,765,768,773,776,781,784,789,792,797,800,805,808,813,816,821,824,829,832,837,840,845,848,853,856,861,864,869,872,877,880,885,888,893,896,901,904,909,912,917,920,925,928,933,936,941,944,949,952,957,960,965,968,973,976,981,984,989,992,997];

for(var i = 1; i <= 1000; i++){
  document.write(i);
  if(i == lists[0]){
    lists.shift();
    document.write("*");
  }
}

</script >

Which one do you think is the best one? Or maybe you have another solution, please share!

Share this post: | | | |

Comments

Jimmy Chandra said:

The formatting might be a mess, but what the heck :)

/*

* NumberPuzzle.js

*

* Coded by: Jimmy Chandra

* Last Modified Date: 2007.06.21

*

* Note:

*    This is an reusable, flexible and pluggable solution to the

*    question from http://www.ptgdi.com/career.htm page.

*

* Quick Run Instruction:

*   Type "cscript.NumberPuzzle.js" from Command Prompt

*

*   To run this in browser, see "Web Page Installation Instruction"

*   below.

*/

/**

* PrintNumbers

* parameters:

*   min           : first number to print

*   max           : last number to print

*   separator     : separator to print

*   switchIndexes : array containing the positions after which the

*                   separator character need to be inserted

*   printFn       : print function to call for outputing result

*/

function PrintNumbers(min, max, separator, switchIndexes, printFn) {

if (printFn == null)

throw "printFn delegate not found";

for (var number = min, currentIndex = 0, count = 1, result = "";

number <= max; number++, count++) {

result += number;

if (count === switchIndexes[currentIndex]) {

result += separator;

currentIndex++;

currentIndex = currentIndex % switchIndexes.length;

count = 0;

}

}

printFn(result);

}

PrintNumbers(1, 1000, "*", [5, 3], function(s) { WScript.Echo(s); });

/*

* Web Page Installation Instruction

* To run this in a browser, cut and paste the function above and the

* code below to your HTML page and call the PrintNumber function as

* shown from somewhere.

*/

/*

PrintNumbers(1, 1000, "*", [5, 3],

function(s) {

document.getElementById("Numbers").innerHTML = s;

});

<!-- DOM container for where the number should be printed -->

<div id="Numbers"></div>

*/

# June 21, 2007 11:41 AM

adrian said:

int c = 0; int d = 3;

for (int x = 1; x <= 1000; x++)

{

 Console.Write(x);

 c += d;

 if (c == 15) { d = -5; Console.Write("*"); }

 if (c == 0) { d = 3; Console.Write("*"); }

}

# June 21, 2007 11:56 AM

cipto_the said:

Dim ctr As Integer = 0

       Dim a As Integer = 5

       Dim b As Integer = 3

       Dim c As Integer

       For hitung As Integer = 1 To 1000

           If ctr = a Then

               Console.Write("*")

               c = a

               a = b

               b = c

               ctr = 0

           End If

           Console.Write(hitung)

           ctr += 1

       Next

       Console.ReadLine()

THIS IS MY ANSWER

# June 21, 2007 12:18 PM

norman said:

JavaScript, C#, VB... well, in Mathematica, it's a ONE LINE OF CODE:

Insert[Table[i, {i, 1, 1000}], "*", Union[Table[{i}, {i, 4, 1000, 10}], Table[{i}, {i, 10, 1000, 10}]]]

Btw, remember this one from Google: "First 10-digit prime found in consecutive digits of e" ? ("e" is Euler number/natural number with the value of 2.7182818284...).

Try that in JavaScript, C# or VB... :) Finding the first 10-digit consecutive number in "e" that is a prime.

And here's the Mathematica code for that... it's also a ONE LINE OF CODE:

Select[FromDigits /@ Partition[First[RealDigits[E, 10, 1000]], 10, 1], PrimeQ, 1]

Isn't Functional Programming lovely? He..he..he..

# June 21, 2007 3:02 PM

csharpindonesia said:

Hahaha... dasar pak tomo.

Hubungi dia di tomo@ptgdi.co.id (apa .com saya lupa)

# June 21, 2007 4:47 PM

csharpindonesia said:

Hmm... kayaknya mesti bawa functional programming ke ruang diskusi lantai 18 nih di weekend (hehehe)

# June 21, 2007 4:49 PM

irwansyah said:

OK. Very good all. Thanks to jimmy, adrian, cipto, and norman for the submission. Well, people really have many types of thinking preferences. Your submission really open my eyes on solving problem.

Hehhehe...I hope one day I can play with Mathematica.

Almost forgot, here is one good solution from mr denni zeng:

<script type="text/javascript">

for (var i = 1, j = 1, k = true; i <= 1000; i++, j++) {

document.write(i);

if ((k && !(j % 5)) || (!k && !(j % 3))) {

k = !k;

j = 0;

document.write("*");

}

}

</script>

# June 21, 2007 8:23 PM

cahnom said:

Kode berikut lebih sederhana, lebih sedikit variabel dan lebih sedikit perhitungan, lebih mudah dibaca.

<script type="text/javascript">

 for (var i = 1, j=5; i <= 1000; i++) {

   document.write(i);

   if (i==j) {

     document.write("*");

     j += ((j%2)==0) ? 5 : 3;

   }

 }

</script>

# June 21, 2007 11:06 PM

irwansyah said:

Thanks for the submission cahnom. Well, it doesn't explicitly mentioned but I hope the next submission will not display the '*' star sign after the last number

9989991000*

It should be printed with no star.

# June 21, 2007 11:48 PM

cahnom said:

<script type="text/javascript">

for (var i = 1, j=5; i <= 1000; i++) {

  document.write(i);

  if ((i==j) && (j < 1000)) {

    document.write("*");

    j += ((j%2)==0) ? 5 : 3;

  }

}

</script>

# June 22, 2007 10:19 AM

zeddy said:

static void Main(string[] args)

{

   bool toggle5 = true;

   int inc = 0, max = 1000;

   for (int i = 1; i < max; ++i)

   {

       inc = toggle5 ? 5 : 3;

       PrintAToB(i, i+inc);

       i += inc-1;

       toggle5 = !toggle5;

       if (i < max) Console.Write('*');

   }

}

static void PrintAToB(int from, int to)

{

   for (int i = from; i < to; ++i)

       Console.Write(i);

}

# June 24, 2007 12:17 AM

norman said:

Ralat dikit:

Insert[Table[i, {i, 1, 1000}], "*", Union[Table[{i}, {i, 6, 1000, 8}], Table[{i}, {i, 10, 1000, 8}]]]

"*" yg pertama bukan di posisi 4, tp di posisi 6 rupanya. Irwan, thanks for bring it up.

Tp intinya, expression-nya sama aja. Beda di mau put "*" di mana aja awalnya.

# June 25, 2007 7:12 AM

norman said:

Hi..hi.. salah paste, yg ini yg benar:

Insert[Insert[Table[i, {i, 1, 1000}], "*", Union[Table[{i}, {i, 6, 1000, 8}], Table[{i}, {i, 9, 1000, 8}]]]

# June 25, 2007 7:19 AM

cahnom said:

Mengganti if terselubung (j+=((j%2)==0)?5:3;) dengan bitwise operator.

<script type="text/javascript">

 for (var i = 1, j=5; i <= 1000; i++) {

   document.write(i);

   if ((i==j) && (j < 1000)) {

     document.write("*");

     j += ((~j & 1) * 2) + 3;

   }

 }

</script>

# June 25, 2007 9:12 AM

ipgd said:

int main(void)

{

int flip = 5;

for(int li=1,lj=1;li<=1000;li++,lj++)

{

printf("%d",li);

if(lj == flip)

{

printf("*");

lj = 0;

flip ^= 6;

}

}

return 0;

}

# June 25, 2007 9:22 AM

ipgd said:

Ups, I forgot about indentation.

int main(void)

{

 int flip = 5;

 for(int li=1,lj=1;li<=1000;li++,lj++)

 {

   printf("%d",li);

   if(lj == flip)

   {

     printf("*");

     lj = 0;

     flip ^= 6;

   }

 }

return 0;

}

# June 25, 2007 9:25 AM

cahnom said:

Nih, kode yang lebih simple, hanya butuh 1 variable untuk loop

<script type="text/javascript">

 for (var i = 1; i <= 1000; i++) {

   document.write(i);

   if (((i&7)==5) || ((i&7)==0)) document.write("*");

 }

</script>

Ayo siapa yang mo apply, boleh tuh pake kode di atas. Hehehe..

Kalo mo Apply ke Irwansyah yg minta di belakang tidak boleh ada bintang, tinggal tambahi logika seperti di bawah.

<script type="text/javascript">

 for (var i = 1; i <= 1000; i++) {

   document.write(i);

   if ((((i&7)==5) || ((i&7)==0)) && (i < 1000)) document.write("*");

 }

</script>

# June 25, 2007 10:42 AM

Risman Adnan Mattotorang said:

#include<stdio.h>

int s[10] = {-7,-6,-5,-4,-3,42,-2,-1,0,42};

int *PrintArray(int *p);

int main(void)

{

int *p = s;

do{PrintArray(PrintArray(p));}while(s[0]<990);

return 0;

}

int *PrintArray(int *p)

{

for(int i = 0;i<10;++i)if(i==5||i==9){printf("%c",*(p+i)); *(p+i)=42;}else{printf("%d",*(p+i));*(p+i)+=8;}

return p;

}

# June 25, 2007 5:21 PM

irwansyah said:

Hahahaha...the old time pointer arithmetic...long time not see that one :) Thanks Mr Risman for the post, but surely don't want to debug it :P

# June 25, 2007 7:24 PM

Console.WriteLine("Risman Adnan"); said:

Little problem from Irwansyah , here is my answer: #include&lt;stdio.h&gt; int s[10] = {-7,-6,-5,-4,-3,42,-2,-1,0,42};

# June 25, 2007 7:36 PM

Ahmad Masykur said:

Little problem from Irwansyah , here is my answer in assembler (x86) language: .model flat, stdcall .stack

# June 26, 2007 12:27 AM

BloggiZ - Blogging by Z said:

This is the one-liner (since you can put it all in one line) solution to ILP : 1 2 3 4 5 6 for (i = 1;

# June 26, 2007 8:53 AM

zeddy said:

for (i = 1; i <= 1000; i+=8)

   printf("%d%d%d%d%d%c%d%d%d%c",

       i, i+1, i+2, i+3, i+4, '*',

       i+5, i+6, i+7,

       i+7 == 1000 ? ' ' : '*' );

# June 26, 2007 8:56 AM

Ahmad Masykur said:

Berikut adalah solusi ILP yang sama dengan Zeddy dengan menggunakan pola kelipatan delapan. Perbedaannya

# June 26, 2007 9:51 AM

cahnom said:

Dengan LINQ bisa 1 baris

Console.WriteLine(string.Join("",Enumerable.Range(1,1000).Select(i => ((i & 7)==5) || ((i & 7)==0) ? i.ToString() + "*" : i.ToString()).ToArray()));

# June 11, 2009 4:28 PM