Generating Random Sorted Arrays We store the random array elements in an array and then sort it and print it.
CPP
// A C++ Program to generate test cases for // array filled with random numbers #include<bits/stdc++.h> using namespace std; // Define the number of runs for the test data // generated #define RUN 5 // Define the range of the test data generated #define MAX 100000 // Define the maximum number of array elements #define MAXNUM 100 int main() { // Uncomment the below line to store // the test data in a file // freopen("Test_Cases_Random_Sorted_Array.in", // "w", stdout); // For random values every time srand ( time (NULL)); int NUM; // Number of array elements for ( int i=1; i<=RUN; i++) { int arr[MAXNUM]; NUM = 1 + rand () % MAXNUM; // First print the number of array elements printf ("%d\n", NUM); // Then print the array elements separated by // space for ( int j=0; j<NUM; j++) arr[j] = rand () % MAX; // Sort the generated random array sort (arr, arr + NUM); // Print the sorted random array for ( int j=0; j<NUM; j++) printf ("%d ", arr[j]); printf ("\n"); } // Uncomment the below line to store // the test data in a file // fclose(stdout); return (0); } |
C#
// A C# Program to generate test cases for // array filled with random numbers using System; namespace TestCasesGenerator { class Program { // Define the number of runs for the test data // generated const int RUN = 5; // Define the range of the test data generated const int MAX = 100000; // Define the maximum number of array elements const int MAXNUM = 100; static void Main( string [] args) { // Uncomment the below line to store // the test data in a file // Console.SetOut(new // System.IO.StreamWriter("Test_Cases_Random_Sorted_Array.txt")); // For random values every time Random rand = new Random(); int NUM; // Number of array elements for ( int i = 1; i <= RUN; i++) { int [] arr = new int [MAXNUM]; NUM = 1 + rand.Next() % MAXNUM; // First print the number of array elements Console.WriteLine(NUM); // Then print the array elements separated by // space for ( int j = 0; j < NUM; j++) arr[j] = rand.Next() % MAX; // Sort the generated random array Array.Sort(arr, 0, NUM); // Print the sorted random array for ( int j = 0; j < NUM; j++) Console.Write(arr[j] + " " ); Console.WriteLine(); } // Uncomment the below line to store // the test data in a file // Console.Out.Close(); } } } |
Python3
import random # Define the number of runs for the test data generated RUN = 5 # Define the range of the test data generated MAX = 100000 # Define the maximum number of array elements MAXNUM = 100 # For random values every time rand = random.Random() for i in range ( 1 , RUN + 1 ): arr = [] NUM = 1 + rand.randint( 0 , MAXNUM - 1 ) # First print the number of array elements print (NUM) # Then print the array elements separated by space for j in range (NUM): arr.append(rand.randint( 0 , MAX - 1 )) # Sort the generated random array arr.sort() # Print the sorted random array print ( " " .join( str (x) for x in arr)) |
Time complexity : O(N log N)
Space complexity : O(N)
Generating Random Palindromes
- The test case generation plan generates odd as well as even length palindromes.
- The test case generation plan uses one of the most under-rated data structure- Deque
- Since a palindrome is read the same from left as well as right so we simply put the same random characters on both the left side (done using push_front()) and the right side (done using push_back())
CPP
// A C++ Program to generate test cases for // random strings #include<bits/stdc++.h> using namespace std; // Define the number of runs for the test data // generated #define RUN 5 // Define the range of the test data generated // Here it is 'a' to 'z' #define MAX 25 // Define the maximum length of string #define MAXLEN 50 int main() { // Uncomment the below line to store // the test data in a file // freopen("Test_Cases_Palindrome.in", "w", // stdout); // For random values every time srand ( time (NULL)); // A container for storing the palindromes deque< char > container; deque< char >::iterator it; int LEN; // Length of string for ( int i=1; i<=RUN; i++) { LEN = 1 + rand () % MAXLEN; // First print the length of string printf ("%d\n", LEN); // If it is an odd-length palindrome if (LEN % 2) container.push_back( 'a' + rand () % MAX); // Then print the characters of the palindromic // string for ( int j=1; j<=LEN/2; j++) { char ch = 'a' + rand () % MAX; container.push_back(ch); container.push_front(ch); } for (it=container.begin(); it!=container.end(); ++it) printf ("%c",*it); container.clear(); printf ("\n"); } // Uncomment the below line to store // the test data in a file // fclose(stdout); return (0); } |
C#
// A C# program to generate test cases for // random strings using System; using System.Collections.Generic; namespace Test_Cases_Random_Strings { class Program { // Define the number of runs for the test data // generated const int RUN = 5; // Define the range of the test data generated // Here it is 'a' to 'z' const int MAX = 25; // Define the maximum length of string const int MAXLEN = 50; static void Main( string [] args) { // Uncomment the below line to store // the test data in a file // Console.SetOut(new // System.IO.StreamWriter("Test_Cases_Palindrome.in")); // For random values every time Random rand = new Random(); // A container for storing the palindromes LinkedList< char > container = new LinkedList< char >(); int LEN; // Length of string for ( int i = 1; i <= RUN; i++) { LEN = 1 + rand.Next(MAXLEN); // First print the length of string Console.WriteLine(LEN); // If it is an odd-length palindrome if (LEN % 2 == 1) container.AddLast( ( char )( 'a' + rand.Next(MAX))); // Then print the characters of the palindromic // string for ( int j = 1; j <= LEN / 2; j++) { char ch = ( char )( 'a' + rand.Next(MAX)); container.AddLast(ch); container.AddFirst(ch); } foreach ( char ch in container) Console.Write(ch); container.Clear(); Console.WriteLine(); } // Uncomment the below line to store // the test data in a file // Console.Out.Close(); } } } |
Time complexity : O(N)
Space complexity : O(N)
This article is contributed by Rachit Belwariar. If you like neveropen and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the neveropen main page and help other Geeks. References : – http://spojtoolkit.com/TestCaseGenerator/ Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!