Problem 1 : Print each word from given the Sentence in random Format :
Example : "The Hungry Dog Jumped Over The Hungry Fox And Killed It"
Java Solution:
// save this code into the file with name: Main.java
import java.util.LinkedHashSet; import java.util.Random; import java.util.Set; public class Main { public static void main(final String[] args) throws Exception { Random randNum = new Random(); Set<Integer>set = new LinkedHashSet<Integer>(); String s2 = "The Hungry Dog Jumped Over The Hungry Fox And Killed It"; String [] s3 = s2.split("\\s"); int String_Length=s3.length; while (set.size() < String_Length) { set.add(randNum.nextInt(String_Length)); } //System.out.println("Random numbers with no duplicates = "+set); //int i; //for (i = 0; i < s3.length; i++) //System.out.print(s3[i]+" "); for (int s : set) System.out.print(s3[s]+" "); } }
Output:
Hungry Fox It Hungry The And Dog Jumped Over Killed The
Python Solution:
import randomstring="The Hungry Dog Jumped Over The Hungry Fox And Killed It".split(" ")length=len(string)random_index=random.sample(range(0,length), length)output= [string[index] for index in random_index]#print( [string[i] for i in random_index])#print(*output, sep = "\n")#print(' '.join(map(str, output)))print(*output)
Output:
The Hungry The It Jumped And Killed Fox Dog Hungry Over