There are given n ropes of different lengths, we need to connect these ropes into one rope. The cost to connect two ropes is equal to sum of their lengths. We need to connect the ropes with minimum cost. If any of the cost is negative, it should print the message saying "Incorrect length!" and end the program. If there's no cost given, It should print the message saying "No ropes found!" and end the program.

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

Use python to solve this question. don't use any libraries or class

Sample:
>>> rope_length([4,3,2,6])
Total cost for connecting all ropes is 5
+ 9 + 15 = 29
>>> rope_length([5,3,2,6,8,11])
Total cost for connecting all ropes is 5
+ 10 + 14 + 21 + 35 = 85
Transcribed Image Text:Sample: >>> rope_length([4,3,2,6]) Total cost for connecting all ropes is 5 + 9 + 15 = 29 >>> rope_length([5,3,2,6,8,11]) Total cost for connecting all ropes is 5 + 10 + 14 + 21 + 35 = 85
There are givenn ropes of different lengths, we need
to connect these ropes into one rope. The cost to
connect two ropes is equal to sum of their lengths.
We need to connect the ropes with minimum cost. If
any of the cost is negative, it should print the
message saying "Incorrect length!" and end the
program. If there's no cost given, it should print the
message saying "No ropes found!" and end the
program.
Explanation:
For example if we are given 4 ropes of lengths 4, 3, 2
and 6. We can connect the ropes in following ways.
1) First connect ropes of lengths 2 and 3. Now we
have three ropes of lengths 4, 6 and 5.
2) Now connect ropes of lengths 4 and 5. Now we
have two ropes of lengths 6 and 9.
3) Finally connect the two ropes and all ropes have
connected.
Total cost for connecting all ropes is 5 + 9 + 15 = 29.
This is the optimized cost for connecting ropes.
Other ways of connecting ropes would always have
same or more cost.
For example, if we connect 4 and 6 first (we get three
strings of 3, 2 and 10), then connect 10 and 3 (we get
two strings of 13 and 2). Finally we connect 13 and 2.
Total cost in this way is 10 + 13 + 15 = 38.
Write a function rope_length() that takes a list of
lengths of n ropes and print the optimized cost for
connecting ropes.
Transcribed Image Text:There are givenn ropes of different lengths, we need to connect these ropes into one rope. The cost to connect two ropes is equal to sum of their lengths. We need to connect the ropes with minimum cost. If any of the cost is negative, it should print the message saying "Incorrect length!" and end the program. If there's no cost given, it should print the message saying "No ropes found!" and end the program. Explanation: For example if we are given 4 ropes of lengths 4, 3, 2 and 6. We can connect the ropes in following ways. 1) First connect ropes of lengths 2 and 3. Now we have three ropes of lengths 4, 6 and 5. 2) Now connect ropes of lengths 4 and 5. Now we have two ropes of lengths 6 and 9. 3) Finally connect the two ropes and all ropes have connected. Total cost for connecting all ropes is 5 + 9 + 15 = 29. This is the optimized cost for connecting ropes. Other ways of connecting ropes would always have same or more cost. For example, if we connect 4 and 6 first (we get three strings of 3, 2 and 10), then connect 10 and 3 (we get two strings of 13 and 2). Finally we connect 13 and 2. Total cost in this way is 10 + 13 + 15 = 38. Write a function rope_length() that takes a list of lengths of n ropes and print the optimized cost for connecting ropes.
Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 2 steps

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