Re: [Tutor] Homework problem
The instructions are to add one to each number. So the expected result would be: I got 433 when I counted, but Jim got 434 which is a lot for only 7 cats, or were there 13 cats? From: Steven D'Aprano To: "tutor@python.org" Sent: Wednesday, July 20, 2011 6:11 PM Subject: Re: [Tutor] Homework problem Ken Baclig wrote: > Hi, > > I'm trying to make a function that receives text (a string) as an argument > and returns the same text (as string), but with 1 added to each word that is > a number. What counts as a number? In the string: "Hello world 1234 ham spam" which of these do you expect to get back? "Hello world 2345 ham spam" # Add one to each digit. "Hello world 1235 ham spam" # Add one to each number. What about strings without spaces like "foo123bar"? I'm going to assume that you mean to add 1 to any digit, not just to complete numbers. > I need help getting started. > > So far, I have: > > def FindNumbers(a_string): > > for index, char in enumerate(a_string): > if char.isdigit(): > a_string[index] = This can't work, because strings are immutable. They cannot be changed in place. You have to form a new string. The approach I would take is something like this. Here's a version which doesn't actually add one to each digit, but merely turns any digit into an asterisk. Your job is to make it do what it is supposed to do. def add_one_to_digits(a_string): # Give the function a name that says what it does. It doesn't # just FindNumbers, that name is completely inappropriate. result = [] # Holder to build up a new string. for char in a_string: if char.isdigit(): char = "*" # Anything else gets passed through unchanged. result.append(char) # Assemble the characters into a string. return ''.join(result) > def Test(): > sometext = "I got 432 when I counted, but Jim got 433 which is a lot >foronly 6 cats, or were there 12 cats?" > FindNumbers(sometext) > > Test() Test functions should actually test something. Try this instead: def test(): source = ("I got 432 when I counted, but Jim got 433 which " "is a lot for only 6 cats, or were there 12 cats?") expected = ("I got 543 when I counted, but Jim got 544 which " "is a lot for only 7 cats, or were there 23 cats?") actual = add_one_to_digits(source) if actual == expected: print("Test passes!") else: print("Test fails!") print("Expected '%s'" % expected) print("but actually got '%s'" % actual) By the way, the test case is not very good, because there is one digit which is special compared to the others, and it doesn't get tested. Think about it... out of the 10 possible digits, 9 of them are obvious: 0 -> 1 1 -> 2 2 -> 3 etc. but one digit is not obvious. Can you see which one? So you need to include that digit in the test case, so you know it gets handled correctly. -- Steven ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Homework problem
Ken Baclig wrote: Hi, I'm trying to make a function that receives text (a string) as an argument and returns the same text (as string), but with 1 added to each word that is a number. What counts as a number? In the string: "Hello world 1234 ham spam" which of these do you expect to get back? "Hello world 2345 ham spam" # Add one to each digit. "Hello world 1235 ham spam" # Add one to each number. What about strings without spaces like "foo123bar"? I'm going to assume that you mean to add 1 to any digit, not just to complete numbers. I need help getting started. So far, I have: def FindNumbers(a_string): for index, char in enumerate(a_string): if char.isdigit(): a_string[index] = This can't work, because strings are immutable. They cannot be changed in place. You have to form a new string. The approach I would take is something like this. Here's a version which doesn't actually add one to each digit, but merely turns any digit into an asterisk. Your job is to make it do what it is supposed to do. def add_one_to_digits(a_string): # Give the function a name that says what it does. It doesn't # just FindNumbers, that name is completely inappropriate. result = [] # Holder to build up a new string. for char in a_string: if char.isdigit(): char = "*" # Anything else gets passed through unchanged. result.append(char) # Assemble the characters into a string. return ''.join(result) def Test(): sometext = "I got 432 when I counted, but Jim got 433 which is a lot foronly 6 cats, or were there 12 cats?" FindNumbers(sometext) Test() Test functions should actually test something. Try this instead: def test(): source = ("I got 432 when I counted, but Jim got 433 which " "is a lot for only 6 cats, or were there 12 cats?") expected = ("I got 543 when I counted, but Jim got 544 which " "is a lot for only 7 cats, or were there 23 cats?") actual = add_one_to_digits(source) if actual == expected: print("Test passes!") else: print("Test fails!") print("Expected '%s'" % expected) print("but actually got '%s'" % actual) By the way, the test case is not very good, because there is one digit which is special compared to the others, and it doesn't get tested. Think about it... out of the 10 possible digits, 9 of them are obvious: 0 -> 1 1 -> 2 2 -> 3 etc. but one digit is not obvious. Can you see which one? So you need to include that digit in the test case, so you know it gets handled correctly. -- Steven ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Homework problem
Ken Baclig wrote: Does this look right? Still a little confused Nope. Notice that Marc said NOT to operate on characters but to split() the string into a wordlist. Then test for each word in the wordlist to see if it isdigit(). Igf so then convert the word to an int() and add one. Then convert the new int back to a str()and insert back into your wordlist. Finally join() your wordlist with spaces to get your original "sentence" back. if char.isdigit(): num = int(char) + 1 a_string[index] = str(num) print a_string If you need to modify them - by adding 1, for example - you need to > refer to them by index instead, and the quickest way to do that is > "for x in range(len(words)): print words[x]". Or use the enumerate() function you started with HTH, Alan G. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Homework problem
Does this look right? Still a little confused if char.isdigit(): num = int(char) + 1 a_string[index] = str(num) print a_string From: Marc Tompkins To: Ken Baclig Cc: "tutor@python.org" Sent: Wednesday, July 20, 2011 3:45 PM Subject: Re: [Tutor] Homework problem On Wed, Jul 20, 2011 at 2:54 PM, Ken Baclig wrote: Hi, > > >I'm trying to make a function that receives text (a string) as an argument and >returns the same text (as string), but with 1 added to each word that is a >number. > > >I need help getting started. > > >So far, I have: > > >def FindNumbers(a_string): > > > for index, char in enumerate(a_string): > if char.isdigit(): >a_string[index] = > > > > >def Test(): > > > sometext = "I got 432 when I counted, but Jim got 433 which is a lot >foronly 6 cats, or were there 12 cats?" > > FindNumbers(sometext) > > >Test() First of all, don't enumerate() the string; split() it instead - this will give you a list of words instead of characters. Then, look at each item in that list; check to see whether it's numeric - isdigit() works for this. If it _is_ numeric, convert it to an int, add one to it, and turn it back into a string. Join the list back into a string, and you're done. Note: you can step through the items in a list by saying (for example) "for word in words:" - but if you do it that way you can't modify any of the items. If you need to modify them - by adding 1, for example - you need to refer to them by index instead, and the quickest way to do that is "for x in range(len(words)): print words[x]". That was a bunch of broad hints - if you need help putting them together, feel free to ask. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Homework problem
On Wed, Jul 20, 2011 at 2:54 PM, Ken Baclig wrote: > Hi, > > I'm trying to make a function that receives text (a string) as an argument > and returns the same text (as string), but with 1 added to each word that is > a number. > > I need help getting started. > > So far, I have: > > def FindNumbers(a_string): > > for index, char in enumerate(a_string): > if char.isdigit(): > a_string[index] = > > > def Test(): > > sometext = "I got 432 when I counted, but Jim got 433 which is a lot > foronly 6 cats, or were there 12 cats?" > > FindNumbers(sometext) > > Test() > First of all, don't enumerate() the string; split() it instead - this will give you a list of words instead of characters. Then, look at each item in that list; check to see whether it's numeric - isdigit() works for this. If it _is_ numeric, convert it to an int, add one to it, and turn it back into a string. Join the list back into a string, and you're done. Note: you can step through the items in a list by saying (for example) "for word in words:" - but if you do it that way you can't modify any of the items. If you need to modify them - by adding 1, for example - you need to refer to them by index instead, and the quickest way to do that is "for x in range(len(words)): print words[x]". That was a bunch of broad hints - if you need help putting them together, feel free to ask. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] Homework problem
Hi, I'm trying to make a function that receives text (a string) as an argument and returns the same text (as string), but with 1 added to each word that is a number. I need help getting started. So far, I have: def FindNumbers(a_string): for index, char in enumerate(a_string): if char.isdigit(): a_string[index] = def Test(): sometext = "I got 432 when I counted, but Jim got 433 which is a lot foronly 6 cats, or were there 12 cats?" FindNumbers(sometext) Test()___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] Homework Problem Flaming (was: Help!)
On 03/03/2011 04:58 PM, James Reynolds wrote: > You are almost assuredly going to get flamed for not having a descriptive > title and for asking what is obviously homework questions > Maybe for not having a descriptive title, but there's nothing wrong with coming to the list with homework! The requirements are that you've put some work into it, you show your code, you say what is should be doing that it isn't, and that you explain what you've tried doing previously. At least, those are what I look for. Even better that he said right up front that it was homework. With homework problems, instead of saying "Hey, replace lines 42-48 with foo", saying "Look into the bar module, it bazifies the proper value for you". Teaching people to learn better for themselves instead of hand-feeding them the answers. This list does a really good job with it, IMO. -- Corey Richardson ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Homework Problem
"Shawn Blazer" wrote This problem told me to use map and filter, so how would I use that to solve it? Because its homework we won't solve it for you, we will only answer questions or suggest approaches. From your earlier post it looks like you have all the tools: recursion, map and filter. Now what do you not understand? What have you tried? FWIW My tutorial covers map and filter in the "Functional Programming" topic. -- Alan Gauld Author of the Learn to Program web site http://www.alan-g.me.uk/ ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Homework Problem
On Fri, 28 May 2010 19:11:13 -0400 "Shawn Blazer" wrote: > > This problem told me to use map and filter, so how would I use that to > solve it? [some piece of interactive session] > Thanks! So, where's the problem? Denis vit esse estrany ☣ spir.wikidot.com ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] Homework Problem
This problem told me to use map and filter, so how would I use that to solve it? remove ( 5 , 6 ) 6 remove ( 5 , 5 ) remove ( 1 , [1 , [1 , [2 , 13]] , 1 , [2] , 5] ) [[[2 , 13]] , [2] , 5] remove ( 2 , [1 , [1 , [2 , 13]] , 1 , [2] , 5] ) [1 , [1 , [13]] , 1 , [] , 5] Thanks! -- Using Opera's revolutionary e-mail client: http://www.opera.com/mail/ ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor