Change Contents of the Bubble
Welcome to CS1315. Click on the python to add comments.

Looking for the book? They have it at the Engineer's Bookstore at 748 Marietta St NW. Here is there website: http://www.engrbookstore.com/ - Monica

Hotspots: Slides and CodeTA CornerComments?AnnouncementsFAQStatic Webspace
View this PageEdit this Page (locked)Uploads to this PageHistory of this PageHomeRecent ChangesSearchHelp Guide

Midterm Exam 2 Review Spring 2004: What do the programs do?

Comments, answers, and questions go here:

(Link back to Sp2004 Midterm 2 Review)


I got (y-x)= -2. Where am I going wrong?

Hmm...I got -3. I think the function basically counts up the number of a's and b's in the string and then returns a value for the differece between the two values...if the answer is negative it means there are more a's than b's, positive means there are more b's than a's. Am I right Mark?

For part c I know that the string represents the sample values and the + means they are positive and the - means they are negative, but what do these positive and negative values correspond to? Amplitude of sound? Frequency? Also, for part d I know the loop will run from 1 to 99, but is s set to a blank character in the begininning of the function? Will that space count as a 100th character?

Any guesses on (b)? What do the sample values represent – amplitude or frequency? s isn't a space – it's empty at the beginning. You can actually run these to test out your theories. Mark Guzdial

a) I also got -3...if anyone's confused, the y-x values I got were: -1,0,0,-1,-1,-2,-2,-3,-2,-2,-3.
b) I'm not sure I understand the question...single character elements run separately, so you would have 3 separate answers because each letter would be dealt with differently(?)
As far as the next two parts go, I have a clarification question...when the program says s = "", does that actually mean that s is the quotation marks or that you're referring to the above code, that
s = 0
? Karin Bowman

a) 2-5= -3
b) yes, it looks at every elementin the input whetehr it is a list or a string.
c) the string represents how many sample values are positive, negative, and zero (above, below, and on the zero line)
d) 99 because the range (i, 100) goes up to 99 only

for the above i meant (1, 100)

When we say s="", it means that S is a string, but it's empty. len(s) would be 0. Mark Guzdial


Could you please explain the whole len(s) thing? I do not understand that at all. What is len? Thanks!

Wow! What are these numbers all about? Can someone please please please take a minute and explain to me where these numbers are coming from? I am not seeing the relationship between abracadabra and 2-5=3. Thanks!
poof #2


len gives you the length of the input string.
>>> print len("abc")
3
Mark Guzdial

Aha!! Thanks, prof Guzdial.

Lauren, the program is basically counting the number of a's and b's present in the string. In the end, we're finding the difference between them.


Thanks. So, there are 2 b's and 5 a's. I get it. 2-5=3! I knew it wouldn't be that difficult!
poof #2

Can someone remind me what "==" means? I can't seem to have lost it.

Is the count additive i.e 1+2+3+4+5= 15 for a or is it just 5 a's equals 5. Also in Lauren's answer does it return a positive number or should the answer be -3?

"==" is a test for equality. "=" is assignment. Just adding one just adds one. Some answers will be negative. Mark Guzdial


It should be a -3. Sorry... basic math! So, it's a negative number.
poof #2

I'm a bit confused, why is a) not 3-6 = -3 since it's x = x+1 and y = Y +!


any help on b? still don't understand (doesn't matter who answers this question).

That's a good point! The answer is the same, since you are basically performing the operation 1-1=0 by adding +1 to both numbers. If you know, please respond!

This also adds the question, why are you adding one (+1) to both x and y if you are just going to subtract them???poof #2

You're adding one while counting them, in the loop. You're subtracting them to get the total difference, after the loop. Mark Guzdial




Link to this Page