answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
Law Incorporation [45]
2 years ago
8

Exercise 9.1.6: Checkerboard, v1 Spoint

Computers and Technology
1 answer:
AysviL [449]2 years ago
8 0

Answer:

In order to get following pattern, we have to use numpy package.

Following code with work perfectly fine and will print the pattern.

Python code starts as below

*********************************************

<em># Python program to print 8 X 8 alternative 1 and 0's. 3rd and 4th row with all 0's </em>

<em># checkerboard pattern using numpy </em>

<em># We need Following pattern </em>

<em># 0 1 0 1 0 1 0 1 </em>

<em># 1 0 1 0 1 0 1 0 </em>

<em># 0 1 0 1 0 1 0 1 </em>

<em># 0 0 0 0 0 0 0 0 </em>

<em># 0 0 0 0 0 0 0 0 </em>

<em># 1 0 1 0 1 0 1 0 </em>

<em># 0 1 0 1 0 1 0 1 </em>

<em># 1 0 1 0 1 0 1 0 </em>

<em> </em>

<em>import numpy as np </em>

<em> </em>

<em># function to print Checkerboard pattern </em>

<em>def printcheckboard(n): </em>

<em> </em>

<em>        print(" Customized Checkerboard pattern:") </em>

<em>        # create a n * n matrix   </em>

<em>        x = np.zeros((n, n), dtype = int) </em>

<em>        y = np.zeros((n, n), dtype = int) </em>

<em>        # fill with 1 the alternate rows and columns </em>

<em>        x[1::2, ::2] = 1 </em>

<em>        x[::2, 1::2] = 1 </em>

<em>       # fill with 0 the alternate rows and columns </em>

<em>        y[1::2, ::2] = 0 </em>

<em>        y[::2, 1::2] = 0 </em>

<em> </em>

<em>        # print the pattern  for first 3 rows</em>

<em>        for i in range(0,3): </em>

<em>                for j in range(n): </em>

<em>                        print(x[i][j], end =" ") </em>

<em>                print() </em>

<em>        # print the pattern   for next two rows with all 0's</em>

<em>        for k in range(3,5): </em>

<em>                for l in range(n): </em>

<em>                        print(y[k][l], end =" ") </em>

<em>                print() </em>

<em>         # print the pattern  for last 3 rows with alternative 1 and 0.        </em>

<em>        for i in range(5,8): </em>

<em>                for j in range(n): </em>

<em>                        print(x[i][j], end =" ") </em>

<em>                print() </em>

<em> </em>

<em># Calling the function code </em>

<em>n = 8 </em>

<em>printcheckboard(n)</em>

**************************************

End of the Python Code.

Explanation:

In this you have to use Python3.7 and numpy should be installed as well in order to execute the code successfully.

2 D matrix will be created using Python numpy library and checkboard pattern is generated using array slicing.

Here n=8 and it will generate the checkerboard pattern of alternative 0 and 1. However, we need row 4th and 5th as all 0. So we have initialized two arrays matrix as x and y.

Comments in the code is self explanatory.

PS: Please make sure code is edited in IDE so that tabs or space issues can be taken care.

You might be interested in
C# Write, compile, and test a program named PersonalInfo that displays a person’s name, birthdate, work phone number, and cell p
Soloha48 [4]

Answer:

#include <stdio.h>

#Include<iostream>

using namespace std;

//Define required function

PersonalInfo( )   {

    printf("Name   : Robert Josh\n");  

    printf("DOB    : July 14, 1975\n");  

    printf("Work Phone : 00-00000000\n");  

    printf("Cell Phone : 11-777777777\n");  

    return(0);  

 }

int main()   {

    //Call function

 PersonalInfo( );

 return 0;

 }

Explanation:

Its just a simple code to print required details when the function PersonalInfo is called.

5 0
2 years ago
You are consulting for a trucking company that does a large amount ofbusiness shipping packages between New York and Boston. The
likoan [24]

Answer:

Answer explained with detail below

Explanation:

Consider the solution given by the greedy algorithm as a sequence of packages, here represented by indexes: 1, 2, 3, ... n. Each package i has a weight, w_i, and an assigned truck t_i. { t_i } is a non-decreasing sequence (as the k'th truck is sent out before anything is placed on the k+1'th truck). If t_n = m, that means our solution takes m trucks to send out the n packages.

If the greedy solution is non-optimal, then there exists another solution { t'_i }, with the same constraints, s.t. t'_n = m' < t_n = m.

Consider the optimal solution that matches the greedy solution as long as possible, so \for all i < k, t_i = t'_i, and t_k != t'_k.

t_k != t'_k => Either

1) t_k = 1 + t'_k

    i.e. the greedy solution switched trucks before the optimal solution.

    But the greedy solution only switches trucks when the current truck is full. Since t_i = t'_i i < k, the contents of the current truck after adding the k - 1'th package are identical for the greedy and the optimal solutions.

    So, if the greedy solution switched trucks, that means that the truck couldn't fit the k'th package, so the optimal solution must switch trucks as well.

    So this situation cannot arise.

  2) t'_k = 1 + t_k

     i.e. the optimal solution switches trucks before the greedy solution.

     Construct the sequence { t"_i } s.t.

       t"_i = t_i, i <= k

       t"_k = t'_i, i > k

     This is the same as the optimal solution, except package k has been moved from truck t'_k to truck (t'_k - 1). Truck t'_k cannot be overpacked, since it has one less packages than it did in the optimal solution, and truck (t'_k - 1)

     cannot be overpacked, since it has no more packages than it did in the greedy solution.

     So { t"_i } must be a valid solution. If k = n, then we may have decreased the number of trucks required, which is a contradiction of the optimality of { t'_i }. Otherwise, we did not increase the number of trucks, so we created an optimal solution that matches { t_i } longer than { t'_i } does, which is a contradiction of the definition of { t'_i }.

   So the greedy solution must be optimal.

6 0
2 years ago
add is a method that accepts two int arguments and returns their sum. Two int variables, euroSales and asiaSales, have already b
Oduvanchick [21]

Answer:

// here is code in Java.

// package

import java.util.*;

// class definition

class Main

{

   // method that return sum of two sale value

   public static int Add(int euroSales,int asiaSales)

   {

       // return the sum

       return euroSales+asiaSales;

   }

   //main method of the class

public static void main (String[] args) throws java.lang.Exception

{

   try{

    // variables

       int euroSales=100;

       int asiaSales=150;

       int eurasiaSales;

        // call the function

       eurasiaSales=Add(euroSales,asiaSales);

        // print the sum

       System.out.println("total sale is:"+eurasiaSales);

   }catch(Exception ex){

       return;}

}

}

Explanation:

Declare and initialize two variables "euroSales=100" and "asiaSales=150". Declare another variable eurasiaSales. Call the method Add() with euroSales and asiaSales as parameter. This method will add both the value and return the sum.This sum will be assigned to variable eurasiaSales.Then print the sum.

Output:

total sale is:250  

8 0
2 years ago
An attacker distributes hostile content on Internet-accessible Web sites that exploit unpatched and improperly secured client so
julia-pushkina [17]

Answer:

Since the attack impacts the client SANS Critical Security Controls. This act affects the Inventory of authorized and unauthorized software running on the victim's machine.

5 0
2 years ago
Read 2 more answers
Drag each tile to the correct box.
Likurg_2 [28]
Learning
planning
designing
developing
testing delivering
4 0
2 years ago
Other questions:
  • your monthly living expenses are $1500 on an income of $1,650 per month. your goal is to have an emergency fund of 4 times your
    5·1 answer
  • Following are groups of three​ phrases, which group states three ways to improve your​ concentration?
    13·1 answer
  • A good website design combines which of the following elements? (select all that apply) powerful web server hardware components
    9·2 answers
  • Read first a user's given name followed by the user's age from standard input. Then use an ofstream object named outdata to writ
    6·1 answer
  • Universal Containers needs a field on the Account to track how many Opportunities are closing within the next 30 days. What can
    9·1 answer
  • JAVAThe method longestStreak is intended to determine the longest substring of consecutive identical characters in the parameter
    7·1 answer
  • Determining the Services Running on a Network Alexander Rocco Corporation has multiple OSs running in its many branch offices. B
    10·1 answer
  • A function defined beginning with void SetNegativesToZeros(int userValues[], ... should modify userValues such that any negative
    12·1 answer
  • What data unit is encapsulated inside a packet?<br> frame<br> datagram<br> segment<br> session
    10·2 answers
  • Computers represent color by combining the sub-colors red, green, and blue (rgb). Each sub-color's value can range from 0 to 255
    5·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!