테스트 케이스 생성 | 세트 5(임의로 정렬된 배열 및 회문 생성)
무작위 생성 정렬된 배열 무작위 배열 요소를 배열에 저장한 다음 이를 정렬하고 인쇄합니다.
CPP // A C++ Program to generate test cases for // array filled with random numbers #include 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 & lt ; = RUN ; i ++ ) { int arr [ MAXNUM ]; NUM = 1 + rand () % MAXNUM ; // First print the number of array elements printf ( & quot ; % d n & quot ; NUM ); // Then print the array elements separated by // space for ( int j = 0 ; j & lt ; 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 & lt ; NUM ; j ++ ) printf ( & quot ; % d & quot ; arr [ j ]); printf ( & quot ; n & quot ;); } // Uncomment the below line to store // the test data in a file // fclose(stdout); return ( 0 ); }
Java import java.util.Arrays ; import java.util.Random ; public class TestCasesGenerator { // Define the number of runs for the test data generated static final int RUN = 5 ; // Define the range of the test data generated static final int MAX = 100000 ; // Define the maximum number of array elements static final int MAXNUM = 100 ; public static void main ( String [] args ) { // Uncomment the below line to store // the test data in a file // System.setOut(new PrintStream(new // FileOutputStream('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 . nextInt ( MAXNUM ); // First print the number of array elements System . out . println ( NUM ); // Then print the array elements separated by space for ( int j = 0 ; j < NUM ; j ++ ) arr [ j ] = rand . nextInt ( MAX ); // Sort the generated random array Arrays . sort ( arr 0 NUM ); // Print the sorted random array for ( int j = 0 ; j < NUM ; j ++ ) System . out . print ( arr [ j ] + ' ' ); System . out . println (); } // Uncomment the below line to store // the test data in a file // System.out.close(); } }
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(); } } }
JavaScript // Define the number of runs for the test data generated const RUN = 5 ; // Define the range of the test data generated const MAX = 100000 ; // Define the maximum number of array elements const MAXNUM = 100 ; // Function to generate random integer within a range function getRandomInt ( min max ) { return Math . floor ( Math . random () * ( max - min + 1 )) + min ; } for ( let i = 1 ; i <= RUN ; i ++ ) { const NUM = 1 + getRandomInt ( 0 MAXNUM ); const arr = []; // First print the number of array elements console . log ( NUM ); // Then print the array elements separated by space for ( let j = 0 ; j < NUM ; j ++ ) { arr . push ( getRandomInt ( 0 MAX )); } // Sort the generated random array arr . sort (( a b ) => a - b ); // Print the sorted random array console . log ( arr . join ( ' ' )); }
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 ))
시간 복잡도 : O(N log N)
공간 복잡도 : 에)
무작위 생성 회문
- 테스트 케이스 생성 계획은 홀수 및 짝수 길이의 회문을 생성합니다.
- 테스트 케이스 생성 계획은 가장 과소평가된 데이터 구조 중 하나를 사용합니다. 그리고
- 회문은 왼쪽과 오른쪽에서 동일하게 읽히므로 왼쪽 양쪽에 동일한 임의의 문자를 넣기만 하면 됩니다(다음을 사용하여 완료). push_front() ) 및 오른쪽 (다음을 사용하여 수행됨) push_back( ))
// A C++ Program to generate test cases for // random strings #include 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 ); }
Java import java.util.Random ; public class PalindromeGenerator { // Define the number of runs for the test data generated private static final int RUN = 5 ; // Define the range of the test data generated // Here it is 'a' to 'z' private static final int MAX = 25 ; // Define the maximum length of string private static final int MAXLEN = 50 ; // A function to generate palindromic strings private static void generatePalindrome () { // Length of string int LEN = 1 + new Random (). nextInt ( MAXLEN ); // Print the length of the string System . out . println ( LEN ); // If it is an odd-length palindrome if ( LEN % 2 == 1 ) { System . out . print (( char ) ( 'a' + new Random (). nextInt ( MAX ))); } // Then print the characters of the palindromic string for ( int j = 0 ; j < LEN / 2 ; j ++ ) { char ch = ( char ) ( 'a' + new Random (). nextInt ( MAX )); System . out . print ( ch ); System . out . print ( ch ); } System . out . println (); } public static void main ( String [] args ) { // Set the seed for reproducibility (optional in Java) // No direct equivalent to Python's random.seed() in Java // but you can use Random instance with a seed // Generate palindromic strings for ( int i = 0 ; i < RUN ; i ++ ) { generatePalindrome (); } } }
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(); } } }
JavaScript // JavaScript Program to generate test cases for // random strings // Define the number of runs for the test data // generated const RUN = 5 ; // Define the range of the test data generated // Here it is 'a' to 'z' const MAX = 25 ; // Define the maximum length of string const MAXLEN = 50 ; function main () { // A container for storing the palindromes let container = []; for ( let i = 1 ; i <= RUN ; i ++ ) { let LEN = 1 + Math . floor ( Math . random () * MAXLEN ); // First print the length of string console . log ( LEN ); // If it is an odd-length palindrome if ( LEN % 2 ) container . push ( String . fromCharCode ( 'a' . charCodeAt ( 0 ) + Math . floor ( Math . random () * MAX ))); // Then print the characters of the palindromic // string for ( let j = 1 ; j <= LEN / 2 ; j ++ ) { let ch = String . fromCharCode ( 'a' . charCodeAt ( 0 ) + Math . floor ( Math . random () * MAX )); container . push ( ch ); container . unshift ( ch ); } // Print the characters of the palindromic string console . log ( container . join ( '' )); // Clear the container for the next run container = []; } } // Call the main function main ();
Python3 import random # Define the number of runs for the test data generated RUN = 5 # Define the range of the test data generated # Here it is 'a' to 'z' MAX = 25 # Define the maximum length of string MAXLEN = 50 # A function to generate palindromic strings def generate_palindrome (): # Length of string LEN = 1 + random . randint ( 0 MAXLEN ) # Print the length of the string print ( LEN ) # If it is an odd-length palindrome if LEN % 2 : print ( chr ( ord ( 'a' ) + random . randint ( 0 MAX )) end = '' ) # Then print the characters of the palindromic string for _ in range ( LEN // 2 ): ch = chr ( ord ( 'a' ) + random . randint ( 0 MAX )) print ( ch end = '' ) print ( ch end = '' ) print () # Set the seed for reproducibility random . seed () # Generate palindromic strings for i in range ( 1 RUN + 1 ): generate_palindrome ()
시간 복잡도 : 에)
공간 복잡도 : 에)
GeeksforGeeks를 좋아하고 기여하고 싶다면 다음을 사용하여 기사를 작성할 수도 있습니다. write.geeksforgeeks.org 또는 기사를 [email protected]로 우편으로 보내세요. GeeksforGeeks 메인 페이지에 나타나는 기사를 보고 다른 Geeks를 도와주세요. 참고자료 : - http://spojtoolkit.com/TestCaseGenerator/ 잘못된 내용을 발견했거나 위에서 논의한 주제에 대해 더 많은 정보를 공유하고 싶다면 의견을 작성해 주세요.