Page 1 of 1

Part A - Intro: These functions will make it possible for you to have access to the values that your program will use wi

Posted: Fri Apr 29, 2022 6:48 am
by answerhappygod
Part A - Intro:
These functions will make it possible for you to have access to
the values that your program will use without typing them in one at
a time. The following is a program that demonstrates this concept.
In PyCharm, create a new file called lab10a.py. Copy and paste this
program into that file and then run it.
The statement
is equivalent to the following statement, but it does not
require the user interaction.
It returns an integer value to your program and stores it in
value. Call this function repeatedly to receive all the
values that your program should process. Eventually,
next_value() will return None to indicate that
there are no more values to read. This is why we call
next_value() immediately before the condition of the
while-loop gets evaluated, once before the while-loop and once at
the end of the while-loop. This way, as soon as
next_value() returns None, we stop the loop. This
is what the book calls a loop-and-a-half.
When you run this program, it should print a list of integers.
For the rest of this lab, you will use these value to test the
programs that you write.
Printing in red
Compare the following program with the one that you just ran.
Copy and paste it into a PyCharm file and see how its output
differs from that of the previous version.
Function number_in_red takes two arguments: an integer
value, which it turns into a string, and a positive integer, which
it uses to format the string. In the example above, the
value that we pass to it gets converted to a string whose
length is 4 (the second argument to num_in_red). The number gets
right justified in the space that is specified by the second
argument. If you were to print the string that this function
return, it will get printed in red.
Part B - Count Inversion:
Part A Intro These Functions Will Make It Possible For You To Have Access To The Values That Your Program Will Use Wi 1
Part A Intro These Functions Will Make It Possible For You To Have Access To The Values That Your Program Will Use Wi 1 (69.94 KiB) Viewed 21 times
Part A Intro These Functions Will Make It Possible For You To Have Access To The Values That Your Program Will Use Wi 2
Part A Intro These Functions Will Make It Possible For You To Have Access To The Values That Your Program Will Use Wi 2 (104.47 KiB) Viewed 21 times
I need help with this python pseudo-code in Part B
so that it can produce the complete output of the listing and
print the inversions in red, please!
For this part of the lab, given a list of numbers, you will write a program that counts and prints the number of inversions in the list. However, before we do that, let's just write a program that prints the numbers on one line, evenly spaced and separated by a pipe-symbol (1). In addition, on a separate line above each value on the list, let's print its index, somewhat similar to how Python-tutor prints lists. Here is an example. 5 6 8 12 13 | | .6 18 131 30 28 | 25 | 22 29 31 5.9 3.0 32 50 64| ; 21 10 40 7 24 11 46 20 10 34 42 14 67 In this example, two lines have been printed, the first line just contains numbers 0 through 14 to index a list that has 15 numbers. Between each pair of pipe-signs (vertical lines), four spaces have been used to print each index. The second line contains the values of some list that has 15 elements. Of course, this list is not sorted. You can use function, rjust(4) to print a given string, right-justified, using 4 spaces. For example: some_number = 25 print( "I", str( some_number ).rjust(4) ) prints a pipe-sign and then right-justifies value 25 within 4 spaces. The syntax might be a bit odd, but it is just a string followed by a period, followed by a function-name. These two statements can be written like this. some_number = 25 number_as_str = str(some_number) print("l", number_as_str.rjust(4) )
Here, we first convert the number to a string and then apply function rjust(4) to it. Now, we are ready to write a program to print the numbers in this fashion and then count the number of inversion. First, let's start with a new file, lab10b.py and copy your code from lab10a.py in it. Then, change the code so that you print the numbers within 4 spaces, separated by pipe-signs, on one line, with a row of indices above it. Once you are able to print the numbers in this way, you should add the necessary code to count the number of inversions in the list. Here is an algorithm.
Recall that numbers.append( value ), where numbers is a list of numbers, adds value to the end of it. The output of your program should 1 11 21 31 41 51 61 71 8 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 291 51 40 100 13138 20 251 241 291 311 541 461 421 501 07 17 76 33 34 35 108 37 138 321 112 15 123 31 138 92 There are 3 versions Do not proceed until you are able to produce this output. Once you are able to duplicate the previous output, you should print the inversions in red. 1121 31 41 51617181 91 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 si 481 180 131 30 281 251 241 291 311 541 461 421 58 671 131 761 331 41 351 1801 371 1101 321 112 151 1231 31 130 921 There are 13 inversions. The complete output is too wide to print here. So, let's look at an abbreviated version of this list so we can discuss it. 1 1 0111 21 31 41 51 61 71 81 51 40 1891 131 30 201 251 241 291 9 10 11 12 13 14 15 31| 541 461 421 501 671 171 In this listing, 13 is in red as it is less than its predecessor, which is 180. The same is true with 20, it is less than 30 and therefore, it has been printed in red. The complete output has 13 inversions and as such, it has 13 numbers printed in red.