Starting from:

$30

CS115-Lab 3 Functions Solved

1.     In this question, write a docstring for your function.

a.    Write a function that takes an integer, n, as a parameter and displays an n-by-n matrix. Each element in the matrix is 0 or 1, which is generated randomly. ​Hint:​ You can generate a random integer number between x and y, inclusive,  by using the function random.randint(x, y)​         ​.​ Write 

import random  as the first statement in your program.

 

b.    Using your function from part a), write a program that prompts the user to enter a positive integer n and displays an n-by-n matrix. 

 

    ​Sample run 1:

Enter a positive integer: 6

0  0 1 1 1 1 

1  1 1 0 1 1 

0  1 1 0 1 0 

0  1 1 0 1 1 

1  1 1 0 1 0 

1  1 0 1 1 0 

    Sample run 2:

Enter a positive integer: 3

0 0 1 

0  1 1 

1  0 1 

2.     In this question, write a docstring for your functions.

 

a)    Write a function named isPrime(i)​       ​ ​that takes a positive integer i 1 and returns True if it i is a prime number and False otherwise. Your function may assume that i 1. A number is a prime number if it has exactly two positive divisors: 1 and itself. However, 1 has only one positive divisor (1 itself), so it is not a prime number. 

 

b)    Using your function from part a), write another function named listPrimes(a,​     

b) ​that takes integers a and b as input, and finds the prime numbers in the interval [a,b].  Your function may assume that 1 < a < b.

 

c)     Using your function from part b), write a program that inputs two integers a and b (1 < a < b) and display all prime numbers between a and b, inclusive. Your program must validate  a < b.

 

Sample run 1:

 

Enter a positive integer a: 5

 

Enter another positive integer b (a must be less than b): 13

5 is a prime

7 is a prime

11 is a prime

13 is a prime

 

Sample run 2

 

Enter a positive integer a: 1

 

Enter another positive integer b (a must be less than b): 19

1 < 1 < 19 is NOT satisfied

 

Sample run 3

 

           Enter a positive integer a: 19

 

Enter another positive integer b (a must be less than b): 5

1 < 19 < 5 is NOT satisfied 

                     3. In this question, write a docstring for your function.​

 

a)    Write a function named throwUntil(x) ​        ​that takes integer x (2 ≤ x ≤ 12) as parameter, and throws a pair of dice randomly until their sum is equal to x, displays the values of dice with the given sum, and returns the number of rolls it takes to roll the given sum. Your function may assume that x is an int and  2 ≤ x ≤ 12.

 

b)    Write a program that inputs a sum between 2 and 12 from the user, and determines the value of two dice adding to the sum, and the number of times it takes to roll the dice.  Your program should validate that the input sum is between 2 and 12 and prompt for another input until it is in the correct range.

 

     Sample run 1:

 

Enter sum of dice: 1

Sum must be between 2 and 12 inclusive

 

Enter sum of dice: 13

Sum must be between 2 and 12 inclusive

 

Enter sum of dice: 12

Die1 6 Die2 6

Dice are rolled 5 times to get the sum 12 

 

Sample run 2:

 

Enter sum of dice: 2

Die1 1 Die2 1

Dice are rolled 64 times to get the sum 2

More products