Warning: foreach() argument must be of type array|object, bool given in /var/www/html/web/app/themes/studypress-core-theme/template-parts/header/mobile-offcanvas.php on line 20

Problem 16

Write a program that generates a sequence of 20 random values between 0 and 99 in a list, prints the sequence, sorts it, and prints the sorted sequence. Use the list sort method.

Problem 17

Give pseudocode for an algorithm that rotates the elements of a list by one position, moving the initial element to the end of the list, like this:

Problem 18

It is a well-researched fact that men in a rest room generally prefer to maximize their distance from already occupied stalls, by occupying the middle of the longest sequence of unoccupied places. For example, consider the situation where ten stalls are empty. The first visitor will occupy a middle position: The next visitor will be in the middle of the empty area at the left. $$ \text { - } x \text { - } x \text { - - - } $$ Write a program that reads the number of stalls and then prints out diagrams in the format given above when the stalls become flled, one at a time. Hint Use a list of Boolean values to indicate whether a stall is occupied.

Problem 18

Give pscudocode for an algorithm that removes all negative values from a list, preserving the order of the remaining elements.

Problem 19

In this assignment, you will model the game of Bulgarian Solitaire. The game starts with 45 cards. (They need not be playing cards. Unmarked index cards work just as well.) Randomly divide them into some number of piles of random size. For example, you might start with piles of size \(20,5,1,9\), and 10 . In each round, you take one card from cach pile, forming a new pile with these cards. For example, the sample starting configuration would be transformed into piles of size \(19,4,8,9\), and 5 . The solitaire is over when the piles have size \(1,2,3,4,5,6,7,8\), and 9 , in some order. (It can be shown that you always cnd up with such a configuration.) In your program, produce a random starting configuration and print it. Then keep applying the solitaire step and print the result. Stop when the solitaire final configuration is reached.

Problem 19

Suppose values is a sorted list of integers. Give pseudocode that describes how a new value can be inserted in its proper position so that the resulting list stays sorted.

Problem 20

Magic squares. An \(n \times n\) matrix that is filled with the numbers \(1,2,3, \ldots, n^{2}\) is a magic square if the sum of the clements in each row, in cach column, and in the two diagonals is the same value. Write a program that reads in 16 values from the keyboard and tests whether they form a magic square when put into a \(4 \times 4\) table. You need to test two features: 1\. Does each of the numbers \(1,2, \ldots, 16\) occur in the user input? 2\. When the numbers are put into a square, are the sums of the rows, columns, and diagonals equal to each other?

Problem 20

A ran is a sequence of adjacent repeated values. Give pseudocode for computing the length of the longest run in a list. For example, the longest run in the list with elements 12553124322223655631 has length 4 .

Problem 21

What is wrong with the following function that aims to fill a list with random numbers? def fillwithnandonNunbers (values) : nunbers = [] for 1 in range(len(values)) : nunbers[i] - random, randonO

Problem 23

Write a program that reads a sequence of input values and displays a bar chart of the values, using asterisks, like this: You may assume that all values are positive. First figure out the maximum value. That value's bar should be drawn with 40 asterisks. Shorter bars should use proportionally fewer asterisks.

Access millions of textbook solutions in one place

  • Access over 3 million high quality textbook solutions
  • Access our popular flashcard, quiz, mock-exam and notes features
  • Access our smart AI features to upgrade your learning
Get Vaia Premium now
Access millions of textbook solutions in one place

Recommended explanations on Computer Science Textbooks