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.

Screen Shot 2020-02-16 at 4.34.11 PM.png

Screen Shot 2020-02-16 at 4.26.33 PM.png

Screen Shot 2020-02-16 at 4.34.01 PM.png

Screen Shot 2020-02-16 at 4.33.52 PM.png


Thank you again!

Screen Shot 2020-02-16 at 4.26.33 PM.png
def deep_list (seq) :
""Returns a new list containing elements of the original list that are lists.
>>> seq = [49, 8, 2, 1, 102]
>>> deep_list(seq) >>> seq = [[500] , [30, 25, 24], 8, [0]]
>>> deep_list (seq)
[ [500], [30, 25, 24], [0]]
>>> seq = ["hello", [12, [25], 24], 8, [0]]
>>> deep_list(seq)
[ [12, [25], 24], [0]]
return [i if type(x) == list for i in seq]
Screen Shot 2020-02-16 at 4.33.52 PM.png
def count_cond (condition, n) :
>>> def divisible(n, i):
return n % i == 0
>>> count_cond (divisible, 2) # 1, 2
2
>>> count_cond (divisible, 4) # 1, 2, 4
B
count_cond (divisible, 12) # 1, 2, 3, 4, 6, 12
6
>>> def is_prime(n, i):
return count_cond (divisible, i) == 2
>>> count_cond(is_prime, 2) # 2
1
>>> count_cond(is_prime, 3) # 2, 3
2
>>> count_cond( is_prime, 4) # 2, 3
2
count_cond (is_prime, 5) # 2, 3, 5
3
>>> count_cond(is_prime, 20) # 2, 3, 5, 7, 11, 13, 17, 19
3
def count_cond (n) :
i, count = 1, 0
while i <= n:
if is_prime (i) :
count += 1
else: divisible(i)
count += 1
else:
i += 1
return count
Screen Shot 2020-02-16 at 4.34.01 PM.png
Question 6: Count van Count
Consider the following implementations of count_factors and count_primes :
def count_factors (n) :
"Return the number of positive factors that n has.""
i, count = 1, 0
while i <= n:
if n $ i == 0:
count += 1
i += 1
return count
def count_primes (n) :
""Return the number of prime numbers up to and including n.""
i, count = 1, 0
while i <= 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 ).
Screen Shot 2020-02-16 at 4.34.11 PM.png
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) :
"Returns a new list containing elements of the original list that are lists.
>>> seq = [49, 8, 2, 1, 102]
>>> deep_list(seq) >>> seq = [[500], [30, 25, 24], 8, [0]]
>>> deep_list (seq)
[ [500], [30, 25, 24], [0]]
> > seq = ["hello", [12, [25], 24], 8, [0]]
>>> deep_list(seq)
[ [12, [25], 24], [0]]
" YOUR CODE HERE *"

Step-by-step answer

The student who asked this found it Helpful

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

Subscribe to view the full answer

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
  • -

    Question & Answers

    Get one-on-one homework help from our expert tutors—available online 24/7. Ask your own questions or browse existing Q&A threads. Satisfaction guaranteed!

    Ask a Question
Let our 24/7 Computer Science tutors help you get unstuck! Ask your first question.
A+ icon
Ask Expert Tutors You can ask You can ask You can ask (will expire )
Answers in as fast as 15 minutes