Homework 3.3 specification

4
3

For Homework 3, exercise 3, the specification is as follows:

#Define a procedure, product_list,
#takes as input a list of numbers,
#and returns a number that is
#the result of multiplying all
#those numbers together.

#product_list([9]) => 9
#product_list([1,2,3,4]) => 24

My question is, do we have to handle an empty list input case ? (I mean, do we assume the list contains at least one element ?)

asked 06 Mar '12, 22:41

Ioan-Victor%20Pantazi's gravatar image

Ioan-Victor ...
1.2k41541
accept rate: 63%

edited 06 Mar '12, 23:10

fnenu-1's gravatar image

fnenu-1 ♦♦
18.5k1981231

@PeterUdacity : as per standard english usage and mathematical meaning, shouldn't the procedure be called list_product instead ?

(08 Mar '12, 10:38) Olivier GERARD Olivier%20GERARD's gravatar image

7 Answers:

Usually the product of no numbers is 1. That's more or less the reasoning behind


3**0 == 1

and even

0**0 == 1

which is a little easier to question. Similarly, the sum of no numbers is 0, thus

0*3 == 0

and

0*0 == 0

link

answered 06 Mar '12, 23:10

Larry%20Wilson's gravatar image

Larry Wilson
1.3k41222

edited 07 Mar '12, 00:37

fnenu-1's gravatar image

fnenu-1 ♦♦
18.5k1981231

Of course, thank you.
I really should sleep instead of coding.

(07 Mar '12, 00:37) fnenu-1 ♦♦ fnenu-1's gravatar image

My edit was to escape the * characters so they show up.

(07 Mar '12, 00:38) fnenu-1 ♦♦ fnenu-1's gravatar image
8

If there was an empty list the result would be 1. I don't test it though.

(07 Mar '12, 01:09) PeterUdacity ♦♦ PeterUdacity's gravatar image
1

Interesting to see that Python indeed gives 0**0 == 1, while in mathematics zero to the power zero is considered undefined

(07 Mar '12, 05:43) mrBB-4 mrBB-4's gravatar image

Just to clarify @larry96's answer it's

any_number**0

equal to 1 and as an example 3**1=0

(12 Mar '12, 06:08) Emilio Garcia-1 Emilio%20Garcia-1's gravatar image

I think the code should return 0 if it is empty [] but return the biggest if not empty. I have never heard of what you guys mean by the product of two zeros is 1.

link

answered 07 Mar '12, 00:40

Eenvincible's gravatar image

Eenvincible
3.6k164179

1

Not the product, the exponentiation. 0 raised to the 0-th power is 1.

(07 Mar '12, 00:44) Larry Wilson Larry%20Wilson's gravatar image

@Eenvincible - I'm talking about HW 3.3, not 3.4
@larry96 - thanks, I'll return 1 until further notice.

(07 Mar '12, 00:58) Ioan-Victor ... Ioan-Victor%20Pantazi's gravatar image

I have the same question:
product_list([]) -> 1? or 0?

link

answered 07 Mar '12, 05:18

renathy's gravatar image

renathy
172

It's 1. Check the comments under the accepted (green) answer. It won't be tested though.

(07 Mar '12, 11:09) fnenu-1 ♦♦ fnenu-1's gravatar image

This(actually something similar) has been covered in a unit 3 quiz.

link

answered 06 Mar '12, 23:01

elssar's gravatar image

elssar
19.2k2563155

1

If you're referring to checking the length, I'm already doing that. If you're referring to the similar sum_list quiz problem, he returns 0 for an empty list, and for sums that would be correct, but for multiplying, you could argue 1 would be better (neutral for multiplication). I could also just return None or False or whatever.

(07 Mar '12, 00:40) Ioan-Victor ... Ioan-Victor%20Pantazi's gravatar image

If we are supposed to handle it, what should the output be? At the moment my code returns 0 when it's empty, but should it return None or a message or what?

Edit: now returning 1.

Thanks @larry96

link

answered 06 Mar '12, 23:08

fnenu-1's gravatar image

fnenu-1 ♦♦
18.5k1981231

edited 07 Mar '12, 00:39

I have the same question, slothmaster. I currently have it returning 0, but would like some clarification.

link

answered 06 Mar '12, 23:51

pseudonoob's gravatar image

pseudonoob
200312

I have the same question: product_list([]) -> 1? or 0?
If I have a product_list([0,1]) -> is 0?

link

answered 08 Mar '12, 20:08

slin-1's gravatar image

slin-1
981112

edited 08 Mar '12, 20:12

product_list([]) -> 1
product_list([0,1]) -> 0 (since 0*1=0)

(08 Mar '12, 20:09) fnenu-1 ♦♦ fnenu-1's gravatar image
Your answer
Question text:

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text](http://url.com/ "Title")
  • image?![alt text](/path/img.jpg "Title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported

Tags:

×15,219
×1,718
×45

Asked: 06 Mar '12, 22:41

Seen: 650 times

Last updated: 13 Mar '12, 16:35