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...
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...
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]); }
convert this code to accept int value instead of float values using python. Make sure to...
convert this code to accept int value instead of float values using python. Make sure to follow the same code. do not change the steps and make sure to point to what code you replaced. make sure to have 2 files Method:----------------------- #define a python user difined method def get_float_val (prompt): is_num = False str_val = input (prompt) #prming read for our while #while is_num == False: (ignore this but it works) old school while not is_num: try: value =...
I need some tests for this python class. some tests for some of the functions in...
I need some tests for this python class. some tests for some of the functions in this class like. def __init__ def __eq__ def __lt__ def __hash__ I am using Pycharm and useing Pytest to write some tests but have no idea how to write one --------------------------------------------------------------------------------------------------- class Movie: def __set_title_internal(self, title: str): if title.strip() == "" or type(title) is not str: self.__title = None else: self.__title = title.strip() def __set_release_year_internal(self, release_year: int): if release_year >= 1900 and type(release_year) is...
The following Python program contains errors that will be detected by the IDLE syntax-checker. For each...
The following Python program contains errors that will be detected by the IDLE syntax-checker. For each of these errors, state the number of the lines on which it occurs and how it should be corrected. a.x = int(input(“Enter value for x”)) b.print(“The square of x is”x*x) c.y = input(“Enter value for y”) d.print(“ The product of x and y is “, x*y) i.if x>y ii.print(“the differences is “, x-y) iii.else: iv.print(“The difference is “, y-x
write a program in python for following x = { 'a' : 1 , 'b':2 ,...
write a program in python for following x = { 'a' : 1 , 'b':2 , 'c':3, 'd':4 } For the following key,value pairs of dictionary display those which are even values i.e for which the value is even. i am trying like this but getting confused. (k:v for k,v in x.items() if v%2 != 0
Create a memory diagram for the following C program for the second time the program reaches...
Create a memory diagram for the following C program for the second time the program reaches point one. int foo(const char *x, int y); int main(void) { int a, b; a=foo("abcde", 'p'); b=foo("purple", 't'); return 0; } int foo(const char *x, int y) { int c = -1, i = 0; while (x[i] != '\0') { if (x[i] == y) i++; } //point 1 return c; }
Please provide answer in the format that I provided, thank you Write a program that prompts...
Please provide answer in the format that I provided, thank you Write a program that prompts the user to input a string. The program then uses the function substr to remove all the vowels from the string. For example, if str=”There”, then after removing all the vowels, str=”Thr”. After removing all the vowels, output the string. Your program must contain a function to remove all the vowels and a function to determine whether a character is a vowel. You must...
Python Blackjack Game Here are some special cases to consider. If the Dealer goes over 21,...
Python Blackjack Game Here are some special cases to consider. If the Dealer goes over 21, all players who are still standing win. But the players that are not standing have already lost. If the Dealer does not go over 21 but stands on say 19 points then all players having points greater than 19 win. All players having points less than 19 lose. All players having points equal to 19 tie. The program should stop asking to hit if...