using python 3 Define a function myFormat which accepts 3 parameters (a number,
ID: 3813436 • Letter: U
Question
using python 3
Define a function myFormat which accepts 3 parameters (a number, totalWidth, and numberDigitsRightOfDecimal). It should format the integer with the specified width and number of digits to the right of the decimal (rounding if necessary), returning the number as a STRING with prefix 0's. If the number of digits in the integer is already longer than the width, just return the original number as a string. Examples: myFormat(34, 5, 0) returns the string"00034". myFormat(34, 1, 0) returns the string"34" myFormat(34.678, 8, 2) returns the string"00034.68" Write a program which takes 2 digits, X, Y as input and generates a 2-dimensional array. The element value in the i-th row and j-th column of the array should be i*j. Example: Suppose the following inputs are given to the program: 3, 5 Then, the output of the program should be: [[0, 0, 0, 0, 0], [0, 1, 2, 3, 4], [0, 2, 4, 6, 8]] A pangram is a sentence that contains aII the letters of the English alphabet at least once, for example: The quick brown fox jumps over the lazy dog. Create a function called isPangram to check a sentence to see if it is a pangram or not. It should return a Boolean (True or False) Write a program that asks the user for a digit d and computes the value of d + dd + ddd + dddd + ... Example: if d is 5, it should compute the value of 5 + 55 + 555 + 5555 + 55555Explanation / Answer
PROGRAM 1:
CODE:
def myFormat(number, totalWidth, numberOfDecimals):
number = float(number);
if(number.is_integer()):
number = int(number);
number = str(number);
number = number + '.';
counter = 0;
while(counter<numberOfDecimals):
number = number + '0';
counter = counter + 1;
while(len(number)<totalWidth):
number = '0' + number;
return number;
else:
number = str(round(number, numberOfDecimals));
decimalIndex = number.index('.');
currentDecimals = len(number)-decimalIndex-1;
while(currentDecimals<numberOfDecimals):
number = number + '0';
currentDecimals = currentDecimals + 1;
while(len(number) < totalWidth):
number = '0' + number ;
return number;
print(myFormat(34.678, 8, 2));
OUTPUT:
PROGRAM 2:
CODE:
num1 = int(input('Enter x: '));
num2 = int(input('Enter y: '));
array = [[x*y for x in range(num2)] for y in range(num1)];
print(array);
OUTPUT:
PROGRAM 3:
def isPangram(string):
string = string.lower();
alphabets = "abcdefghijklmnopqrstuvwxyz";
for i in range(0,26):
if string.find(alphabets[i]) == -1:
return False;
return True;
string = input('Enter a string: ');
print(isPangram(string));
OUTPUT:
PROGRAM 4:
d = int(input('Enter digit: '));
sum = 0;
num = str(d);
for i in range(0, d):
sum = sum + int(num);
num = num + str(d);
print(sum)
OUTPUT:
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.