Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Write a function list_diff that takes as input two lists, l1 and l2. The output is produced as follows. For every element x of l1:

Write a function list_diff that takes as input two lists, l1 and l2. The output is produced as follows.

For every element x of l1:

  • if xl2, strike out x both from l1 and from l2.
  • if xl2, add x to the resulting string.

Thus, list_diff(['a', 'b', 'b', 'c'], ['b', 'c', 'd']) returns

['a', 'b']

and list_diff(['a', 'a', 'b', 'b', 'b'], ['a', 'a', 'b', 'b', 'c']) returns

['b']

The implementation should leave the input lists unchanged. (Recall that lists are mutable, not immutable like tuples are!) You can achieve this by copying the input arguments before using them; you can obtain a copy of a list l with list(l).image text in transcribed

The implementation should leave the input lists unchanged. (Recall that lists are mutable, not immutable like tuples are!) You can achieve this by copying the input arguments before using them; you can obtain a copy of a list 1 with list(1). [23] def list_diff(11, 12): """The implementation takes 8 lines of code. """ # YOUR CODE HERE raise NotImplementedError() # Tests for list_diff assert_equal(list_diff(['a', 'b', 'b', 'c'], ['b', 'c','d']), ['a', 'b']) assert_equal(list_diff(['a', 'a', 'b', 'b', 'b'], ['a', 'a', 'b', 'b', 'c']), ['b']) [17] # Further tests for list_diff 11 = ['a', 'b', 'c', 'c','d'] 12 = ['b', 'a', 'a', 'c'] assert_equal(list_diff(11, 12), ['c', 'd']) assert_equal(11, ['a', 'b', 'c', 'c','d']) assert_equal(12, ['b', 'a', 'a', 'c'])

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Machine Learning And Knowledge Discovery In Databases European Conference Ecml Pkdd 2014 Nancy France September 15 19 2014 Proceedings Part I Lnai 8724

Authors: Toon Calders ,Floriana Esposito ,Eyke Hullermeier ,Rosa Meo

2014th Edition

3662448475, 978-3662448472

More Books

Students also viewed these Databases questions