none
Coding Competition.... RRS feed

  • Question

  • Alrite people, most of us enjoyed to the Devsquare Quiz competitions,
    So I m starting this thread just to Stimulate the environment a bit,
    We will start with the problems here, Post your codes for the problems...

    We will discuss out whose code is better, and will distribute points for it..
    Wat say?? Did you liked it or not....


    Friday, June 1, 2007 3:07 PM

Answers

  • So here goes the 1st question..

    Now answer this... write a code to create a series like this....

    111
    112
    113
    121
    122
    123
    131
    132
    133
    211
    212
    .................
    332
    333

    This is the case of generating all the permutation of N numbers.. like we have 3 above and the possible set of 3 numbers is generated, like 1 1 1 is one set 1 2 3 is another set...

    So you take 2 input
    1) the number N
    2) The size of the set K....




    Friday, June 1, 2007 3:09 PM
  • Man i really love this. This is what i was expecting. I was going to start a thread like this, but you thought of it first Smile good one Varun.

    I really would love to start it. Lets get started guys.
    Friday, June 1, 2007 3:10 PM
  • I haven't done the above program yet. I have dont a similar one, that does the permutation of a given set of characters or numbers. May be a ilttle bit of modifications to it is required. Anyways, i think ill post my code, it will result the permutatation of a string using recursion.


    Friday, June 1, 2007 4:34 PM

  • Permutation of a string using Recursion

    #include<stdio.h>
    #include<conio.h>
    #include<string.h>
    void perm (char *a, int k, int n) {
        int i=0,t;
        if(k==n) {
            puts(a);
        }
        else {
            for(i=k ; i<n ; i++) {
                t = a[i];
                a[i] = a[k];
                a[k] = t;
                perm(a,k+1,n);
                t = a[i];
                a[i] = a[k];
                a[k] = t;
            }
        }
    }
    void main() {
        char *str;
        int len,i;
        clrscr();
        printf("Enter a string : ");
        gets(str);
        len = strlen(str);
        perm(str,0,len);
        getch();

    }
    Friday, June 1, 2007 4:35 PM
  • Hey Harshil, nice code, you can use this logic to work out my probem, its very similar, but the pattern must be maintained, and this is only a beta part of the question... the tougher question is after this one gets finished....
    Friday, June 1, 2007 4:59 PM
  • @Varun do you have a solution to this problem ? if not, then i will try creating the code for it. Its not hard to do it. Smile
    Friday, June 1, 2007 5:43 PM
  • Yes i have the code for it, and i loved making one for it, that is the reason y i posted the question, people will enjoy coding this problem...
    Friday, June 1, 2007 6:05 PM
  • okay m8, i will try creating the code for it tomorrow Smile Actually i am busy with other things, i don't get time where i can sit 1-2 hours at a stretch with concentration ...
    Friday, June 1, 2007 6:41 PM
  • SILLY-SORT

     

     

    Suppose you translate a whole number  into a string of words, one for each digit,

    followed by a single space. For example, the number 407 becomes the word string:

    "FOUR ZERO SEVEN".

    Now comes the fun part. You are to write a program that accepts a list of whole numbers,

    and  prints out the numbers in Silly-Sort order, sorted by their word strings in

    dictionary order.

    In particular, given the six numbers

     

            7  23  99  54  974  5

     

    your program output for the Silly-Sorted list should be printed in the following order:

     

            5  54  99  974  7  23 

     

    because "FIVE" < "FIVE FOUR" <.....< "TWO THREE" as strings in dictionary order.
    Friday, June 1, 2007 10:08 PM
  • Nice problem, I will work out the code for it and post the answer soom ,keep it up and let the questions follow in, while then try out the problem i posted
    Saturday, June 2, 2007 3:46 AM
  • I think we should post small questions. As solving these long problems, will consume lot of our time. what say you guys ? Or we can just discuss the logic and give algo for it ?
    Saturday, June 2, 2007 4:44 AM
  • hey this problem can easily be solved by "nested loops" technique.
    i'll soon post the answer

    Saturday, June 2, 2007 6:23 AM
  • I thin it can be better solved using recursion.
    Saturday, June 2, 2007 7:23 AM
  • Well why dont both of you give your code then.. I have done it using a for loop nesting, I will post my code if you want the answer, but it will be fun working it out, specially the other part of the question when i am done with this question..
    Saturday, June 2, 2007 6:00 PM
  • man please dont post the code for the next 1-2 days. I was coding somthing else, so i didn;t get time to code for this problem Smile

    Ill surely try it tomorrow or day after tomorrow.


    Saturday, June 2, 2007 6:56 PM
  • Man i really love this. This is what i was expecting. I was going to start a thread like this, but you thought of it first good one Varun.

    I really would love to start it. Lets get started guys.
    Saturday, June 2, 2007 7:08 PM
  • @Raman - If you have tried out the code, please paste it here.
    Sunday, June 3, 2007 2:33 PM
  • @Varun i have made that code for fixed 3 numbers and 3 size Stick out tongue but making it general will take much more time Sad I think its better you post the code. I dont want to do similar college stuffs in vacations Stick out tongue
    Sunday, June 3, 2007 4:11 PM
  • Well making a general form is the main task, but no probs, I have done it..

    So see the code and tell me if we can do anything to improve it...
    This code can be more generalised to create a sample set from any vector....

    Code Snippet


    // This is a program to generate all the permutations of the first N integers.

    #include<stdio.h>
    #include<conio.h>

    void main()
    {
        int digit[10]={1,1,1,1,1,1,1,1,1}, n, r=3,i;
        clrscr();
    start :
        printf("Please give the Value of 1st n Integers to be sampled = ");
        scanf("%d",&n);
        printf("Please give the value of sample set = ");
        scanf("%d",&r);
        if (n<r)
        {
            printf("The sample set cannot be greater than the number of integers.");
            printf("\nPlease try again");
            goto start;
        }
        while (!(digit[0]>n))
        {
            for (i=0; i<r; i++)
                printf("%d ",digit[i]);
            digit[r-1]++;
            for (i=r-1; i>0; i--)
            {
                if (digit[i]>n)
                {
                    digit[i]=digit[i]%n;
                    digit[i-1]++;
                }
            }
            printf("\n");
        }
        getch();
    }

    /*                   OUTPUT

    Please give the Value of 1st n Integers to be sampled = 3
    Please give the value of sample set = 3

    1 1 1
    1 1 2
    1 1 3
    1 2 1
    1 2 2
    1 2 3
    1 3 1
    1 3 2
    1 3 3
    2 1 1
    2 1 2
    2 1 3
    2 2 1
    2 2 2
    2 2 3
    2 3 1
    2 3 2
    2 3 3
    3 1 1
    3 1 2
    3 1 3
    3 2 1
    3 2 2
    3 2 3
    3 3 1
    3 3 2
    3 3 3

    */


    Sunday, June 3, 2007 5:06 PM
  • now when you have this code, you can work out on the tougher part of this question...

    Generate a series like this :-
       123
       124
       125
       134
       135

    Mind well this is not as simple as it seems... its tougher than the previous one.. as the numbers should not repeat...
    Sunday, June 3, 2007 5:08 PM
  • hey i couldn't understand your logic.
    sorry dude.
    explain it to me
    Monday, June 4, 2007 2:53 AM
  • Sure Sunil,
    Its like a counter, i.e. lets say you have the number system of base N+1,
    So you only increment the last digit, and then check for every digit, if any digit crosses N, than initialize it again to 1 and increment the preceding digit...
    i.e.
    if the digits are for N = 3
    1 2 3
    next increment will become
    1 2 4
    Here 4 over laps 4, so
    1 3 1
    Initialized 4 to 1 and incremented the preceding digit....

    Now take example of
    1 3 3
    After increment
    1 3 4
    ----------
    1 4 1
    -------
    2 1 1
    -------

    So after 1 3 3
    you get  2 1 1
    -----------------------
    I hope its clear now
    Monday, June 4, 2007 4:16 AM
  • Hey varun , i had tried a similar code that you did, but i used recursion Stick out tongue and i used a reverse loop from r-1 to K-1 instead of r-1 to 0, may be thats why it wasn;t working proper in general Stick out tongue

    But short and sweet code man, let me see if i can modify and generate unique numbers.
    Monday, June 4, 2007 4:56 AM
  • After seeing the code, it seems so simple, hahaha , i dont know why was i not able to make it complete Stick out tongue
    Monday, June 4, 2007 4:59 AM
  • thanks buddy!!
    Monday, June 4, 2007 6:18 AM
  • & what's the use of passiing the no of sets?
    Monday, June 4, 2007 6:41 AM

All replies

  • So here goes the 1st question..

    Now answer this... write a code to create a series like this....

    111
    112
    113
    121
    122
    123
    131
    132
    133
    211
    212
    .................
    332
    333

    This is the case of generating all the permutation of N numbers.. like we have 3 above and the possible set of 3 numbers is generated, like 1 1 1 is one set 1 2 3 is another set...

    So you take 2 input
    1) the number N
    2) The size of the set K....




    Friday, June 1, 2007 3:09 PM
  • Man i really love this. This is what i was expecting. I was going to start a thread like this, but you thought of it first Smile good one Varun.

    I really would love to start it. Lets get started guys.
    Friday, June 1, 2007 3:10 PM
  • I haven't done the above program yet. I have dont a similar one, that does the permutation of a given set of characters or numbers. May be a ilttle bit of modifications to it is required. Anyways, i think ill post my code, it will result the permutatation of a string using recursion.


    Friday, June 1, 2007 4:34 PM

  • Permutation of a string using Recursion

    #include<stdio.h>
    #include<conio.h>
    #include<string.h>
    void perm (char *a, int k, int n) {
        int i=0,t;
        if(k==n) {
            puts(a);
        }
        else {
            for(i=k ; i<n ; i++) {
                t = a[i];
                a[i] = a[k];
                a[k] = t;
                perm(a,k+1,n);
                t = a[i];
                a[i] = a[k];
                a[k] = t;
            }
        }
    }
    void main() {
        char *str;
        int len,i;
        clrscr();
        printf("Enter a string : ");
        gets(str);
        len = strlen(str);
        perm(str,0,len);
        getch();

    }
    Friday, June 1, 2007 4:35 PM
  • Hey Harshil, nice code, you can use this logic to work out my probem, its very similar, but the pattern must be maintained, and this is only a beta part of the question... the tougher question is after this one gets finished....
    Friday, June 1, 2007 4:59 PM
  • @Varun do you have a solution to this problem ? if not, then i will try creating the code for it. Its not hard to do it. Smile
    Friday, June 1, 2007 5:43 PM
  • Yes i have the code for it, and i loved making one for it, that is the reason y i posted the question, people will enjoy coding this problem...
    Friday, June 1, 2007 6:05 PM
  • okay m8, i will try creating the code for it tomorrow Smile Actually i am busy with other things, i don't get time where i can sit 1-2 hours at a stretch with concentration ...
    Friday, June 1, 2007 6:41 PM
  • SILLY-SORT

     

     

    Suppose you translate a whole number  into a string of words, one for each digit,

    followed by a single space. For example, the number 407 becomes the word string:

    "FOUR ZERO SEVEN".

    Now comes the fun part. You are to write a program that accepts a list of whole numbers,

    and  prints out the numbers in Silly-Sort order, sorted by their word strings in

    dictionary order.

    In particular, given the six numbers

     

            7  23  99  54  974  5

     

    your program output for the Silly-Sorted list should be printed in the following order:

     

            5  54  99  974  7  23 

     

    because "FIVE" < "FIVE FOUR" <.....< "TWO THREE" as strings in dictionary order.
    Friday, June 1, 2007 10:08 PM
  • Nice problem, I will work out the code for it and post the answer soom ,keep it up and let the questions follow in, while then try out the problem i posted
    Saturday, June 2, 2007 3:46 AM
  • I think we should post small questions. As solving these long problems, will consume lot of our time. what say you guys ? Or we can just discuss the logic and give algo for it ?
    Saturday, June 2, 2007 4:44 AM
  • hey this problem can easily be solved by "nested loops" technique.
    i'll soon post the answer

    Saturday, June 2, 2007 6:23 AM
  • I thin it can be better solved using recursion.
    Saturday, June 2, 2007 7:23 AM
  • Well why dont both of you give your code then.. I have done it using a for loop nesting, I will post my code if you want the answer, but it will be fun working it out, specially the other part of the question when i am done with this question..
    Saturday, June 2, 2007 6:00 PM
  • man please dont post the code for the next 1-2 days. I was coding somthing else, so i didn;t get time to code for this problem Smile

    Ill surely try it tomorrow or day after tomorrow.


    Saturday, June 2, 2007 6:56 PM
  • Man i really love this. This is what i was expecting. I was going to start a thread like this, but you thought of it first good one Varun.

    I really would love to start it. Lets get started guys.
    Saturday, June 2, 2007 7:08 PM
  • @Raman - If you have tried out the code, please paste it here.
    Sunday, June 3, 2007 2:33 PM
  • @Varun i have made that code for fixed 3 numbers and 3 size Stick out tongue but making it general will take much more time Sad I think its better you post the code. I dont want to do similar college stuffs in vacations Stick out tongue
    Sunday, June 3, 2007 4:11 PM
  • Well making a general form is the main task, but no probs, I have done it..

    So see the code and tell me if we can do anything to improve it...
    This code can be more generalised to create a sample set from any vector....

    Code Snippet


    // This is a program to generate all the permutations of the first N integers.

    #include<stdio.h>
    #include<conio.h>

    void main()
    {
        int digit[10]={1,1,1,1,1,1,1,1,1}, n, r=3,i;
        clrscr();
    start :
        printf("Please give the Value of 1st n Integers to be sampled = ");
        scanf("%d",&n);
        printf("Please give the value of sample set = ");
        scanf("%d",&r);
        if (n<r)
        {
            printf("The sample set cannot be greater than the number of integers.");
            printf("\nPlease try again");
            goto start;
        }
        while (!(digit[0]>n))
        {
            for (i=0; i<r; i++)
                printf("%d ",digit[i]);
            digit[r-1]++;
            for (i=r-1; i>0; i--)
            {
                if (digit[i]>n)
                {
                    digit[i]=digit[i]%n;
                    digit[i-1]++;
                }
            }
            printf("\n");
        }
        getch();
    }

    /*                   OUTPUT

    Please give the Value of 1st n Integers to be sampled = 3
    Please give the value of sample set = 3

    1 1 1
    1 1 2
    1 1 3
    1 2 1
    1 2 2
    1 2 3
    1 3 1
    1 3 2
    1 3 3
    2 1 1
    2 1 2
    2 1 3
    2 2 1
    2 2 2
    2 2 3
    2 3 1
    2 3 2
    2 3 3
    3 1 1
    3 1 2
    3 1 3
    3 2 1
    3 2 2
    3 2 3
    3 3 1
    3 3 2
    3 3 3

    */


    Sunday, June 3, 2007 5:06 PM
  • now when you have this code, you can work out on the tougher part of this question...

    Generate a series like this :-
       123
       124
       125
       134
       135

    Mind well this is not as simple as it seems... its tougher than the previous one.. as the numbers should not repeat...
    Sunday, June 3, 2007 5:08 PM
  • hey i couldn't understand your logic.
    sorry dude.
    explain it to me
    Monday, June 4, 2007 2:53 AM
  • Sure Sunil,
    Its like a counter, i.e. lets say you have the number system of base N+1,
    So you only increment the last digit, and then check for every digit, if any digit crosses N, than initialize it again to 1 and increment the preceding digit...
    i.e.
    if the digits are for N = 3
    1 2 3
    next increment will become
    1 2 4
    Here 4 over laps 4, so
    1 3 1
    Initialized 4 to 1 and incremented the preceding digit....

    Now take example of
    1 3 3
    After increment
    1 3 4
    ----------
    1 4 1
    -------
    2 1 1
    -------

    So after 1 3 3
    you get  2 1 1
    -----------------------
    I hope its clear now
    Monday, June 4, 2007 4:16 AM
  • Hey varun , i had tried a similar code that you did, but i used recursion Stick out tongue and i used a reverse loop from r-1 to K-1 instead of r-1 to 0, may be thats why it wasn;t working proper in general Stick out tongue

    But short and sweet code man, let me see if i can modify and generate unique numbers.
    Monday, June 4, 2007 4:56 AM
  • After seeing the code, it seems so simple, hahaha , i dont know why was i not able to make it complete Stick out tongue
    Monday, June 4, 2007 4:59 AM
  • thanks buddy!!
    Monday, June 4, 2007 6:18 AM
  • & what's the use of passiing the no of sets?
    Monday, June 4, 2007 6:41 AM
  • Hey Guys why is this thread stopped ? start posting new questions.
    Tuesday, June 5, 2007 4:02 AM
  • Hey, i have already given one more series to work on.. Well if you have some other nice questions than post them, Will work out on them too...
    Tuesday, June 5, 2007 4:55 AM
  • Damn, how many assignments are you given Stick out tongue looks like your college gives only gives assignment, hehehehe.
    Tuesday, June 5, 2007 5:47 AM