viernes, 19 de noviembre de 2021

JAVA Ejercicios de Arreglos

 JAVA Ejercicios de Arreglos

https://introcs.cs.princeton.edu/java/14array/

Exercises

  1. Describe and explain what happens when you try to compile a program HugeArray.java with the following statement:
    int n = 1000;
    int[] a = new int[n*n*n*n];
    
  2. Write a code fragment that reverses the order of values in a one-dimensional string array. Do not create another array to hold the result. Hint: Use the code in the text for exchanging two elements.

    Solution.

    int n = a.length;
    for (int i = 0; i < n/2; i++) {
        String temp = a[n-i-1];
        a[n-i-1] = a[i];
        a[i] = temp;
    }
    
  3. What is wrong with the following code fragment?
    int[] a;
    for (int i = 0; i < 10; i++)
       a[i] = i * i;
    

    Solution: It does not allocate memory for a[] with new. The code results in a variable might not have been initialized compile-time error.

  4. What does the following code fragment print?
    int[] a = { 1, 2, 3 };
    int[] b = { 1, 2, 3 };
    System.out.println(a == b);
    
    Solution: It prints false. The == operator compares whether the (memory addresses of the) two arrays are identical, not whether their corresponding values are equal.
  5. Write a program Deal.java that takes an integer command-line argument n and prints n poker hands (five cards each) from a shuffled deck, separated by blank lines.
  6. Write a program HowMany.java that takes a variable number of command-line arguments and prints how many there are.
  7. Write a program DiscreteDistribution.java that takes a variable number of integer command-line arguments and prints the integer i with probability proportional to the ith command-line argument.
  8. Write a code fragment Transpose.java to transpose a square two-dimensional array in place without creating a second array.

Creative Exercises

  1. Bad shuffling. Suppose that you choose a random integer between 0 and n-1 in our shuffling code instead of one between i and n-1. Show that the resulting order is not equally likely to be one of the n! possibilities. Run the test of the previous exercise for this version.

    Partial solution: when n = 3, all 3! = 6 outcomes are possible, but some are more likely:

    ABCACBBACBCACABCBA
    4/275/276/274/275/273/27

    Here's what happened to PlanetPoker when they used a broken shuffling algorithm that could only generate only about 200,000 of the possible 52! shuffles.

  2. Inverse permutation. Write a program InversePermutation.java that reads in a permutation of the integers 0 to n-1 from n command-line arguments and prints the inverse permutation. (If the permutation is in an array a[], its inverse is the array b[] such that a[b[i]] = b[a[i]] = i.) Be sure to check that the input is a valid permutation.
  3. Hadamard matrix. The n-by-n Hadamard H(n) matrix is a boolean matrix with the remarkable property that any two rows differ in exactly n/2 bits. (This property makes it useful for designing error-correcting codes.) H(1) is a 1-by-1 matrix with the single entry true, and for n > 1, H(2n) is obtained by aligning four copies of H(n) in a large square, and then inverting all of the entries in the lower right n-by-n copy, as shown in the following examples (with T representing true and F representing false, as usual).
    H(1)  H(2)    H(4)
    -------------------
     T    T T   T T T T
          T 0   T 0 T 0
                T T 0 0
                T 0 0 T 
    
    Write a program Hadamard.java that takes one command-line argument n and prints H(n). Assume that n is a power of 2.
  4. Random walkers. Suppose that n random walkers, starting in the center of an n-by-n grid, move one step at a time, choosing to go left, right, up, or down with equal probability at each step. Write a program RandomWalkers.java to help formulate and test a hypothesis about the number of steps taken before all cells are touched.
  5. Birthday problem. Suppose that people enter an empty room until a pair of people share a birthday. On average, how many people will have to enter before there is a match? Write a program Birthday.java to simulate one experiment. Write a program Birthdays.java to repeat the experiment many times and estimate the average value. Assume birthdays to be uniform random integers between 0 and 364.
  6. Binomial coefficients. Write a program BinomialDistribution.java that builds and prints a two-dimensional ragged array a such that a[n][k] contains the probability that you get exactly k heads when you toss a coin n times. Take a command-line argument to specify the maximum value of n. These numbers are known as the binomial distribution: if you multiply each entry in row i by 2^n, you get the binomial coefficients—the coefficients of x^k in (x+1)^n—arranged in Pascal's triangle. To compute them, start with a[n][0] = 0.0 for all n and a[1][1] = 1.0, then compute values in successive rows, left to right, with a[n][k] = (a[n-1][k] + a[n-1][k-1]) / 2.
    Pascal's triangle   Binomial distribution
    --------------------------------------------
    1                   1 
    1 1                 1/2  1/2 
    1 2 1               1/4  1/2  1/4 
    1 3 3 1             1/8  3/8  3/8  1/8 
    1 4 6 4 1           1/16 1/4  3/8  1/4  1/16
    


Web Exercises

  1. Birthday problem. Modify Birthday.java so that it compute the probability that two people have a birthday within a day of each other.
  2. Above average. 90% of incoming college students rate themselves as above average. Write a program AboveAverage.java that takes a command-line argument n, reads in n integers from standard input, and prints the fraction of values that are strictly above the average value.
  3. Random permutation. Write a program Permutation.java so that it takes a command-line argument N and prints a random permutation of the integers 0 through N-1. Also print a checkerboard visualization of the permutation. As an example, the permutation { 4, 1, 3, 0, 2 } corresponds to:
    4 1 3 0 2
    * * * Q * 
    * Q * * * 
    * * * * Q 
    * * Q * * 
    Q * * * * 
    
  4. 8 queens checker. A permutation of the integer 0 to n-1 corresponds to a placement of queens on an n-by-n chessboard so that no two queens are in the same row or column. Write a program QueensChecker.java that determines whether or not a permutation corresponds to a placement of queens so that no two are in the same row, column, or diagonal. As an example, the permutation { 4, 1, 3, 0, 2 } is a legal placement:
    * * * Q * 
    * Q * * * 
    * * * * Q 
    * * Q * * 
    Q * * * * 
    

    Try to do it without using any extra arrays besides the length n input permutation qHint: to determine whether setting q[i] conflicts with q[j] for i < j.

    • if q[i] equals q[j]: two queens are placed in the same row
    • if q[i] - q[j] equals j - i: two queens are on same major diagonal
    • if q[j] - q[i] equals j - i: two queens are on same minor diagonal
  5. Finding your beer. A large number of college students are attending a party. Each guest is drinking a can of beer (or soda of they are under 21). An emergency causes the lights to go out and the fire alarm to go off. The guests calmly put down their beer and exit the building. When the alarm goes off, they re-enter and try to retrieve their beer. However, the lights are still off, so each student randomly grabs a bottle of beer. What are the chances that at least one student gets his or her original beer? Write a program MyBeer.java that takes a command-line argument n and runs 1,000 simulations this event, assuming their are n guests. Print the fraction of times that at least one guest gets their original beer. As n gets large, does this fraction approach 0 or 1 or something in between?
  6. Linear feedback shift register. Rewrite linear feedback shift register from Chapter 1 by using an array to streamline it and makes it more extensible, e.g., if the number of cells in the shift register increases. Program LFSR.java uses a boolean Hint: use the ^ operator to take the exclusive or of two boolean values.
  7. Lockers. Your are in a locker room with 100 open lockers, numbered 1 to 100. Toggle all of the lockers that are even. By toggle, we mean close if it is open, and open if it is closed. Now toggle all of the lockers that are multiples of three. Repeat with multiples of 4, 5, up to 100. How many lockers are open? Answer: lockers 1, 4, 9, 16, 25, ..., 100 will be open. Guess you don't need an array once you see the pattern.
  8. Scheduling with deadline. Suppose that you have N tasks to schedule. Each task takes 1 unit of time and has a deadline by which time it is expected to finish. If a task is not completed by its deadline, you pay a $1,000 fine. Find a schedule that minimizes the penalty. Hint: schedule the tasks in order of their deadline, but don't bother with any task that won't finish by its deadline.
  9. Calendar. Repeat Exercise 1.33 to produce a calendar for a given month and year. Use arrays to store the names of the days of the week, the names of the months, and the number of days in a month.
  10. Connect Four. Given an N-by-N grid with each cell either occupied by an 'X', an 'O', or empty, write a program to find the longest sequence of consecutive 'X's either horizontal, vertically, or diagonally. To test your program, you can create a random grid where each cell contains an 'X' or 'O' with probability 1/3.
  11. Thai kickboxing. Write a program KickBoxer.java that takes an integer weight w as a command line input and prints the corresponding kickboxing weight-class according to the table below.
    weight class              from    to
    ------------------------------------
    Fly Weight                   0   112
    Super Fly Weight           112   115
    Bantam Weight",            115   118
    Super Bantam Weight        118   122
    Feather Weight             122   126
    Super Feather Weight       126   130
    Light Weight               130   135
    Super Light Weight         135   140
    Welter Weight              140   147
    Super Welter Weight        147   154
    Middle Weight              154   160
    Super Middle Weight        160   167
    Light Heavy Weight         167   174
    Super Light Heavy Weight   174   183
    Cruiser Weight             183   189
    Super Cruiser Weight       189   198
    Heavy Weight               198   209
    Super Heavy Weight         209
    
    Use an integer array to store the weight limits and a string array to store the weight categories (ranging from Flyweight to Super Heavyweight).
  12. N-ary counter. Write a program that counts in base N from 0 to N20 - 1. Use an array of 20 elements.
  13. Terrain analysis. Given an N-by-N grid of elevation values (in meters), a peak is a grid point for which all four neighboring cells are strictly lower. Write a code fragment that counts the number of peaks in a given N-by-N grid.
  14. Magic squares. Write a program MagicSquare.java that reads in an odd integer N from the command line and prints out an N-by-N magic square. The square contains each of the integers between 1 and N^2 exactly once, such that all row sums, column sums, and diagonal sums are equal.
    4  9  2    11 18 25  2  9
    3  5  7    10 12 19 21  3
    8  1  6     4  6 13 20 22
               23  5  7 14 16
               17 24  1  8 15
    

    One simple algorithm is to assign the integers 1 to N^2 in ascending order, starting at the bottom, middle cell. Repeatedly assign the next integer to the cell adjacent diagonally to the right and down. If this cell has already been assigned another integer, instead use the cell adjacently above. Use wrap-around to handle border cases.

  15. Banner. Write a program Banner.java that takes a string as a command line argument and prints the string in large letters as below.
    % java Banner "Kevin"
     #    #  ######  #    #     #    #    #
     #   #   #       #    #     #    ##   #
     ####    #####   #    #     #    # #  #
     #  #    #       #    #     #    #  # #
     #   #   #        #  #      #    #   ##
     #    #  ######    ##       #    #    #
    
    Mimics the Unix utility banner.
  16. Voting and social choice theory. Plurality (US presidential election), run-off elections, sequential run-off elections (Australia, Ireland, Princeton faculty committees), Condorcet. Kemeny rank aggregation. Arrow's impossibility theorem. Same ideas for sports, google, meta-search, machine learning
  17. Borda count. In 1781, Borda proposed a positional method for determining the outcome of a political election with K voters and N candidates. Each voter ranks the candidates in increasing order of preference (from 1 to N). Borda's method assigns a score to each candidate equal to the sum of their rankings. The candidate with the highest sum wins. This is used in Major League Baseball to determine the MVP.
  18. Kendall's tau distance. Given two permutations, Kendall's tau distance is the number of pairs out of position. "Bubblesort metric." Useful in top-k lists. Optimal Kemeny rank aggregation in voting theory minimizes Kendall tau distance. Also useful for ranking genes using several expression profiles, ranking search engine results, etc.
  19. Spearman's footrule distance. Given two permutations, Spearman's footrule distance is the L1 distance between the permutations as vectors. Useful in top-k lists.
    int footrule = 0;
    for (int i = 0; i < N; i++)
        footrule = footrule + Math.abs(p[i] - q[i]);
    
  20. US postal barcodes. The POSTNET barcode is used by the US Postal System to route mail. Each decimal digit in the zip code is encoded using a sequence of 5 short and long lines for use by scanners as follows:
    VALUEENCODING
    0||╷╷╷
    1╷╷╷||
    2╷╷|╷|
    3╷╷||╷
    4╷|╷╷|
    5╷|╷|╷
    6╷||╷╷
    7|╷╷╷|
    8|╷╷|╷
    9|╷|╷╷

    A sixth checksum digit is appended: it is computed by summing up the original five digits mod 10. In addition, a long line is added to the beginning and appended to the end. Write a program ZipBarCoder.java that reads in a five digit zip code as the command line parameter and prints the corresponding postal barcode. Print the code vertically instead of horizontally, e.g, the following encodes 08540 (with the check digit of 7).

    *****
    *****
    *****
    **
    **
    **
    *****
    **
    **
    *****
    **
    **
    *****
    **
    *****
    **
    **
    *****
    **
    **
    *****
    *****
    *****
    **
    **
    **
    *****
    **
    **
    **
    *****
    *****
    
  21. US postal barcodes. Repeat the previous exercise, but plot the output using Turtle graphics.
  22. Gaps with no primes. Find the longest consecutive sequence of integers with no primes. Write a program PrimeGap.java that takes a command line parameter N and prints the largest block of integers between 2 and N with no primes.
  23. Goldbach conjecture. In 1742, Christian Goldbach conjectured that every even number greater than 2 could be written as the sum of two primes. For example, 16 = 3 + 13. Write a program Goldbach.java that takes one command line parameter N and expresses N as the sum of two primes. Goldbach's conjecture is still unresolved, but it is known to be true for all N < 1014.
  24. Minima in permutations. Write a program that takes an integer n from the command line, generates a random permutation, prints the permutation, and prints the number of left-to-right minima in the permutation (the number of times an element is the smallest seen so far). Then write a program that takes integers m and n from the command line, generates m random permutations of length n, and prints the average number of left-to-right minima in the permutations generated. Extra credit: Formulate a hypothesis about the number of left-to-right minima in a permutation of length n, as a function of n.
  25. In-place inverse permutation. Redo Exercise 1.4.25, but compute the permutation in-place, i.e., do not allocate a second array for the inverse permutation. Caveat: this is hard.
  26. Most likely roll. Alice and Bob are in a heated argument about whether if they repeatedly roll a die until the sum is more than 12, is 13 the most likely sum? Write a program MostLikelyRoll.java to simulate the process a million times and produce a table of the fraction of times the sum is 13, 14, 15, 16, 17, and 18.
  27. Spiraling 2-D array. Given a 2-D array, write a program Spiral.java to print it out in spiral order.
     1  2  3  4
     5  6  7  8
     9 10 11 12
    13 14 15 16
    
    1 2 3 4 8 12 16 15 14 13 9 5 6 7 11 10
    

  28. Sudoko verifier. Given a 9-by-9 array of integers between 1 and 9, check if it is a valid solution to a Sudoku puzzle: each row, column, and block should contain the 9 integers exactly once.
     5 3 4 | 6 7 8 | 9 1 2 
     6 7 2 | 1 9 5 | 3 4 8 
     1 9 8 | 3 4 2 | 5 6 7
    -------+-------+------ 
     8 5 9 | 7 6 1 | 4 2 3 
     4 2 6 | 8 5 3 | 7 9 1 
     7 1 3 | 9 2 4 | 8 5 6 
    -------+-------+------ 
     9 6 1 | 5 3 7 | 2 8 4 
     2 8 7 | 4 1 9 | 6 3 5 
     3 4 5 | 2 8 6 | 1 7 9
    
  29. Sum of powers conjecture. Redo Exercise 1.3.x, but precompute the 5th powers of all relevant integers. Evaluate how much time this saves. The program Euler.java searches for integral solutions to a5 + b5 + c5 + d5= e5.
  30. Haar wavelet transform. Given, an array a[] of length 2^n, its 1D Haar transform is obtained as follows: Compute the average and difference of a[2i] and a[2i+1], and compute the array of the same length containing the averages, followed by the differences. Then apply the same technique to the averages (the first 2^n-1 entries) and so on. An example with 2^3 entries is shown below.
     448  768  704  640 1280 1408 1600 1600  (original)
     608  672 1344 1600 -160   32  -64    0  (step 1)
     640 1472  -32 -128 -160   32  -64    0  (step 2)
    1056 -416  -32 -128 -160   32  -64    0  (step 3)
    
    The 2D Haar wavelet transform of a 2^n-by-2^n matrix, is obtained by applying the Haar wavelet transform to each row, and then to each column. The Haar wavelet transform is useful in signal processing, medical imaging, and data compression.
  31. What happens when you try to compile a program with the following statement?
    int[] a = new int[-17];
    
    It compiles cleanly, but throws a java.lang.NegativeArraySizeException when you execute it.
  32. Blackjack. Write a program Blackjack.java that takes three command line integers x, y, and z representing your two blackjack cards x and y, and the dealer's face-up card z, and prints the "standard strategy" for a 6 card deck in Atlantic city. Assume that x, y, and z are integers between 1 and 10, representing an ace through a face card. Report whether the player should hit, stand, or split according to these strategy tables. Encode the strategy tables using three 2-D boolean arrays.

    Modify Blackjack.java to allow doubling.

  33. Boltzmann distribution. Here's a simple model to approximate the Boltzmann distribution from statistical physics: generate 100 random integers between 1 and 10. If the sum is exactly 200 keep this trial. Repeat this process until you get 1,000 trials that meet the criterion. Now plot a histogram of the number of times each of the 10 integers occurs.
  34. Doubly stochastic. Write a program to read in an N-by-N matrix of real numbers and print true if the matrix is doubly stochastic, and false otherwise. A matrix is stochastic if all of the row and column sums are 1. Since you are dealing with floating point numbers, allow the sums to be between 1 - ε and 1 + ε where ε= 0.000000001.
  35. Suppose that b[] is an array of 100 elements, with all entries initialized to 0, and that a[] is an array of N elements, each of which is an integer between 0 and 99. What is the effect of the following loop?

    for (j = 0; j < N; j++)
       b[a[j]]++;
    
  36. Modify RandomStudent.java so that it stores a parallel array of type boolean named isFemale, where element i is true if student i is female and false otherwise. Now, print one male student at random and one female student at random. Hint: use a do-while loop to generate random integers until you get one that indexes a male student.
  37. Which of the following require using arrays. For each, the input comes from standard input and consists of N real numbers between 0.0 and 1.0.
    1. Print the maximum element.
    2. Print the maximum and minimum elements.
    3. Print the median element.
    4. Print the element that occurs most frequently.
    5. Print the sum of the squares of the elements.
    6. Print the average of the N elements.
    7. Print the element closest to 0.
    8. Print all the numbers greater than the average.
    9. Print the N elements in increasing order.
    10. Print the N elements in random order.
    11. Print histogram (with, say 10 bins of size 0.1).
  38. Write a program Yahtzee.java that simulates the rolling of five dice and prints "Yahtzee" if all five dice are the same; otherwise it should print "Try again."
  39. Modify DayOfWeek.java so that it reads in a date and print which day of the week that date falls on. Your program should take three command line arguments, M (month), D (day), and Y (year). Do not use any if-else statements; instead use a string array consisting of the names of the 7 days of the week.
  40. Write a program Pascal.java to compute Pascal's triangle using a ragged array.
  41. Zero out matrix rows and columns. Given an m-by-n integer matrix a[][], if a[i][j] is 0, set row i and column j to 0. Do not use any extra arrays.

    Solution. First, check whether row 0 has a 0 and whether column 0 has a 0; record this information in two boolean variables. Next, for each element a[i][j] that is 0, set element a[i][0] and a[0][j] to 0. Finally, set a[i][j] to 0 if either a[i][0] or a[0][j].

No hay comentarios:

Publicar un comentario