Question

Python 1. Open up IDLE and in a multiline comment write “Functions and Sets II,” your...

Python

1. Open up IDLE and in a multiline comment write “Functions and Sets II,” your name, and the date.

2. Use Python to write a function f(n) that finds the sum of the first n positive integers. Do not use the sum() function or lists. Write the function from scratch. Then use a for loop to print the sums for n = 10, 50, 1000.

3. Finding the Image of a function Suppose that we have a function f : A → B where A is a finite set. We can use Python to find the image f(A) with a single command, provided we have already defined f as a function. im_f = {f(a) for a in A} For the following functions and domains, use Python to compute and print the images: a) f(x) = x 2 − 2x + 7 and A = {1, 2, 3, . . . , 20}. b) g(x) = (x + 1)2/(3x + 2) and A = {2, 4, 6, 8, 10}.

4. Testing for onto: To determine a function is onto is simple. If f : A → B we first compute im(f) as in the previous exercise. Then we compute any(x for x in B-im_f) which literally asks if there is anything in the codomain that is left when we remove the image. If there are, then the function is not onto. Use Python to determine if f(x) = 2x−1 with domain {1, 2, . . . , 10} is onto the set {1, 3, 5, . . . , 19}.

5. Figure out how to test a function for one to one using the all() command and list comprehension only. No looping, no element-by-element testing allowed! Check your result with the function h(x) = x 3 + 5 over the domain {−100, −99, . . . , 99, 100}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

2)

def SUM(n):
    s = 0
    s = (n*(n+1))/2
    print s

#Driver program

a = [100,50,1000]
for x in a:
    SUM(x)


3)

def funcF(A):
    print "Images of function f(x) are :\n"
    for x in A:
        s = x+2-2*x+7
        print "%d --> %d"%(x,s)

def funcG(A):
    print "\n\nImages of function g(x) are : \n"
    for x in A:
        x = float(x)
        s = float(((x+1)*2)/(3*x+2))
        print "%d --> %f"%(x,s)

A = [x for x in range(1,20)]

funcF(A)

A = [x for x in range(2,11,2)]

funcG(A)
  

Add a comment
Know the answer?
Add Answer to:
Python 1. Open up IDLE and in a multiline comment write “Functions and Sets II,” your...
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. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Part 1: Using Idle Write a Python Script that Does the Following 1. At the top...

    Part 1: Using Idle Write a Python Script that Does the Following 1. At the top of your program, import the math library as in from math import * to make the functions in the math module available. Create a variable and assign into it a constant positive integer number of your choice. The number should be at most 10. 1 Suppose we call this variable x for this writeup document Try something like x = 4 2. Create another...

  • 10.3.1 Exercises 10.33: An ordinary function graph combines domain and range information in a single picture: we plot the ordered pair (x, f(x)). several variables we quickly run out of pictures...

    10.3.1 Exercises 10.33: An ordinary function graph combines domain and range information in a single picture: we plot the ordered pair (x, f(x)). several variables we quickly run out of pictures we can draw, but there is simple alternative, which we illustrate first with an ordinary function from R to R. (This is exactly a domain-range picture, introduced in Section 1.3.) Take f(x) 2. Draw the domain of the function as a single vertical line (fair: the domain is R)....

  • ****python**** Q1. Write a recursive function that returns the sum of all numbers up to and...

    ****python**** Q1. Write a recursive function that returns the sum of all numbers up to and including the given value. This function has to be recursive; you may not use loops! For example: Test Result print('%d : %d' % (5, sum_up_to(5))) 5 : 15 Q2, Write a recursive function that counts the number of odd integers in a given list. This function has to be recursive; you may not use loops! For example: Test Result print('%s : %d' % ([2,...

  • This is for programming using Python with the JES software. Please write the code with the...

    This is for programming using Python with the JES software. Please write the code with the proper indentation. Thanks! Problem 1: Write a function that takes 2 arguments (integer or float) and computes the sum, difference, product, and quotient of the arguments. Once you calculate the values you should print out statements telling the user what arguments we used to compute the values and what the result of each calculation (your function should produce 5 lines of output in the...

  • 1. In Python, 'mutable' objects such as lists are passed to functions 'by reference'. This means:...

    1. In Python, 'mutable' objects such as lists are passed to functions 'by reference'. This means: a.) Changes made to the object within the function persist after the function completes b.) The function receives a copy of the object c.) Python keeps a history of where then object was used d.) Statements in the function can refer to the object by nickname 2. Which of the following is NOT a requirement for a Python function: a.) The function must have...

  • 1. In Python, Write a function to convert inches into yards, feet and inches. The functions...

    1. In Python, Write a function to convert inches into yards, feet and inches. The functions should take one parameter, 'inches', and print out the number of yards, feet, and inches equivalent to the value of the argument. Call the function after prompting the user to enter the number of inches. Don't forget to convert the input to an 'int'. 2. In Python,Write a function to convert celsius to fahrenheit. The function should return a value, which you should assign...

  • 1) Translate the following equation into a Python assignment statement 2) Write Python code that prints...

    1) Translate the following equation into a Python assignment statement 2) Write Python code that prints PLUS, MINUS, O ZERO, depending on the value stored in a variable named N. 3) What is printed by: 3 - 1 while 5: while 10 Print ) Page 1 of 9 4) Write a Python while loop that reads in integers until the user enters a negative number, then prints the sum of the numbers. 1-2 of 9 4) Write a Python while...

  • using python with only using import math and copy no other powerful functions. Write a function...

    using python with only using import math and copy no other powerful functions. Write a function defined as: def GaussSeidel(Aaug, x, Niter = 15): Purpose: use the Gauss-Seidel method to estimate the solution to a set of N linear equations expressed in matrix form as Ax = b. Both A and b are contained in the function argument – Aaug. Aaug: an augmented matrix containing [A | b ] having N rows and N+1 columns, where N is the number...

  • Answer the questions in the space provided below. 1. The definition of a function f: X...

    Answer the questions in the space provided below. 1. The definition of a function f: X + Y is as a certain subset of the product X x Y. Let f: N + N be the function defined by the equation f(n) = n2. For each pair (x, y) listed below, determine whether or not (x,y) ef. a) (2,4) b) (5, 23) c) (1,1) d) (-3,9) 2. For each function defined below, state whether it is injective (one-to-one) and whether...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT