Python: Karma Points

Computer Networking: A Top-Down Approach (7th Edition)
7th Edition
ISBN:9780133594140
Author:James Kurose, Keith Ross
Publisher:James Kurose, Keith Ross
Chapter1: Computer Networks And The Internet
Section: Chapter Questions
Problem R1RQ: What is the difference between a host and an end system? List several different types of end...
icon
Related questions
Question
100%

Python: Karma Points

Inspired by the concept of brownie points, karma points are a hypothetical social currency that can be
earned by doing good deeds and "lost" when doing things that are frowned upon. Contrary to popular
belief, karma points can, in fact, be transferred at your fingertips.
You will bring this concept closer to reality. In this task, you'll implement a function
keepTabs (actions: list[str]) -> dict[str, int]
that take as input a list of actions (an action log book) and returns a dictionary storing the karma points
of all the characters mentioned in actions. People with zero net karma points will be removed from
the dictionary before the function returns.
Each entry of actions comes in one of the following two forms:
• a name (a sequence of English letters without any punctuation marks or spaces) followed by ei-
ther ++ or --, indicating positive karma and negative karma, respectively. For example, "Jack++"
or "Kanat--". The names are case-sensitive and may appear in mixed upper- and lower- cases.
• a name followed by -> then another name, indicating a transfer of all karma points from the first
person to the second. For example, "Abby->Jeff" means Abby gives all her karma points to
Jeff. That is, Jeff inherits all karma points of Abby, be it negative or positive. After that, Abby's
karma point resets to 0.
We guarantee that there will not be extra spaces or any irrelevant punctuation marks anywhere.
Example: Say we call keepTabs on the following actions:
actions
["Jim++", "John--", "Jeff++", "Jim++", "John--", "John->Jeff",
"Jeff--", "June++", "Home->House"]
Initially everyone has 0 karma points. For each positive deed, the point goes up by 1, and for each
negative deed, it goes down by 1. As a result:
• "Jim++" gives +1 to Jim. "John--" gives –1 to John. "Jeff++" gives +1 to Jeff. "Jim++" gives
+1 to Jim. "John--" gives -1 to John. Therefore, now Jim has 2 points, John, -2 points, and Jeff,
1 point.
• Then "John->Jeff" transfers all karma points from John to Jeff. Hence, John's –2 points is given
to Jeff. John now has 0 points and Jeff has 1+ (-2) = –1 points.
%3D
Transcribed Image Text:Inspired by the concept of brownie points, karma points are a hypothetical social currency that can be earned by doing good deeds and "lost" when doing things that are frowned upon. Contrary to popular belief, karma points can, in fact, be transferred at your fingertips. You will bring this concept closer to reality. In this task, you'll implement a function keepTabs (actions: list[str]) -> dict[str, int] that take as input a list of actions (an action log book) and returns a dictionary storing the karma points of all the characters mentioned in actions. People with zero net karma points will be removed from the dictionary before the function returns. Each entry of actions comes in one of the following two forms: • a name (a sequence of English letters without any punctuation marks or spaces) followed by ei- ther ++ or --, indicating positive karma and negative karma, respectively. For example, "Jack++" or "Kanat--". The names are case-sensitive and may appear in mixed upper- and lower- cases. • a name followed by -> then another name, indicating a transfer of all karma points from the first person to the second. For example, "Abby->Jeff" means Abby gives all her karma points to Jeff. That is, Jeff inherits all karma points of Abby, be it negative or positive. After that, Abby's karma point resets to 0. We guarantee that there will not be extra spaces or any irrelevant punctuation marks anywhere. Example: Say we call keepTabs on the following actions: actions ["Jim++", "John--", "Jeff++", "Jim++", "John--", "John->Jeff", "Jeff--", "June++", "Home->House"] Initially everyone has 0 karma points. For each positive deed, the point goes up by 1, and for each negative deed, it goes down by 1. As a result: • "Jim++" gives +1 to Jim. "John--" gives –1 to John. "Jeff++" gives +1 to Jeff. "Jim++" gives +1 to Jim. "John--" gives -1 to John. Therefore, now Jim has 2 points, John, -2 points, and Jeff, 1 point. • Then "John->Jeff" transfers all karma points from John to Jeff. Hence, John's –2 points is given to Jeff. John now has 0 points and Jeff has 1+ (-2) = –1 points. %3D
Then "Jeff--" gives
• Then "Home->House" has no effect because Home didn't have any karma point previously (treated
as 0) and neither did House.
-1 to Jeff. And "June++" gives +1 to June.
So this is where everyone ends up, recorded in a dictionary that keepTabs should return:
{'Jeff': -2, 'June': 1, 'Jim': 2}
Notice that at the end, John has 0 karma points and so doesn't show
up
in the output.
Transcribed Image Text:Then "Jeff--" gives • Then "Home->House" has no effect because Home didn't have any karma point previously (treated as 0) and neither did House. -1 to Jeff. And "June++" gives +1 to June. So this is where everyone ends up, recorded in a dictionary that keepTabs should return: {'Jeff': -2, 'June': 1, 'Jim': 2} Notice that at the end, John has 0 karma points and so doesn't show up in the output.
Expert Solution
steps

Step by step

Solved in 3 steps with 1 images

Blurred answer
Recommended textbooks for you
Computer Networking: A Top-Down Approach (7th Edi…
Computer Networking: A Top-Down Approach (7th Edi…
Computer Engineering
ISBN:
9780133594140
Author:
James Kurose, Keith Ross
Publisher:
PEARSON
Computer Organization and Design MIPS Edition, Fi…
Computer Organization and Design MIPS Edition, Fi…
Computer Engineering
ISBN:
9780124077263
Author:
David A. Patterson, John L. Hennessy
Publisher:
Elsevier Science
Network+ Guide to Networks (MindTap Course List)
Network+ Guide to Networks (MindTap Course List)
Computer Engineering
ISBN:
9781337569330
Author:
Jill West, Tamara Dean, Jean Andrews
Publisher:
Cengage Learning
Concepts of Database Management
Concepts of Database Management
Computer Engineering
ISBN:
9781337093422
Author:
Joy L. Starks, Philip J. Pratt, Mary Z. Last
Publisher:
Cengage Learning
Prelude to Programming
Prelude to Programming
Computer Engineering
ISBN:
9780133750423
Author:
VENIT, Stewart
Publisher:
Pearson Education
Sc Business Data Communications and Networking, T…
Sc Business Data Communications and Networking, T…
Computer Engineering
ISBN:
9781119368830
Author:
FITZGERALD
Publisher:
WILEY