Re: [Tutor] Select a string

2017-09-06 Thread Cameron Simpson

On 06Sep2017 09:12, Pat Martin  wrote:

I got it working for all the tests, but the code is ugly. I think regex or
using string methods would have been easier and neater but like I said we


Always go for string methods first. Regexps are cryptic and easy to get wrong, 
and relatively hard to debug when they are wrong. And they're more expensive to 
create and to run.


The flip side is that there are things which can be said easily and concisely 
with regexps. But you should generally consider string methods first.



hadn't covered it in the class yet so didn't think I should use it. I am
embarrassed by how bad the code looks to me.


That comes with practice: getting intuition about what reads well, and what 
tends to be error prone or fragile.


For the exercise, have a relook at the code you have. What would make it easier 
to read? Easier to debug? Better variable names? Even trivial changes like 
changing "firstb" to "first_b" and "theo" to "the_o" can help. Would things be 
clearer with a leading comment explaining the purposes of the state variables:


 # Scan a string for instances of "bob", including overlaps like "bobob".
 # State variables:
 #  first_b: set on encountering the first "b" of a potential "bob"
 #  the_o: set on encountering the :o"  of a potential "bob"; only set if the
 #preceeding character was the "b" (first_b).

Partiularly for little state machines like yours I often find such descriptions 
useful.  They help get the logic clear in my own mind _before_ writing the 
loop, and help you and others understand the code later when you come back to 
debug.


It can also help to comment the individual if-statements. Eg:

 # recognise the "o" of "bob" if the preceeding character was "b"

Cheers,
Cameron Simpson  (formerly c...@zip.com.au)
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Select a string

2017-09-06 Thread Pat Martin
I got it working for all the tests, but the code is ugly. I think regex or
using string methods would have been easier and neater but like I said we
hadn't covered it in the class yet so didn't think I should use it. I am
embarrassed by how bad the code looks to me.

I am self taught and have written a couple useful scripts but still very
much a beginner and there are definite holes in my knowledge. Hoping this
class fills in some of the holes.

Thanks again all for the suggestions and the tips.

On Wed, Sep 6, 2017 at 8:01 AM, Pat Martin  wrote:

> I knew it was an assignment I just couldn't see it no matter how hard I
> looked at the code. Thanks for the tips on printing the variables, I was
> printing them right after the if statements not at the beginning and ending
> of the loop. I will remember that about True/False and comparisons thanks
> for that as well.
>
> The reason I haven't used regex/methods/etc is it hasn't been covered yet,
> we have only covered basic types, for, while and if statements.
>
> On Wed, Sep 6, 2017 at 1:54 AM, Alan Gauld via Tutor 
> wrote:
>
>> On 06/09/17 06:34, Pat Martin wrote:
>>
>> > but my script returns 0. Since they want it to find 2 from the bobob in
>> the
>> > string using "bob in s" doesn't work (it only returns 1).
>>
>> Your code has bugs and Cameron has addressed those along
>> with general advice on how to debug your code in future.
>> However there are other string methods that could save
>> you some work.
>>
>> At the >>> prompt use dir(str) to see a list of all
>> the methods available. Then use help(str.methodName)
>> to find out what each one does.
>> (Or just browse the string documentation on the web site!)
>>
>> In this case you might find some of the following methods useful:
>>
>> str.count
>> str.find
>> str.index
>> str.startswith
>>
>> and of course you can use string slicing to access a
>> substring. Combining slicing with the parameters of
>> find or index should make your task easier.
>>
>> Finally, if you want to carry on with your loop approach
>> you might find that enumerate is useful here since it
>> gives you the index as well as the character. That
>> will allow you to look-ahead to see if the next two
>> characters are 'ob'... That should simplify things.
>>
>> --
>> Alan G
>> Author of the Learn to Program web site
>> http://www.alan-g.me.uk/
>> http://www.amazon.com/author/alan_gauld
>> Follow my photo-blog on Flickr at:
>> http://www.flickr.com/photos/alangauldphotos
>>
>>
>> ___
>> Tutor maillist  -  Tutor@python.org
>> To unsubscribe or change subscription options:
>> https://mail.python.org/mailman/listinfo/tutor
>>
>
>
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Select a string

2017-09-06 Thread Pat Martin
I knew it was an assignment I just couldn't see it no matter how hard I
looked at the code. Thanks for the tips on printing the variables, I was
printing them right after the if statements not at the beginning and ending
of the loop. I will remember that about True/False and comparisons thanks
for that as well.

The reason I haven't used regex/methods/etc is it hasn't been covered yet,
we have only covered basic types, for, while and if statements.

On Wed, Sep 6, 2017 at 1:54 AM, Alan Gauld via Tutor 
wrote:

> On 06/09/17 06:34, Pat Martin wrote:
>
> > but my script returns 0. Since they want it to find 2 from the bobob in
> the
> > string using "bob in s" doesn't work (it only returns 1).
>
> Your code has bugs and Cameron has addressed those along
> with general advice on how to debug your code in future.
> However there are other string methods that could save
> you some work.
>
> At the >>> prompt use dir(str) to see a list of all
> the methods available. Then use help(str.methodName)
> to find out what each one does.
> (Or just browse the string documentation on the web site!)
>
> In this case you might find some of the following methods useful:
>
> str.count
> str.find
> str.index
> str.startswith
>
> and of course you can use string slicing to access a
> substring. Combining slicing with the parameters of
> find or index should make your task easier.
>
> Finally, if you want to carry on with your loop approach
> you might find that enumerate is useful here since it
> gives you the index as well as the character. That
> will allow you to look-ahead to see if the next two
> characters are 'ob'... That should simplify things.
>
> --
> Alan G
> Author of the Learn to Program web site
> http://www.alan-g.me.uk/
> http://www.amazon.com/author/alan_gauld
> Follow my photo-blog on Flickr at:
> http://www.flickr.com/photos/alangauldphotos
>
>
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> https://mail.python.org/mailman/listinfo/tutor
>
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Select a string

2017-09-06 Thread Alan Gauld via Tutor
On 06/09/17 06:34, Pat Martin wrote:

> but my script returns 0. Since they want it to find 2 from the bobob in the
> string using "bob in s" doesn't work (it only returns 1). 

Your code has bugs and Cameron has addressed those along
with general advice on how to debug your code in future.
However there are other string methods that could save
you some work.

At the >>> prompt use dir(str) to see a list of all
the methods available. Then use help(str.methodName)
to find out what each one does.
(Or just browse the string documentation on the web site!)

In this case you might find some of the following methods useful:

str.count
str.find
str.index
str.startswith

and of course you can use string slicing to access a
substring. Combining slicing with the parameters of
find or index should make your task easier.

Finally, if you want to carry on with your loop approach
you might find that enumerate is useful here since it
gives you the index as well as the character. That
will allow you to look-ahead to see if the next two
characters are 'ob'... That should simplify things.

-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos


___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Select a string

2017-09-06 Thread Ben Southwell
May I suggest you use regular expressions?

You can check it out here (take note of the difference between search,
findall etc):
https://docs.python.org/3/library/re.html

Essentially it is a standard library built for matching patterns in
strings. It is a powerful tool that is kind of it's own language in itself
(but other languages have re libraries too so it's definitely worth
learning). The magic of re is learning how to construct your patterns.

re.findall()  will return all non-overlapping matches of a pattern in a
given string.  However, the pattern I've used below will match overlapping
bobs, this is because of the lookahead assertion (the "?=" part of the
pattern). I won't go into regex anymore, it's something I believe you learn
as you do otherwise its way too syntax to learn at once.

 DISCLAIMER: I have only just learnt regex myself lately so if anyone wants
to charm in with a better method please to do so!

Here is a snippet which solves your problem

#!/usr/bin/env python3

import re

pat = r'(?=(bob))'
s = 'azcbobobegghakl'

result = re.findall(pat, s)

print(result)
print(len(result))



On 6 September 2017 at 15:49, Cameron Simpson  wrote:

> On 05Sep2017 22:34, Pat Martin  wrote:
>
>> I am trying to write a program for a programming class that finds the
>> number of a specific string (bob) in a string of characters. I am using
>> one
>> of the sample strings they give me and it should find 2 instances of bob
>> but my script returns 0. Since they want it to find 2 from the bobob in
>> the
>> string using "bob in s" doesn't work (it only returns 1). My eyes are
>> crossing looking at the code, can someone give me a hint on what I am
>> missing that causes this to not give me the correct answer of 2.
>>
>> #!/usr/bin/env python3
>>
>> s = 'azcbobobegghakl'
>>
>> count = 0
>> theo = False
>> firstb = False
>>
>> for i in s:
>>if i == 'b':
>>firstb == True
>>
>
> This line is a boolean expression using "==", not an assignment using "=".
> As a consequence firstb is never set to True and the rest of the logic
> never fires.
>
> It is legal in Python to just put an expression on a line.
>
> It is usually worth putting in print() calls to debug things like this. I
> put:
>
>  print(i, firstb, theo, count)
>
> at the start of the loop and learned that firstb never becomes true. I
> also put the same print statement at the bottom.
>
> The advantage of hte above print statement is that it shows you the
> character from the string along with each set of values. That way you can
> scan down the output to the "bob" part and look for correct behaviour.
>
> I that hadn't helped I've have stuck print after each "if" until the
> problem became glaringly obvious.
>
> BTW, it isn't good to write tests like:
>
>  if theo == True:
>
> "theo" a Boolean anyway. Just say:
>
>  if theo:
>
> It reads more naturally as well.
>
> Cheers,
> Cameron Simpson  (formerly c...@zip.com.au)
>
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> https://mail.python.org/mailman/listinfo/tutor
>



-- 
Best Regards,

Ben Southwell
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Select a string

2017-09-06 Thread Cameron Simpson

On 05Sep2017 22:34, Pat Martin  wrote:

I am trying to write a program for a programming class that finds the
number of a specific string (bob) in a string of characters. I am using one
of the sample strings they give me and it should find 2 instances of bob
but my script returns 0. Since they want it to find 2 from the bobob in the
string using "bob in s" doesn't work (it only returns 1). My eyes are
crossing looking at the code, can someone give me a hint on what I am
missing that causes this to not give me the correct answer of 2.

#!/usr/bin/env python3

s = 'azcbobobegghakl'

count = 0
theo = False
firstb = False

for i in s:
   if i == 'b':
   firstb == True


This line is a boolean expression using "==", not an assignment using "=". As a 
consequence firstb is never set to True and the rest of the logic never fires.


It is legal in Python to just put an expression on a line.

It is usually worth putting in print() calls to debug things like this. I put:

 print(i, firstb, theo, count)

at the start of the loop and learned that firstb never becomes true. I also put 
the same print statement at the bottom.


The advantage of hte above print statement is that it shows you the character 
from the string along with each set of values. That way you can scan down the 
output to the "bob" part and look for correct behaviour.


I that hadn't helped I've have stuck print after each "if" until the problem 
became glaringly obvious.


BTW, it isn't good to write tests like:

 if theo == True:

"theo" a Boolean anyway. Just say:

 if theo:

It reads more naturally as well.

Cheers,
Cameron Simpson  (formerly c...@zip.com.au)
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Select a string

2017-09-05 Thread Ben Finney
Pat Martin  writes:

> My eyes are crossing looking at the code, can someone give me a hint
> on what I am missing that causes this to not give me the correct
> answer of 2.

It's good to admit that looking at the code has ceased to help :-)

So, try a different approach. Without using the code, can you describe
your algorithm? Make a pseudo-code description of how you expect to
transform the input to the output.

For example, I might describe the algorithm for reversing a text
string::

* Given a text string named ‘input_text’
* Set the output, named ‘output_text’, to the value "".
* Set the index (named ‘index’) to the character length of ‘input_text’.
* While ‘index’ is greater than 0:
  * Subtract 1 from ‘index’.
  * Get the character at index ‘index’ of ‘input_text’, assign the
name ‘input_char’ to that character.
  * Append ‘input_char’ to ‘output_text’.
* Return the value of ‘output_text’.

Can you describe your algorithm that way, so we can see what is your
intention for the code?

-- 
 \   “Faith is the determination to remain ignorant in the face of |
  `\ all evidence that you are ignorant.” —Shaun Mason |
_o__)  |
Ben Finney

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


[Tutor] Select a string

2017-09-05 Thread Pat Martin
Hello all,

I am trying to write a program for a programming class that finds the
number of a specific string (bob) in a string of characters. I am using one
of the sample strings they give me and it should find 2 instances of bob
but my script returns 0. Since they want it to find 2 from the bobob in the
string using "bob in s" doesn't work (it only returns 1). My eyes are
crossing looking at the code, can someone give me a hint on what I am
missing that causes this to not give me the correct answer of 2.

#!/usr/bin/env python3

s = 'azcbobobegghakl'

count = 0
theo = False
firstb = False

for i in s:
if i == 'b':
firstb == True
if (i == 'b') and (theo == True):
count += 1
if (i =='o') and (firstb == True):
theo = True
if (i != 'b') and (i != 'o'):
theo = False
firstb = False


print(count)

Thanks,
Pat
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor