Answered by:
Coding Competition....
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....
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....


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.

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();
} 




SILLYSORT
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 SillySort 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 SillySorted list should be printed in the following order:
5 54 99 974 7 23










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[r1]++;
for (i=r1; i>0; i)
{
if (digit[i]>n)
{
digit[i]=digit[i]%n;
digit[i1]++;
}
}
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
*/ 


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 
Hey varun , i had tried a similar code that you did, but i used recursion and i used a reverse loop from r1 to K1 instead of r1 to 0, may be thats why it wasn;t working proper in general
But short and sweet code man, let me see if i can modify and generate unique numbers. 


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....


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.

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();
} 




SILLYSORT
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 SillySort 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 SillySorted list should be printed in the following order:
5 54 99 974 7 23










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[r1]++;
for (i=r1; i>0; i)
{
if (digit[i]>n)
{
digit[i]=digit[i]%n;
digit[i1]++;
}
}
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
*/ 


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 
Hey varun , i had tried a similar code that you did, but i used recursion and i used a reverse loop from r1 to K1 instead of r1 to 0, may be thats why it wasn;t working proper in general
But short and sweet code man, let me see if i can modify and generate unique numbers. 




