Given a pattern of O's and 7's, can you move all the I's to
the beginning and 0's to the end?
Sample Test Case
Input
010101011001
Output
111111000000
Answers
Required Answer:-
Question:
- Given a pattern of 0s and 1s, move the 1s to the beginning and 0s to the end.
Solution:
Here comes the program.
1. In Python.
n=input("Enter a number: ")
print("Number: ",n)
n="".join(sorted(n))[::-1]
print("Number after modification: ",n)
Logic: Enter the number in string format. Sort the string. Find the reverse of the string and display it.
2. In Java.
import java.util.*;
public class Java {
public static void main(String[] args) {
String s;
int i;
Scanner sc=new Scanner(System.in);
System.out.print("Enter a number: ");
s=sc.nextLine();
System.out.println("Given Number: "+s);
char x[]=s.toCharArray();
Arrays.sort(x);
s="";
for(i=x.length-1;i>=0;i--)
s+=x[i];
System.out.println("Number after modifications: "+s);
sc.close();
}
}
Logic: Ask the user to enter the number in the form of string. Create a character array which contains all the characters of the string. Sort the array using Arrays.sort() function. Arrays.sort() function sorts the list in ascending order. We will then find the reverse of the array. Convert the array of characters to string and display the result.
See the attachment for output ☑.