1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
Bumek [7]
3 years ago
15

Write a function that takes in a sequence s and a function fn and returns a dictionary. The values of the dictionary are lists o

f elements from s. Each element e in a list should be constructed such that fn(e) is the same for all elements in that list. Finally, the key for each value should be fn(e)
def group_by(s, fn):
""" >>> group_by([12, 23, 14, 45], lambda p: p // 10)
{1: [12, 14], 2: [23], 4: [45]}
>>> group_by(range(-3, 4), lambda x: x * x)
{0: [0], 1: [-1, 1], 4: [-2, 2], 9: [-3, 3]}
"""
Use Python

Computers and Technology
1 answer:
aleksandrvk [35]3 years ago
3 0

Answer:

Here is the Python function:

def group_by(s, fn):  #method definition

    group = {}  #dictionary

    for e in s:  #for each element from s

         key = fn(e)  #set key to fn(e)

         if key in group:  #if key is in dictionary group

              group[key].append(e)  #append e to the group[key]

         else:  #if key is not in group

              group[key] = [e]  #set group[key] to [e]

    return group #returns dictionary group

Explanation:

To check the working of the above function call the function by passing a sequence and a function and use print function to display the results produced by the function:

print(group_by([12, 23, 14, 45], lambda p: p // 10))

print(group_by(range(-3, 4), lambda x: x * x))

function group_by takes in a sequence s and a function fn as parameters. It then creates an empty dictionary group. The values of the dictionary are lists of elements from s. Each element e in a list is constructed such that fn(e) is the same for all elements in that list. Finally, the key for each value is fn(e). The function returns a dictionary. The screenshot of the program along with its output is attached.

You might be interested in
Qbasic program to accept 10 numbers and to find their sum. <br>​
sesenic [268]

Answer:

the answer is 5

Explanation:

6 0
4 years ago
Jack was drinking a glass of water near his computer when his dog bumped his arm, spilling water on his keyboard. What is the fi
Anvisha [2.4K]

Answer:

4no. is the answer may be

3 0
3 years ago
Read 2 more answers
This is not a factor that you should use to determine the content of your presentation. Your audience your goals your purpose yo
aksik [14]

Your technology I believe

4 0
4 years ago
Read 2 more answers
The ____ line for any e-mail messages you write should clearly state the intention of the e-mail..
KatRina [158]

The <u>subject </u>line for any e-mail messages you write should clearly state the intention of the e-mail.

Electronic mail (e-mail) is a method of digital communications in which messages are exchanged through the internet. The messages are sent from a sender to one or more receivers.  Depending on user’s perspective, every user uses e-mail differently.  Communicating via e-mail requires e-mail platforms such as G*mail, Ya*hoo! Mail, Hot*mail, and Out*look etc. In the every e-mail, there is a subject line that clearly state the intention of the e-mail.

Subject line is an important element of an e-mail. Through the subject line, the message written in the e-mail is briefly summarized in six to eight words. A clearly written subject line provides the recipient a clear understanding that why the email has been sent. When recipient replies back to the e-mail, the subject line is changed accordingly.

This is the subject line of the e-mail that convince the receiver whether to open and read the e-mail. So, it should be written in the way that gives clear intention of the e-mail. We can say that the subject line of the e-mail is the most significant few words in the whole e-mail to convey the main purpose of the e-mail.

You can learn more about subject line at

brainly.com/question/14572730

#SPJ4

3 0
2 years ago
When comparison shopping, all of these hint at a good deal EXCEPT_____________________.
pav-90 [236]

Answer:

lower-priced models offer more features

Explanation:

4 0
3 years ago
Other questions:
  • Which layer defines an interface that applications can use to request network services, rather than referring directly to applic
    5·1 answer
  • Give a recursive implementation for the function: def is_sorted(lst, low, high) This function is given a list of numbers, lst, a
    10·1 answer
  • Why is it important to cite your sources?
    8·2 answers
  • Which of the following statements about electronic cover letters is true?
    14·2 answers
  • Hard and soft skills can both be transferable skills. *<br><br> True<br><br> False
    5·2 answers
  • Let X and Y be two decision problems. Suppose we know that X reduces to Yin polynomial time. Which of the following statements a
    14·1 answer
  • For this assignment your are to implement the Pet Class described in Programming Exercise 1, starting on page 494 in our textboo
    6·1 answer
  • Hello everyone. New ppl on Ro.blox? I want to play sometime.
    8·2 answers
  • Which command tells the for loop what to count by?
    9·2 answers
  • Computer in education are used for teaching and learning aids​
    14·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!