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: Count 'em up

Comments, answers, and questions go here:

(Link back to Sp2004 Midterm 2 Review)


Here is your answer
a)
def countAs(string):
x=0
for i in string:
if i == "a":
x = x+1
print x


figure out the spacing on your own because I can't figure out how to do it here and i definately am not reading the FAO's page.

b)

def countAs(string):
x=0
y=0
z=0
for i in string:
if i == "a":
x = x+1
if i == "b":
y = y+1
if i == "c":
z = z+1
print x
print y
print z

the only problem is it doesn't say which count is which. Post that up if you can figure it out.


try print x,y,z
all on the same line

i'm having the same problem figuring out how to say which count is which. Can you help me?


print "A's",x,"B's",y,"C's",z. Also look at count in the book. Mark Guzdial


def countLetters(string):
  x=0
  y=0
  z=0
  for i in string:
    if i == "a":
      x = x+1
    if i == "b":
      y = y+1
    if i == "c":
      z = z+1
  print x,"As",y, "Bs",z, "Cs" 

Susan Kommeth

Where is this in the book or the power point slides, because I can't seem to find any examples of this? poof #2


What about calculating percentages of a's, b's, etc???


for some reason i'm having problems just loading the basics here..def countAs(string):
x=0
for i in string:
if i== "a" :
x=x+1
print x

it gives me an error saying 'for i in string' has something wrong with it... any ideas please? Sabrina Hassanali

Oh! I get this problem. Is this right: since you are trying to count the numbers of a,b,c's... you use x,y,z! x is equal to the number of a's you have, y is equal to the number of b's you have, and z is equal to the number of c's you have. So, if you said, there are 6 a's, you know that x=6. My only question is why are you doing +1 for x,y,z.... wouldn't this add one extra number to the count?

x='s 0 originally, so you have to add 1

Just remember, it is a for loop.poof #2

for calculating percentage of a's you would do this
def percA(string)
  a = 0
  num = 0
  for i in string:
    if i =="a":
      a = a + 1
    num = num + 1
  percentage = (x/num)*100
  print percentage 

Laura Bosworth

I don't think that'll work, Laura, unless you multiply x or num by 1.0. Otherwise, the percentage will always be zero. Mark Guzdial


Mark could you post the percent example that you did in Breakout today...similar to:
"abracadabra".count("a") ?
thanks

def percents(string):
  print "A's",string.count("a")/(1.0 * len(string))
  print "B's",string.count("b")/(1.0 * len(string))
  print "C's",string.count("c")/(1.0 * len(string))

Mark Guzdial

When i ran the above code, I got 0's...that can't be right...right?

print ("Number of A", x)
print ("Number of B", y)
print ("Number of C", z)

this will say Number of letter and then the actual count.

Lee Yu

def percents(string):
  print "A's",string.count("a")
  print "B's",string.count("b")
  print "C's",string.count("c")


this one took a while. the percentages were tough but only because i left out the 1.0 length, but what is posted should work.
def countAs(string):
a = 0
b = 0
c = 0
for x in string:
if x == "a"
a = a + 1
if x == "b"
b = b + 1
if x == "c"
c = c + 1
length = 1.0len(string)
print a/length, "%A's"
print b/length, "%B's"
print c/length, "%C's"


your code does not work


Hey everyone, I was a tad confused why we all started in on this percentage stuff....if you plug this code into JES, you get the right thing for "Count 'em up" (b)

def countABCs(string):
  x=0
  y=0
  z=0
  for i in string:
    if i == "a":
      x = x+1
    if i == "b":
      y = y+1
    if i == "c":
      z = z+1
  print x,"As",y, "Bs",z, "Cs" 


Christina Sedor


See the comments page, Christina – I challenged everyone to do these as percentages. Mark Guzdial


In't this a simpler way to do the first part of this question?

def countAs(input)
  new=input.split("")
  print new.count("a")
 


Maybe this doesn't work, but Prof. Guzdial suggested looking at the count method (pg 219 in the book).


Jonathan Laing

You don't need the split, Jonathan. Strings understand count. Mark Guzdial

Thanks.

So, it would just be this, correct?:
def countAs(input)
  print input.count("a")
 





Link to this Page