[Beginner/intermediate] Do you know your string methods?

[Beginner/intermediate] Do you know your string methods?

Postby Mekire » Thu Jan 30, 2014 3:22 pm

Well let's give this a try then.
The original problem here comes from exercism.io.

But let's try and make it a little harder.

The basic problem:
Code: Select all
Create a module called bob.py
Within it create a class Bob
The class should have a method hey(self, prompt)

Depending on the prompt the method must return the following:
Code: Select all
Bob answers 'Sure.' if you ask him a question.
He answers 'Woah, chill out!' if you yell at him.
He says 'Fine. Be that way!' if you address him without actually saying anything.
He answers 'Whatever.' to anything else.

Test your script with this script (placed in the same directory).
Note that because of the use of the u"string" syntax this must be run in either python 2.7 or 3.3 (python 3 had previously removed this syntax but re-added it /sigh)

This requires basic knowledge of string methods and the ability to understand the errors the unit test script gives you.
But it is too easy.

The challenge:
Normal mode: No ifs.
Hard mode: No square brackets [ ]. No ifs.
Golf mode: 135 characters or less any way you can.

For those of you for whom this might still be too easy, maybe hold off on answering a bit.
-Mek

Updates:
Stranac currently has the best golf with 128 characters.
Mekire currently has the best golf with 126 characters.
Stranac currently has the best golf with 121 characters!
User avatar
Mekire
 
Posts: 1015
Joined: Thu Feb 07, 2013 11:33 pm
Location: Amakusa, Japan

Re: [Beginner/intermediate] Do you know your string methods?

Postby pedros » Thu Jan 30, 2014 4:58 pm

Code: Select all
class Bob:
   def hey(self,prompt=raw_input()):
      p=prompt
      t={0:'Sure.',1:'Woah, chill out!',2:'Fine. Be that way!',3:'Whatever.'}
      return(not p.strip()and t.get(2))or(p.isupper()and t.get(1))or (p.endswith('?')and t.get(0))or t.get(3)


passes all the unit tests, normal and hard mode; not fully golfed...
pedros
 
Posts: 3
Joined: Mon Feb 11, 2013 10:55 am

Re: [Beginner/intermediate] Do you know your string methods?

Postby stranac » Thu Jan 30, 2014 6:24 pm

My best so far is 141.
Not sure if I can do any less...

EDIT: Nvm, got it down to 133
Friendship is magic!

R.I.P. Tracy M. You will be missed.
User avatar
stranac
 
Posts: 1210
Joined: Thu Feb 07, 2013 3:42 pm

Re: [Beginner/intermediate] Do you know your string methods?

Postby Mekire » Thu Jan 30, 2014 10:50 pm

@Pedros: Very nice. One of my solutions is very similar, but without the ands and ors.

@Stranac: Really, 133? Are you counting the empty spaces like the " " between "class Bob"? If so you've got me beat.

-Mek
User avatar
Mekire
 
Posts: 1015
Joined: Thu Feb 07, 2013 11:33 pm
Location: Amakusa, Japan

Re: [Beginner/intermediate] Do you know your string methods?

Postby stranac » Thu Jan 30, 2014 11:06 pm

Mekire wrote:@Stranac: Really, 133? Are you counting the empty spaces like the " " between "class Bob"?

Of course. Both spaces and the newline.
My code is pretty square-bracket-heavy, though, so no number 2 for me.

How are you doing on the character count, btw?
Friendship is magic!

R.I.P. Tracy M. You will be missed.
User avatar
stranac
 
Posts: 1210
Joined: Thu Feb 07, 2013 3:42 pm

Re: [Beginner/intermediate] Do you know your string methods?

Postby Mekire » Fri Jan 31, 2014 1:26 am

Got mine down to 132 (133 if you count the newline at the end). My previous was 135 using a dictionary and a dict.get.

-Mek

Edit:
Down to 131. Back to using dictionary and dict.get()
User avatar
Mekire
 
Posts: 1015
Joined: Thu Feb 07, 2013 11:33 pm
Location: Amakusa, Japan

Re: [Beginner/intermediate] Do you know your string methods?

Postby stranac » Fri Jan 31, 2014 9:42 am

That's cool.
Your solution doesn't even look all that hacky.

Mine is an absolute piece of hackiness and now it even uses an if...
But it's 128 characters.
Friendship is magic!

R.I.P. Tracy M. You will be missed.
User avatar
stranac
 
Posts: 1210
Joined: Thu Feb 07, 2013 3:42 pm

Re: [Beginner/intermediate] Do you know your string methods?

Postby Mekire » Fri Jan 31, 2014 9:44 am

Well, ifs fine for the golf one by what I stated, but not sure how to get mine any lower. Tried several ideas but nothing yet.

-Mek
User avatar
Mekire
 
Posts: 1015
Joined: Thu Feb 07, 2013 11:33 pm
Location: Amakusa, Japan

Re: [Beginner/intermediate] Do you know your string methods?

Postby stranac » Fri Jan 31, 2014 10:11 am

Well, I know for a fact your approach can be shortened by at least 2 characters, so don't give up.
Friendship is magic!

R.I.P. Tracy M. You will be missed.
User avatar
stranac
 
Posts: 1210
Joined: Thu Feb 07, 2013 3:42 pm

Re: [Beginner/intermediate] Do you know your string methods?

Postby Mekire » Sat Feb 01, 2014 4:20 am

Ok. Since we already have a decent solution with Pedros' post I will go ahead and post mine.

Firstly, my fairly reasonable solutions.
First complies with normal setting:
Code: Select all
class Bob(object):
    def hey(self, prompt):
        test = not prompt.strip(), prompt.isupper(), prompt.endswith("?"), True
        reply = "Fine. Be that way!", "Woah, chill out!", "Sure.", "Whatever."
        return reply[test.index(True)]

This next one complies with hard (and is the basis for my golf attempt):
Code: Select all
class Bob(object):
    def hey(self, prompt):
        replies = {not prompt.strip() : "Fine. Be that way!",
                   prompt.endswith("?") : "Sure.",
                   prompt.isupper() : "Woah, chill out!"}
        return replies.get(True, "Whatever.")

And finally my golfs.
My original reduction of the second one above (131 characters):
Code: Select all
class Bob:hey=lambda s,p:{not p.strip():"Fine. Be that way!",p[-1:]=="?":"Sure.",p.isupper():"Woah, chill out!"}.get(1,"Whatever.")
And after using an improvement pointed out by Stranac (129 characters):
Code: Select all
class Bob:hey=lambda s,p:{1:"Whatever.",p[-1]=="?":"Sure.",p.isupper():"Woah, chill out!"}[1]if p.strip()else"Fine. Be that way!"

Stranac has currently bested me in the golfing of this code with this one (128 characters):
Code: Select all
class Bob:hey=lambda s,p:[['Whatever.','Sure.'][p[-1]=='?'],'Woah, chill out!'][p.isupper()]if p.strip()else'Fine. Be that way!'

I believe his solution may be minimal, but who knows.

-Mek
User avatar
Mekire
 
Posts: 1015
Joined: Thu Feb 07, 2013 11:33 pm
Location: Amakusa, Japan

Re: [Beginner/intermediate] Do you know your string methods?

Postby Mekire » Sat Feb 01, 2014 5:22 am

Mekire wrote:I believe his solution may be minimal, but who knows.

Seems I was wrong. This is my latest golf (coming in at 127):
Code: Select all
class Bob:hey=lambda s,p:p.strip()and{1:"Whatever.",p[-1]=="?":"Sure.",p.isupper():"Woah, chill out!"}[1]or"Fine. Be that way!"

-Mek

Edit:
Applying the same technique to Stranac's I reduce it to 126:
Code: Select all
class Bob:hey=lambda s,p:p.strip()and[["Whatever.","Sure."][p[-1]=="?"],"Woah, chill out!"][p.isupper()]or"Fine. Be that way!"
User avatar
Mekire
 
Posts: 1015
Joined: Thu Feb 07, 2013 11:33 pm
Location: Amakusa, Japan

Re: [Beginner/intermediate] Do you know your string methods?

Postby stranac » Sat Feb 01, 2014 1:48 pm

I thought I was out of ideas, but...
121:
Code: Select all
class Bob:hey=lambda s,p:{1:'Whatever.',p<'!':'Fine. Be that way!',p[-1:]=='?':'Sure.',p.isupper():'Woah, chill out!'}[1]
Friendship is magic!

R.I.P. Tracy M. You will be missed.
User avatar
stranac
 
Posts: 1210
Joined: Thu Feb 07, 2013 3:42 pm

Re: [Beginner/intermediate] Do you know your string methods?

Postby Mekire » Sat Feb 01, 2014 1:57 pm

Well crap. I abandon my dict approach just as you pick it up for the break away lead.

-Mek
User avatar
Mekire
 
Posts: 1015
Joined: Thu Feb 07, 2013 11:33 pm
Location: Amakusa, Japan


Return to Challenges

Who is online

Users browsing this forum: No registered users and 1 guest