Question

Python Write a function square(a) that takes a list a of numbers and squares each of...

Python

  1. Write a function square(a) that takes a list a of numbers and squares each of the numbers in the list. The function should not return a value (more specifically, it should return None).
  2. Write a function squared(a) that takes a list a of numbers and returns a new list that contains each value in a squared. The original list a should not be modified by your function.
  3. Write a function lowercase(a) that takes a list a of strings and replaces each string in a with a lowercase version of the string
  4. Write a function lowercased(a) that takes a list a of strings and returns a new list in that contains a lowercase version of each string in a
  5. Create a new class called Point that represents a point in the Cartesian plane with two coordinates.
    1. Each instance of Point should have to coordinates called x and y.
    2. The constructor for Point should take two arguments x and y that represent the two coordinates of the newly created point.
    3. Your class Point should override the __str__(self) method so that it returns a string showing the x- and y-coordinates of the Point enclosed in parentheses and separated by a comma.
    4. Your class Point should override the __repr__(self) method so that it returns a string that contains Python code that would produce an instance of the same point.
    5. Your class Point should override the __eq__(self, other) method so that it returns True if and only if other is also a Point that has the same x- and y- coordinate as self.
  6. Write a function distance(p, q) that takes two Points p and q and returns the Euclidean distance between p and q.
  7. Write a function double(p) that modifies p so that the values of its x- and y-coordinates are doubled.
  8. Write a function doubled(p) that returns a new Point whose x- and y-coordinates are twice those of p

Sample Output

>>> a = list(range(1, 6))
>>> a
[1, 2, 3, 4, 5]
>>> square(a)
>>> a
[1, 4, 9, 16, 25]
>>> 
>>> 
>>> b = list(range(1,6))
>>> squared(b)
[1, 4, 9, 16, 25]
>>> b
[1, 2, 3, 4, 5]
>>> c = squared(b)
>>> c
[1, 4, 9, 16, 25]
>>> b
[1, 2, 3, 4, 5]
>>> 
>>> 
>>> a = ["Kate", "and", "Allie", "spoke", "LOUDLY"]
>>> a
['Kate', 'and', 'Allie', 'spoke', 'LOUDLY']
>>> lowercase(a)
>>> a
['kate', 'and', 'allie', 'spoke', 'loudly']
>>> 
>>> 
>>> b = ["Kate", "and", "Allie", "spoke", "LOUDLY"]
>>> b
['Kate', 'and', 'Allie', 'spoke', 'LOUDLY']
>>> lowercased(b)
['kate', 'and', 'allie', 'spoke', 'loudly']
>>> b
['Kate', 'and', 'Allie', 'spoke', 'LOUDLY']
>>> 
>>> c = lowercased(b)
>>> c
['kate', 'and', 'allie', 'spoke', 'loudly']
>>> b
['Kate', 'and', 'Allie', 'spoke', 'LOUDLY']
>>> 
>>> p = Point(3,4)
>>> p
Point(3,4)
>>> 
>>> str(p)
'(3,4)'
>>> 
>>> print("p = {}".format(p))
p = (3,4)
>>> 
>>> 
>>> q = Point(3,4)
>>> 
>>> 
>>> p
Point(3,4)
>>> q
Point(3,4)
>>> 
>>> 
>>> p == q
True
>>> 
>>> 
>>> p is q
False
>>> 
>>> 
>>> 
>>> 
>>> r = Point(3,7)
>>> p
Point(3,4)
>>> r
Point(3,7)
>>> 
>>> p == r
False
>>> 
>>> 
>>> p
Point(3,4)
>>> q
Point(3,4)
>>> r
Point(3,7)
>>> 
>>> distance (p,q)
0.0
>>> 
>>> distance(p,r)
3.0
>>> 
>>> 
>>> p
Point(3,4)
>>> double(p)
>>> p
Point(6,8)
>>> 
>>> 
>>> q
Point(3,4)
>>> doubled(q)
Point(6,8)
>>> q
Point(3,4)
>>> s = doubled(q)
>>> 
>>> s
Point(6,8)
>>> q
Point(3,4)
>>> 
>>> quit()
0 0
Add a comment Improve this question Transcribed image text
Answer #1
def square(a):
    for i in range(0, len(a)):#Loop through the list a
        a[i] = a[i]*a[i]#Update element of list to its square

def squared(a):
    b = []#Empty list
    for i in range(0, len(a)):#Loop through the list a
        b.append(a[i]*a[i])#Append to list b square of element in a
    return b#Return new list

def lowercase(a):
    for i in range(0, len(a)):#Loop through the list a
        a[i] = a[i].lower()#Convert each string of list a to lowercase

def lowercased(a):
    b = []#Empty list
    for i in range(0, len(a)):#Loop through the list a
        b.append(a[i].lower())#Append lowercase of each string of list a to list b
    return b#Return new list

#Test square function
a = list(range(1, 6))
square(a)
print(a)

#Test squared function
b = list(range(6, 12))
c = squared(b)
print(c)

#Test lowercase function
d = ["Kate", "and", "Allie", "spoke", "LOUDLY"]
lowercase(d)
print(d)

#Test lowercased function
e = ["Kate", "and", "Allie", "spoke", "LOUDLY"]
f = lowercased(e)
print(f)

OUTPUT:

AS PER CHEGG'S POLICY ONLY FIRST 4 QUESTIONS WILL BE ANSWERED

Add a comment
Know the answer?
Add Answer to:
Python Write a function square(a) that takes a list a of numbers and squares each of...
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
  • Python Write a move function that takes a list of tuples, where each tuple is an...

    Python Write a move function that takes a list of tuples, where each tuple is an x-y-z coordinate pair. This function should update the original list in-place to shift/move/translate the coordinates by an x_move, y_move, and z_move (respectively) and return None.

  • PYTHON Programming short Questions: 1. Write a function takes as input name (string) and age (number)...

    PYTHON Programming short Questions: 1. Write a function takes as input name (string) and age (number) and prints: My name is <name> and I am <age> years old(where name and age are the inputs) 2. Write a function that takes as input a and b and returns the result of: ??ab 3. Write a function that takes as input a and b and returns the result of: ??ab if it is valid to divide a and b 4. Write a...

  • Haskell Function program Write a function negateOdds that takes a list of integers and returns a...

    Haskell Function program Write a function negateOdds that takes a list of integers and returns a list of integers with all of the odd integers negated. negateOdds :: [Integer] -> [Integer] example: [1,2,3,4,5] should return [-1,2,-3,4,-5]

  • Problem 1. Write a Python function times_i_at_odd(L) that takes as arguments a list L and returns...

    Problem 1. Write a Python function times_i_at_odd(L) that takes as arguments a list L and returns a list consisting of the elements of L multiplied by the index number of the element at odd positions. (Use list comprehensions) >>> times_i_at_odd([1,2,3,4,5,6,7,8,9,10]) [2, 12, 30, 56, 90] Problem 2. Write a recursive function sum_cols(grid, n) that takes a list of lists of integers grid and integer n and returns the sum of column n in grid. For example, the call sum_cols([[1,2,3,4], [10,20,30,40],...

  • Python 3.0 Please Write a function that takes, as arguments, a binary string, a list of...

    Python 3.0 Please Write a function that takes, as arguments, a binary string, a list of characters and the corresponding Huffman code for those characters. It decodes the binary string using the Huffman code, and returns the resulting decoded string. Name this function decodeStringHuffman(binaryString, myCharacters, myCode). For example, >>>decodeStringHuffman("1001100010111010101010111", ["A", "B", "C"], ["1", "00", "01"]) should return the string 'ABAABCCAACCCCCAA'

  • Write a function called most_consonants(words) that takes a list of strings called words and returns the...

    Write a function called most_consonants(words) that takes a list of strings called words and returns the string in the list with the most consonants (i.e., the most letters that are not vowels). You may assume that the strings only contain lowercase letters. For example: >>> most_consonants(['python', 'is', 'such', 'fun']) result: 'python' >>> most_consonants(['oooooooh', 'i', 'see', 'now']) result: 'now' The function that you write must use a helper function (either the num_vowels function from lecture, or a similar function that you...

  • Write the function deep_contains. It takes as input a number and a list. That list might...

    Write the function deep_contains. It takes as input a number and a list. That list might contain lists as elements, and those lists might contain lists, etc. The deep_contains' function should return a Boolean indicating whether the number is contained inputted list, or a sublist of it, or a sublist of that, and so forth. For example: deep_contains (3, (1,3,5]) returns True deep_contains(3, 11, [3,5]]) returns True deep_contains (3, 1, [[3,4],5),6]) returns True deep_contains (2, (1,3,5]) returns False This function...

  • Write a function findEvens that takes an integer, n, as input and returns the list of...

    Write a function findEvens that takes an integer, n, as input and returns the list of even integers between 1 and n. Ex. Input: 10 Output: [2,4,6,8,10] Write a function sortList that takes in a list of strings and returns a list of those strings now sorted and lowercase. Ex. Input: [‘ABE’,’CAD’,’gaB’] Output: [‘abe’,’acd’,’abg’] Both done in Python

  • write a Python function that takes in a list of integers and returns maximum and minimum values in the list as a tuple

    1 write a Python function that takes in a list of integers and returns maximum and minimum values in the list as a tuple. Hint (can be done in one pass, you are not allowed to use built-on min and max functions.)max, min = find_max_min(my_list):2 write a Python function that takes in a list of integers (elements), and an integer number (num). The functions should count and return number of integers in elements greater than, less than, and equal to...

  • Python - Write a function called create_basic_pattern(background_colour, size)

    Write a function called create_basic_pattern(background_colour, size) which takes a background colour code and an integer as parameters and returns a list of strings. The list represents the pattern of a pixel art pattern. For example, consider the following code fragment:pattern \(=\) create_basicThen the function should create a list of strings. The size of the list is 8 . Each element is a string with 8 'y' characters:

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