0

I can't seem to find the answer anywhere for this, but I have a program i need to write for computer science 1(beginner friendly answers please). These are the instructions.

Write a program that will emulate this car counter.

For this program, there will be a continuous string of characters (split up in 10 lines of 50 characters each) in which "x" will represent space between bumps and the "o" will represent a "bump" of an axle.

Small vehicles will have the pattern "oo" surrounded by any number of x’s.

Medium vehicles will have the pattern “oxo” surrounded by x’s.

Large vehicles will have the pattern “oxoxxooo”.

To make it easier, a vehicle will not be split across different lines of data. For example, the following represents 2 small vehicles, followed by 2 medium vehicles, and lastly one large vehicle: xooxxxxooxxxxoxoxxxxoxoxxxxxxxoxoxxoooxxxxxxxxxxxx

Input: There are 10 lines of data, each 50 characters long.

    line1 = “xooxxxxooxxxxoxoxxxxoxoxxxxxxxoxoxxoooxxxxxxxxxxxx”
    line2 = “ooxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx”
    line3 = “oxoxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx”
    line4 = “oxoxxoooxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx”
    line5 = “xxxoxoxxoooxxxxxxoxoxxoooxxxxxxxxoxoxxoooxxxxxxxxx”
    line6 = “xoxoxxoooxxxxxooxxxooxxooxxooxxxxxooxxxxooxxxxooxx”
    line7 = “oxoxxoxoxxoxoxxoxoxxoxoxxxxxxxoxoxxxxxoxoxxxxxoxox”
    line8 = “xooxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxoo”
    line9 = “xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx”
    line10 = “xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxoxoxxooo”

The output should be:

    12 small
    11 medium
    7 large

I have tried using the count function, if 'xoox' in line1 scount += 1, and just cant seem to find a way to do this. Here is the code i have written so far.

line1 = "xooxxxxooxxxxoxoxxxxoxoxxxxxxxoxoxxoooxxxxxxxxxxxx"
line2 = "oxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
line3 = "oxoxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
line4 = "oxoxxoooxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
line5 = "xxxoxoxxoooxxxxxxoxoxxoooxxxxxxxxoxoxxoooxxxxxxxxx"
line6 = "xoxoxxoooxxxxxooxxxooxxooxxooxxxxxooxxxxooxxxxooxx"
line7 = "oxoxxoxoxxoxoxxoxoxxoxoxxxxxxxoxoxxxxxoxoxxxxxoxox"
line8 = "xooxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxoo"
line9 = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
line10 = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxoxoxxooo"

scount = 0
mcount = 0
lcount = 0

def replace(lst):
    scount = lst.count('xoox')
    mcount = lst.count('oxo')
    lcount = lst.count('oxoxxooo')
replace(line1)
print scount,mcount,lcount

I end up getting all 0's as my answer. So my question is, how do i count the number of 'xoox', 'oxo' and 'oxoxxooo's in a single lined string?

Mark Skelton
  • 3,663
  • 4
  • 27
  • 47
avbirm
  • 105
  • 1
  • 1
  • 9
  • "abcabc".count("ab") //output>>2 – Eray Balkanli Mar 07 '16 at 19:33
  • I do not intend to give answer to homework, but only guidance! There are already suggestions on how to solve your problem! but that is not the big deal! Where you have to work is to make sure that you do not count any pattern twice. You can observe that the pattern for medium car is a subpattern of the large car. You have to account for that. – innoSPG Mar 07 '16 at 19:43

2 Answers2

1

You need to return variables from inside functions otherwise these will be discarded after the function terminates. For example you could do it like this:

def replace(lst):
    scount = lst.count('xoox')
    mcount = lst.count('oxo')
    lcount = lst.count('oxoxxooo')
    return scount, mcount, lcount # Return them


scount, mcount, lcount = replace(line1) # Save the returned values
print scount,mcount,lcount # Print the saved values

This avoids (unintentional) overwriting of global variables and is therefore recommended except you really need to use globals, but in most cases it is sufficient to pass and return values to and from functions.

Notice however that my solution cannot be used as complete answer because you will overwrite your variables every time you call the function.

MSeifert
  • 145,886
  • 38
  • 333
  • 352
  • 2
    If it is your personal opinion not to help students with homework you shouldn't vote answers based on that but you should vote answers based on if the answer is clear, concise, and follows SO rules. – Mark Skelton Mar 07 '16 at 19:59
  • 1
    @MarkyPython, I understand your concern! If you look at the question itself, you can see that I commented the question and give guidance. – innoSPG Mar 07 '16 at 20:22
0

Your problem is that scount, mcount, and lcount are global variables, that are not referenced inside of replace. To change their values you must reference them like this:

def replace(lst):
    global scount, mcount, lcount
    #rest of function

Hope this helps!

Mark Skelton
  • 3,663
  • 4
  • 27
  • 47
  • Awesome! It works. So basically the global function makes the lcount,mcount and lcount local to the replace function? Thank you so much i was stuck for 30 minutes on this – avbirm Mar 07 '16 at 19:41
  • Yes what the global declaration does is it lets you reference variables that were created outside of a function. If you don't reference the global variables you end up making local variables inside the function which, even though they have the same name as the global variables the reference different objects in memory. – Mark Skelton Mar 07 '16 at 19:45
  • @MarkyPython, After answering to your comment on the other answer, and noticed that you also provided an answer, I will like to say that I like your answer better, you provide only guidance to help the student find the best way to the solution. That is all what my comment was about. I guess it was not rude. – innoSPG Mar 07 '16 at 20:35