# The CE Coding Challange

Discussion in 'Computer Science | IT | Networking' started by Sanyam Khurana, Sep 7, 2013.

1. ### avii Star

Engineering Discipline:
Computer Science
Code:
```binstring = '1010101010'
newstring = '1'*(len(binstring) - binstring.count('0')) + '0'* binstring.count('0')```

• Like x 2
2. ### Vishal0203 Ace

Engineering Discipline:
Computer Science
i'd appreciate a solution in C. And, convert the same input string instead of using a new one

3. ### avii Star

Engineering Discipline:
Computer Science
^I don't like C. So used Python. and strings are immutable in python, so its not possible to 'convert' it

• Like x 1
4. ### Vishal0203 Ace

Engineering Discipline:
Computer Science
I know python strings are immutable, I mentioned the conversion for C. Python also provides a bunch of predefined functions which are so good that you don't actually need to think much whereas C makes you think and that is what expected. I would not like to continue the discussion over the strength of programming language. Anyway, let someone else answer it in C!

Engineering Discipline:
Electronics & Communications
Array of binary number. Do you mean like this
Code:
`Array[]="10001101011101" ; `

6. ### durga Star

Engineering Discipline:
Communications
avii's solution was best. but a non-pythonic way of doing it (elaborate).

s = '100110111101'
sn = '' # intiatize a new str
for i in s:
if i =='1':
sn += i​
sn += ('0' * (len(s) - len(sn)))
prints '111111110000'

• Like x 1

Engineering Discipline:
Electronics & Communications
I am not sure if I understand your question properly but here is one lengthy code without thinking too much.

Code:
```void main()
{
unsigned char tuArray[]="1110111";
unsigned char tuArraySize;
unsigned char tuCnt;
unsigned char tuNoOfZero=0;

tuArraySize = strlen(tuArray);
//Findout no of Zero
for(tuCnt=0; tuCnt<tuArraySize; tuCnt++)
{
if(tuArray[tuCnt]== 0x30)
tuNoOfZero++;
}
for(tuCnt=0; tuCnt<(tuArraySize-tuNoOfZero); tuCnt++)
{
tuArray[tuCnt] = '1';
}
for(; tuCnt<tuArraySize; tuCnt++)
{
tuArray[tuCnt] = '0';
}
printf("%s",tuArray);
}```

• Like x 1
#167
Last edited: Jun 17, 2014
8. ### Vishal0203 Ace

Engineering Discipline:
Computer Science
how about converting the same string to the required form?

9. ### Vishal0203 Ace

Engineering Discipline:
Computer Science
don't you think you made it more complicated? it can be done in one pass.. think that way!

Engineering Discipline:
Electronics & Communications
It can be as I said I have not thought too much on that as I am not sure if I have understand your question properly. But Now I will try to get some easy way.

11. ### durga Star

Engineering Discipline:
Communications
not possible. That of code was made in python

12. ### Vishal0203 Ace

Engineering Discipline:
Computer Science
Oh my god! I'll stop commenting

13. ### SurEC93 Rookie

Engineering Discipline:
Electronics & Communications
Hello all! The new version of forum is cool

It's been a while I have practiced these kind of c programs.
Below is one that I was trying.
printing magic numbers: a magic no is a no that is same as the sum of factorials of its individual digits.
(eg. 145= 1!+4!+5!)

Code:
```void main()
{
long int x,n,p,a[10],c,temp,q,sum,s[10];
clrscr();

for(x=1;x<=100;x++)            // checking for no.s from 1 to 100
{
c=0;
sum=0;
p=x;
temp=x;
while(temp!=0)            // to find no. of digits in no
{
temp=temp/10;
c++;
}

for(n=1;n<=c;n++)
{
a[n]=x%10;
x=x/10;                // separating the digits
}

for(n=c;n>0;n--)
{
s[n]=1;
for(q=a[n];q>0;q--)        // eg 5 then it will run 5 4 3 2 1
{
s[n]=s[n]*q;        // finding factorial
}
sum=sum+s[n];            // taking sum of factoials eg. 1!+9!+5! for 195
}

if(p==sum)                // checking criteria
{
printf("%d \n",p);
}
}
}```
Can't get what's wrong. Plz figure out.

14. ### Vishal0203 Ace

Engineering Discipline:
Computer Science
That's a very complicated code you've written and hence it doesn't interest me going through it.. Please divide your program in functions where it gets easy to go through (I haven't seen the code)

Engineering Discipline:
Electronics & Communications
Please share the solution for the previous question. I have not got chance to spend time on that but now I think it is time to post the solution.

Engineering Discipline:
Electronics & Communications
@Vishal0203 will you please share your solution for that 0-1 swapping code.

17. ### Vishal0203 Ace

Engineering Discipline:
Computer Science
I'll lay the method here,
Keep 2 pointers, 1 at the start and 1 at the end.
compare, swap and increment/decrement (increment 1st pointer, decrement 2nd pointer) the pointers accordingly.
This continues until 1st and 2nd pointers meet

18. ### durga Star

Engineering Discipline:
Communications
that looks like a variation of quick sort to me, being language specific(python) ,, swapping a within a list is possible, that would need the str to be converted to a list though, I still don't know if the same string can be skimmed that way and be swapped, other python-enthusiasts might chip in.

19. ### Vishal0203 Ace

Engineering Discipline:
Computer Science
According to me python makes life easy,
And so coding these small problems in python doesn't improve thinking skills

20. ### durga Star

Engineering Discipline:
Communications
I would disagree. Python has many inbuilt functions which makes life easy, but that does-not in anyway deter you from full-filling the requirement of your project. It helps in abstracting all complexities of a language and thus aiding in the user to focus on real problem and not language nauseas.
for example - python network programming gives you the flexibility to code at a higher application layer or as low as socket layer, in sense it gives more choice of usage. Anyways, each to their own.