Question

In: Computer Science

Using PyCharm, create a new Project lab8. Under lab8 create a new Directory exercise, then create...

Using PyCharm, create a new Project lab8. Under lab8 create a new Directory exercise, then create a Python file called nested.py, type the following:

Test your code by putting the following at the bottom of the file: make_table_of_stars(2,3) (as shown in the code above). Run the file by right clicking in the code area of Pycharm, then choosing “Run nested”. Your output should be:

***

***

(2 rows with 3 columns of stars). Reverse the 2 and 3. What happens? Make a new function, make_table_of_digits that prints the digits 1..row*column instead. So nested.make_table_of_digits(2,3) would print:

123

456

To do this, change the '*' to a number. This is a little tricky. Start by just getting the first row correct. Change the printed '*' to print the value of j yields:

012

012

when make_table_of_digits(2,3) is called. Since you want to start with 1, not 0, change number being printed to (j+1) instead of j. Then, make_table_of_digits(2,3) should print

123

123

Much better. The whole first row is correct. To increase the numbers after the first row, multiply row number (i) times the total number of columns. Then, add that value to (j+1), and print. Then make_table_of_digits(2,3) prints:

123

456

That works well until you try make_table_of_digits(5,3), which prints:

123

456

789

101112

131415

make_table_of_digits only works well with 1 digit numbers. Create a new function, make_table_of_numbers, which makes each number take up 3 spaces using string.format. For example,

n = 3

print("{:3}".format(n), end=" ")

would print --3, where ‘-’ is a space. make_table_of_numbers(5,3) should print:

1 2 3

4 5 6

7 8 9

10 11 12

13 14 15

Test your solution and submit.

Solutions

Expert Solution

The code is well commented to understand the initial flow and has carried out steps as mentioned in the question.

def make_table_of_stars(rows, cols):
  # using for loop to iterate over rows
  for i in range(rows):
    # using for loop to iterate over cols
    for j in range(cols):
      print("*", end="")
    # print() to go to next line
    print()
  print()

make_table_of_stars(2,3)
make_table_of_stars(3,2)


def make_table_of_digits(rows, cols):
  for i in range(rows):
    for j in range(cols):
      print(i*cols + (j+1), end="")
    print()
  print()

make_table_of_digits(2,3)
make_table_of_digits(5,3)


def make_table_of_numbers(rows, cols):
  for i in range(rows):
    for j in range(cols):
      print("{:3}".format(i*cols + (j+1)), end=" ")
    print()
  print()

make_table_of_numbers(5, 3)


Output:


Related Solutions

In STS, create Java Project, called java_generics_yourNameLastname that; (Under source directory called, src, create a package...
In STS, create Java Project, called java_generics_yourNameLastname that; (Under source directory called, src, create a package csci3444.generics and put all code in it) Create a generic interface called “MyGenInterface” that takes 2 generic types K,V with below methods; public K getKey(); public V getValue(); Create a generic class called “MyGenClass” that implements “MyGenInterface” interface. has attributes “K key” and “V value” that can be inherited has a constructor that takes “K _key”, “V _value” inputs and initializes “key”, “value” attributes...
Use your IDE (PyCharm) to create a new file named countertop.py. Type in the following lines,...
Use your IDE (PyCharm) to create a new file named countertop.py. Type in the following lines, exactly as they are shown here (including the four spaces before the indented lines of code) and save the file: def countertop(sideLength): """ Compute the area of a square countertop with a missing wedge. The parameter x is the length of one side of the square. """ square = sideLength ** 2 # area of the full square triangle = ((sideLength / 2) **...
For this coding exercise, you need to create a new Java project in Eclipse and finish...
For this coding exercise, you need to create a new Java project in Eclipse and finish all the coding in Eclipse. Run and debug your Eclipse project to make sure it works. Then you can just copy and paste the java source code of each file from Eclipse into the answer area of the corresponding box below. The boxes will expand once you paste your code into them, so don’t worry about how it looks J All data members are...
You first needs to create a file called “myJS.js” in your project directory. Then, include the...
You first needs to create a file called “myJS.js” in your project directory. Then, include the AngularJS library and myJS.js in the header of the index.html page. Now, you need to define your angular application and a controller in this file such that the controller has the following variables with these initial values: name = "Alex Cool"; faculty= "Faculty of Busienss and IT"; university = {name:"University of Ontario Institute of Technology", url:"http://www.uoit.ca"}; contacts = {email:"[email protected]", phone:"xxx-xxx-xxxx"}; skills = [ {name:...
UNIX/LINUX SCRIPT: Create a named directory and verify that the directory is there by listing all...
UNIX/LINUX SCRIPT: Create a named directory and verify that the directory is there by listing all its contents. Write a shell script to validate password strength. Here are a few assumptions for the password string.   Length – a minimum of 8 characters. • Contain alphabets , numbers , and @ # $ % & * symbols. • Include both the small and capital case letters. give a prompt of Y or N to try another password and displays an error...
Create a new project in BlueJ. Create two new classes in the project, with the following...
Create a new project in BlueJ. Create two new classes in the project, with the following specifications: Class name: Part Fields: id (int) description (String) price (double) onSale (boolean) 1 Constructor: takes parameters partId, partDescrip, and partPrice to initialize fields id, description, and price; sets onSale field to false. Methods: Write get-methods (getters) for all four fields. The getters should be named getId, getDescription, getPrice, and isOnSale.
Pycharm Write a function to make a new list 1. if original_samples is [1,2,3], then this...
Pycharm Write a function to make a new list 1. if original_samples is [1,2,3], then this should return a new list [3,2,1]. Don't use the reversed function or original_samples[::-1] def make_reserved_samples (original_samples): new_samples[ ]    return new_samples 2. multiplied by volume. If original_samples is [1,2,3] and volume is 2, the new list is [2,4,6] def make_louder_samples(original_samples, volume): return [ ] 3. If the original list is [1,2,3,4,5,6,7,8] and skip is 3, the new liat should be [1,4,7]. Don't use original_list[::skip]....
Create a new Java project named Program 4 Three Dimensional Shapes. In this project, create a...
Create a new Java project named Program 4 Three Dimensional Shapes. In this project, create a package named threeDimensional and put all 3 of the classes discussed below in this package Include a header comment to EACH OF your files, as indicated in your instructions. Here's a link describing the header. Note that headers are not meant to be in Javadoc format. Note that Javadoc is a huge part of your grade for this assignment. Javadoc requires that every class,...
Create a new Java project named Program 4 Three Dimensional Shapes. In this project, create a...
Create a new Java project named Program 4 Three Dimensional Shapes. In this project, create a package named threeDimensional and put all 3 of the classes discussed below in this package Include a header comment to EACH OF your files, as indicated in your instructions. Here's a link describing the header. Note that headers are not meant to be in Javadoc format. Note that Javadoc is a huge part of your grade for this assignment. Javadoc requires that every class,...
Problem 1 Create a new project called Lab7 Create a class in that project called ListORama...
Problem 1 Create a new project called Lab7 Create a class in that project called ListORama Write a static method in that class called makeLists that takes no parameters and returns no value In makeLists, create an ArrayList object called avengers that can hold String objects. Problem 2 Add the following names to the avengers list, one at a time: Chris, Robert, Scarlett, Clark, Jeremy, Gwyneth, Mark Print the avengers object. You will notice that the contents are displayed in...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT