Question

Which of the following strings P is a Python program. (b) P = “def f(x): x...

Which of the following strings P is a Python program.
(b) P = “def f(x): x = 'am I a Python program?'”
(d) P = “def f(x,y,z): return y”

(e) P = “def f(x): return y”

For each of the following Python programs P and input strings I, give the output P(I),

  1. (f) P = “def f(x): return str(len(x+x+'x'))”, I = “GAGAT”

  2. (g) P = “def f(x): return str(len(x))”, I=P

  3. (h) P = “def f(x): return str(1/int(x))”, I = “0”

Homework Answers

Answer #1

b,d,e are all valid python programs

def f(x): x = 'am I a Python program?'

def a(x, y, z): return y

def g(x): return x

print(a(4, 5, 6))

print(g(4))

output:

f)

def f(x): return str(len(x+x+'x'))

print(f("GAGAT"))

# will return 11 because x+x+'x'= GAGATGAGATx and its length is 11

output:

g)

def f(x): return str(len(x))

print(f("def f(x): return str(len(x))"))

there are 28 characters in P

output:

h)

it will give runtime error as we doing 1/0 which is dividing by zero

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
For each of the following Python programs P and input strings I, give the output P(I),...
For each of the following Python programs P and input strings I, give the output P(I), using the formal definition of P(I) given, and employing any reasonable reference computer C: Definition of P(I), the output of a Python program. Let P be a Python program with respect to a reference computer system C. Suppose P has main function M, and let I be a string of ASCII characters that will be used as input to P. The output of P...
Python Programming 1. Write the following Python expressions in mathematical notation. a. dm = m *...
Python Programming 1. Write the following Python expressions in mathematical notation. a. dm = m * (sqrt(1 + v / c) / sqrt(1 - v / c) - 1) b. volume = pi * r * r * h c. volume = 4 * pi * r ** 3 / 3 d. z = sqrt(x * x + y * y) 2. What are the values of the following expressions? In each line, assume that s = "Hello" t =...
So, i have this code in python that i'm running. The input file is named input2.txt...
So, i have this code in python that i'm running. The input file is named input2.txt and looks like 1.8 4.5 1.1 2.1 9.8 7.6 11.32 3.2 0.5 6.5 The output2.txt is what i'm trying to achieve but when the code runs is comes up blank The output doc is created and the code doesn't error out. it should look like this Sample Program Output 70 - 510, [semester] [year] NAME: [put your name here] PROGRAMMING ASSIGN MENT #2 Enter...
Write a program that prompts the user to input a string and outputs the string in...
Write a program that prompts the user to input a string and outputs the string in uppercase letters. (Use dynamic arrays to store the string.) my code below: /* Your code from Chapter 8, exercise 5 is below. Rewrite the following code to using dynamic arrays. */ #include <iostream> #include <cstring> #include <cctype> using namespace std; int main() { //char str[81]; //creating memory for str array of size 80 using dynamic memory allocation char *str = new char[80]; int len;...
##4. What will the following program display? ##def main(): ## x = 1 ## y =...
##4. What will the following program display? ##def main(): ## x = 1 ## y = 3.4 ## print(x, y) ## first printing ## change_us(x, y) ## print(x, y) ##second printing ## ##def change_us(a, b): ## a = 0 ## b = 0 ## print(a, b) ## ##main() ## ##Yes, yes, main() displays ##1 3.4 ##0 0 ##1 3.4 ## The question is: why x and y are still the same while the second printing of (x,y)? ## It seems...
Give the output of the program using Call-By-Reference: int i, a[3]; void f (int x, int...
Give the output of the program using Call-By-Reference: int i, a[3]; void f (int x, int y){ x = (x*y) mod 3; y = y – x; } main(){ i = 0; a[0] = 1; a[1] = 2; a[2] = 0; f(i, a[i]); print(“%d %d %d %d\n”, i, a[0], a[1], a[2]); f(a[i], a[i]); print(“%d %d %d\n”, a[0], a[1], a[2]); }
# Define showExspenses function def showExpenses(loan, insure, gas, oil, tyres, maintenance): expense = loan + insure...
# Define showExspenses function def showExpenses(loan, insure, gas, oil, tyres, maintenance): expense = loan + insure + gas + oil + tyres + maintenance # Print monthly and yearly automobile operating expenses print("\nTotal Monthly expenses for operating expenses you entered = ", expense) print(f"\nTotal Yearly expenses for operating expenses you entered = 12 *", expense, f"= {expense * 12:,}") # yearly expenses # write the testing code inside main function def main(): loan = int(input("Enter Loan :")) insure = int(input("Enter...
from typing import List def longest_chain(submatrix: List[int]) -> int: """ Given a list of integers, return...
from typing import List def longest_chain(submatrix: List[int]) -> int: """ Given a list of integers, return the length of the longest chain of 1's that start from the beginning. You MUST use a while loop for this! We will check. >>> longest_chain([1, 1, 0]) 2 >>> longest_chain([0, 1, 1]) 0 >>> longest_chain([1, 0, 1]) 1 """ i = 0 a = [] while i < len(submatrix) and submatrix[i] != 0: a.append(submatrix[i]) i += 1 return sum(a) def largest_rectangle_at_position(matrix: List[List[int]], x:...
In python comment the following code line by line as to what each line does def...
In python comment the following code line by line as to what each line does def f(x): return 6*x**5-5*x**4-4*x**3+3*x**2 #here we define a polynomial def df(x): return 30*x**4-20*x**3-12*x**2+6*x #here we define a polynomial that takes the derivative of the above function f(x) def dx(f, x): return abs(0 - f(x)) def newtons_method(f, df, x0, e): delta = dx(f, x0) while delta > e: x0 = x0 - f(x0)/df(x0) delta = dx(f, x0) print('Root is at:', x0) print('f(x) at root is: ',...
In Python def main(): c = 10 print("Welcome to Roderick's Chikkin and Gravy") e = False...
In Python def main(): c = 10 print("Welcome to Roderick's Chikkin and Gravy") e = False while not e: x = input("Would you like some chikkin?") if x == "Y" or x == "y": c = f(c) else: e = True if c == 0: e = True print("I hope you enjoyed your chikkin!") def f(c): if c > 0: print("Got you some chikkin! Enjoy") return c-1 else: print("No chikkin left. Sorry") return 0 main() For practice with lists, create...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT