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?