1. A list of numbers is said to be balanced if the sum of its first half is equal to the sum of its second half (if the list has odd length, then we ignore the center value in both sums). Write a function is_balanced that takes any list of numnbers num_list and returns True if the list is balanced. Otherwise, it should return False. For example, is_balanced (B) and is_balanced(D) should return True , and is_balanced (C) should return False, where B, C, and D are the example lists defined in Part 1 of the lab above. Hint: I might suggest starting your function with something like the three lines below: len (num_list) first_half_sum = 0 last half_sum = 0 Then you can build the two sums, conditioncd on whetheT n is cven or odd.

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
Python help
For all questions on Part 2, you may not use any built-in functions or methods other than
len, and apрепd
range,
1. A list of numbers is said to be balanced if the sum of its first half is equal to the sum of its
second half (if the list has odd length, then we ignore the center value in both sums). Write
a function is_balanced that takes any list of numnbers num list and returns True if the
list is balanced. Otherwise, it should return False.
For example, is_balanced (B) and is_balanced (D) should return True , and is_balanced (C)
should return False,
where B, C, and D are the example lists defined in Part 1 of the lab
above.
Hint: I might suggest starting your function with something like the three lines below:
len (num_list)
first_half_sum =
0.
last half_sum =
Then you can build the two sums, conditioned on whetheT n is even of odd.
2. Write a function interleave that takes two lists 1st1 and 1st2 of the same length and
that returns a combincd list that alternates between the elements of cach list.
For example, interleave([1, 2, 3, 4], ['a', 'b', 'c', 'd']) should return the list
[1, 'а', 2, ъ", 3, 'с', 4, 'd'].
3. Write a function swap adjacent that takes a list 1st with an even number of entrics, and
it returns the new list with the same entrics as 1lst, except every pair of two adjacent values
has swapped locations.
For example, swap_adjacent ([34, 76, 12, 45, 98, 23]) should return
[76, 34, 45, 12, 23, 981
Ilint: This problem can be done urith a traversal. bul it can also be doe without a truversal
Yau en not use luple assignment (ifyou don't know what this is, it's something
Transcribed Image Text:For all questions on Part 2, you may not use any built-in functions or methods other than len, and apрепd range, 1. A list of numbers is said to be balanced if the sum of its first half is equal to the sum of its second half (if the list has odd length, then we ignore the center value in both sums). Write a function is_balanced that takes any list of numnbers num list and returns True if the list is balanced. Otherwise, it should return False. For example, is_balanced (B) and is_balanced (D) should return True , and is_balanced (C) should return False, where B, C, and D are the example lists defined in Part 1 of the lab above. Hint: I might suggest starting your function with something like the three lines below: len (num_list) first_half_sum = 0. last half_sum = Then you can build the two sums, conditioned on whetheT n is even of odd. 2. Write a function interleave that takes two lists 1st1 and 1st2 of the same length and that returns a combincd list that alternates between the elements of cach list. For example, interleave([1, 2, 3, 4], ['a', 'b', 'c', 'd']) should return the list [1, 'а', 2, ъ", 3, 'с', 4, 'd']. 3. Write a function swap adjacent that takes a list 1st with an even number of entrics, and it returns the new list with the same entrics as 1lst, except every pair of two adjacent values has swapped locations. For example, swap_adjacent ([34, 76, 12, 45, 98, 23]) should return [76, 34, 45, 12, 23, 981 Ilint: This problem can be done urith a traversal. bul it can also be doe without a truversal Yau en not use luple assignment (ifyou don't know what this is, it's something
3. Write a function swap_adjacent that takes a list 1st with an even number of entries, and
it returns the new list with the same entries as 1st , except every pair of two adjacent values
has swapped locations.
For example, swap_adjacent ([34, 76, 12, 45, 98, 23]) should return
[76, 34, 45, 12, 23, 98] .
Hint: This problem can be done uith a traversal, but it can also be done without a traversal
using slicing. You may not use tuple assignment (if you don't know what this is, it's something
we'll talk about later in the semester).
4. Write a function remove repetition that takes any list 1st and returns a new list with
the same entrics as 1st , cxcept all instances of consecutive repetition are no longer present.
For example, remove_repetition([3, 4, 4, 6, 4, 3, 3, 3, 1]) should return the new
2
Transcribed Image Text:3. Write a function swap_adjacent that takes a list 1st with an even number of entries, and it returns the new list with the same entries as 1st , except every pair of two adjacent values has swapped locations. For example, swap_adjacent ([34, 76, 12, 45, 98, 23]) should return [76, 34, 45, 12, 23, 98] . Hint: This problem can be done uith a traversal, but it can also be done without a traversal using slicing. You may not use tuple assignment (if you don't know what this is, it's something we'll talk about later in the semester). 4. Write a function remove repetition that takes any list 1st and returns a new list with the same entrics as 1st , cxcept all instances of consecutive repetition are no longer present. For example, remove_repetition([3, 4, 4, 6, 4, 3, 3, 3, 1]) should return the new 2
Expert Solution
trending now

Trending now

This is a popular 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