Please is there any other way to write the line l = [x for x in l if x not in n] and write the code another way ? Please help me on python without using "break" or "continue", and no global variables in functions.

C++ Programming: From Problem Analysis to Program Design
8th Edition
ISBN:9781337102087
Author:D. S. Malik
Publisher:D. S. Malik
Chapter17: Linked Lists
Section: Chapter Questions
Problem 18PE
icon
Related questions
Question

Please is there any other way to write the line l = [x for x in l if x not in n] and write the code another way ? Please help me on python without using "break" or "continue", and no global variables in functions.

 

def remove_pairs(1):
(list of str)->list of str
Returns a copy of list 1 where all the pairs from 1 are removed AND
the elements of the new list shuffled
Precondition: elements of 1 are cards represented as strings described above
Testing:
Note that for the individual calls below, the function should
return the displayed list but not necessarily in the order given in the examples.
>>> remove_pairs(['94', '54',
['104', '24', '3♡',
>>> remove_pairs(['104'
['24', '50',
'44'
'54', '7♡', 'A^', '104', 'Q♡', '8♡'.
190', '100
'J♡', '10♡',
'J h', '3♡'])
'KO', 'A',
'A',
6',
'KO',
'J4', 'Q0']
'AO',
'24', 'Q*', 'KA', 'Qo', 'Jª', 'A♡',
'44',
'7♡',
'80',
194',
'24', '50',
'AO'j
'100'])
'6', '94'
no_pairs=[]
for i in ('2','3','4','5','6','7','8','9','10','J','Q','K','A'):
n = []
for j in range (len(1)):
if 1[j].find (i) != -1:
n.append (1[j])
if len(n) == 2 or len(n) == 3:
1.remove (n[0])
1.remove (n[1])
elif len(n) == 4:
1 = [x for x in 1 if x not in n]
no_pairs = 1
random. shuffle(no_pairs)
return no_pairs
Transcribed Image Text:def remove_pairs(1): (list of str)->list of str Returns a copy of list 1 where all the pairs from 1 are removed AND the elements of the new list shuffled Precondition: elements of 1 are cards represented as strings described above Testing: Note that for the individual calls below, the function should return the displayed list but not necessarily in the order given in the examples. >>> remove_pairs(['94', '54', ['104', '24', '3♡', >>> remove_pairs(['104' ['24', '50', '44' '54', '7♡', 'A^', '104', 'Q♡', '8♡'. 190', '100 'J♡', '10♡', 'J h', '3♡']) 'KO', 'A', 'A', 6', 'KO', 'J4', 'Q0'] 'AO', '24', 'Q*', 'KA', 'Qo', 'Jª', 'A♡', '44', '7♡', '80', 194', '24', '50', 'AO'j '100']) '6', '94' no_pairs=[] for i in ('2','3','4','5','6','7','8','9','10','J','Q','K','A'): n = [] for j in range (len(1)): if 1[j].find (i) != -1: n.append (1[j]) if len(n) == 2 or len(n) == 3: 1.remove (n[0]) 1.remove (n[1]) elif len(n) == 4: 1 = [x for x in 1 if x not in n] no_pairs = 1 random. shuffle(no_pairs) return no_pairs
Expert Solution
steps

Step by step

Solved in 2 steps with 8 images

Blurred answer
Knowledge Booster
Fibonacci algorithm
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
C++ Programming: From Problem Analysis to Program…
C++ Programming: From Problem Analysis to Program…
Computer Science
ISBN:
9781337102087
Author:
D. S. Malik
Publisher:
Cengage Learning