Solved by Expert Tutors
Solved by Expert Tutors
Question

Hello, thank you in advance for your help. I'm a new computer science student working on an assignment. I've written some code for the questions I attached below, but my code isn't quite working, and I can't figure out why. Might someone be able to point out what I'm doing wrong/what I need to fix? Thanks again. I included pictures of my code as well.

Thank you again!

def deep_list (seq) :
&quot;&quot;Returns a new list containing elements of the original list that are lists.
&gt;&gt;&gt; seq = [49, 8, 2, 1, 102]
&gt;&gt;&gt; deep_list(seq) &gt;&gt;&gt; seq = [[500] , [30, 25, 24], 8, [0]]
&gt;&gt;&gt; deep_list (seq)
[ [500], [30, 25, 24], [0]]
&gt;&gt;&gt; seq = [&quot;hello&quot;, [12, [25], 24], 8, [0]]
&gt;&gt;&gt; deep_list(seq)
[ [12, [25], 24], [0]]
return [i if type(x) == list for i in seq]
def count_cond (condition, n) :
&gt;&gt;&gt; def divisible(n, i):
return n % i == 0
&gt;&gt;&gt; count_cond (divisible, 2) # 1, 2
2
&gt;&gt;&gt; count_cond (divisible, 4) # 1, 2, 4
B
count_cond (divisible, 12) # 1, 2, 3, 4, 6, 12
6
&gt;&gt;&gt; def is_prime(n, i):
return count_cond (divisible, i) == 2
&gt;&gt;&gt; count_cond(is_prime, 2) # 2
1
&gt;&gt;&gt; count_cond(is_prime, 3) # 2, 3
2
&gt;&gt;&gt; count_cond( is_prime, 4) # 2, 3
2
count_cond (is_prime, 5) # 2, 3, 5
3
&gt;&gt;&gt; count_cond(is_prime, 20) # 2, 3, 5, 7, 11, 13, 17, 19
3
def count_cond (n) :
i, count = 1, 0
while i &lt;= n:
if is_prime (i) :
count += 1
else: divisible(i)
count += 1
else:
i += 1
return count
Question 6: Count van Count
Consider the following implementations of count_factors and count_primes :
def count_factors (n) :
&quot;Return the number of positive factors that n has.&quot;&quot;
i, count = 1, 0
while i &lt;= n:
if n \$ i == 0:
count += 1
i += 1
return count
def count_primes (n) :
&quot;&quot;Return the number of prime numbers up to and including n.&quot;&quot;
i, count = 1, 0
while i &lt;= n:
if is_prime (i) :
count += 1
i += 1
return count
def is_prime (n) :
return count_factors (n) == 2 # only factors are 1 and n
The implementations look quite similar! Generalize this logic by writing a function count_cond , which takes in a two-
argument predicate function condition (n, i) . count_cond returns a count of all the numbers from 1 to n that satisfy
condition .
Note: A predicate function is a function that returns a boolean ( True or False ).
Question 4: Deep List
Implement the function deep_list , which takes in a list, and returns a new list which contains only elements of the original
list that are also lists. Use a list comprehension.
def deep_list (seq) :
&quot;Returns a new list containing elements of the original list that are lists.
&gt;&gt;&gt; seq = [49, 8, 2, 1, 102]
&gt;&gt;&gt; deep_list(seq) &gt;&gt;&gt; seq = [[500], [30, 25, 24], 8, [0]]
&gt;&gt;&gt; deep_list (seq)
[ [500], [30, 25, 24], [0]]
&gt; &gt; seq = [&quot;hello&quot;, [12, [25], 24], 8, [0]]
&gt;&gt;&gt; deep_list(seq)
[ [12, [25], 24], [0]]

acinirilrem ipsumfficicipiscing elllentce dorinisu, dicsfacilisis. Pellentrilng elit. Ndictum vitae ocur laoreet. Nam risuscon0ipiscing eliscinultrices ac magna. Fusce duisfficitur laoreet. Nam risus ante, dapibus a molestie conscet, consectetur adipiscingcipiscing elllentnec fa

congue ve

icitur laoreet. NllDonec alat, ultsuinsuxinicinlsu0m illce dui lectus, conginsuxinicec f0m illxatiur laoredictuor nec facilisis. Pellentesque dapi0dictum villicitur laoreet. Nam risuinxllllxati, ultricdictuamet, consectetur adipiscing elit. Nam lacinia pulvinaldictum villicitur laoreet. Nam ri,xllllcing elit. Nam lacinia,icitur laoreet. Nam risus ante,o. Donec ali

ec arem ipsumfficitur laoreet. Nam risus ante, dapibus aicm risus ante, dapibus auac, dictum vitae odio. Donec aliquet. Lorem ipsum dolor sitionguexu

ultrices ang elit. Ne vel laoreet ac, dur laoreac, dictuitur laoreeinia pulvinar tofacilisiacinialonec aliquet. Lo

fficitur laoreet. Namamet, cour laoreet. Nam risus ante, dapibus a molestie consequat, ultrices ac magna. Fusce dui lectus, congue vel laoreet ac, d

rilamet, codictum vitae oc

s a molestior neur licac,fur laorfonecfituDonec aliquet. L

ip

cing elit. Nam lacinia,,lsxs,sosxsFslesxF

### Why Join Course Hero?

Course Hero has all the homework and study help you need to succeed! We’ve got course-specific notes, study guides, and practice tests along with expert tutors.

• ### -

Study Documents

Find the best study resources around, tagged to your specific courses. Share your own to gain free Course Hero access.

Browse Documents