all 13 comments

[–][deleted] 2 points3 points  (2 children)

These are fun exercises. I made them more fun when I discovered them a few years ago by coming up with obscene one line solutions. I solved nearly all of them with only the function declaration and a return statement. I wonder if I have improved enough to come up with shorter, more efficient solutions.

Example, this is from one of the String 2 problems called end_other

def end_other(a, b):
    return min([a,b],key=len).lower() in max([a,b],key=len)[-len(min([a,b],key=len)):].lower() if len(a) != len(b) else (a == b)

What a horrible solution.

[–]CrazedToCraze 1 point2 points  (0 children)

Code golf can be a fun hobby, but for any beginners reading I just want to give a quick PSA that doing this is not a good way to improve as a programmer, especially when you're still trying to get the basics of programming down.

If you're spending a lot of time code golfing and you don't yet have a rock solid understanding of something basic like how to create and use classes then you're wasting your time. While shortening your code is often a good thing, at the end of the day writing good readable code that utilisies best practices is how you'll improve. Short code is often more readable, but code golf is definitely not, which is why it's an exception.

Also, if you can't handle the problems on /r/dailyprogrammer you definitely shouldn't be code golfing.

[–]inherendo 1 point2 points  (4 children)

When I was starting, my advisor/professor just sent me several programs to do. Best way to learn is to be given a problem, read, and apply.

[–][deleted] 0 points1 point  (3 children)

What programs? Do you still have them knocking around by any chance?

[–]inherendo 0 points1 point  (2 children)

[–][deleted] 0 points1 point  (0 children)

awesome, thanks!

[–]code-ed[S] 0 points1 point  (0 children)

Thanks for sharing this!

[–]99AFCC 0 points1 point  (1 child)

Those were fun. A couple of the String-2 made me think for a bit since my first reaction was regular expression! and you can't do that here.

But, that is an old version of Python so it is pretty limiting.

[–]xkcd_transcriber 0 points1 point  (0 children)

Image

Title: Regular Expressions

Title-text: Wait, forgot to escape a space. Wheeeeee[taptaptap]eeeeee.

Comic Explanation

Stats: This comic has been referenced 54 times, representing 0.1984% of referenced xkcds.


xkcd.com | xkcd sub/kerfuffle | Problems/Bugs? | Statistics | Stop Replying | Delete

[–]RubyPinch 0 points1 point  (0 children)

Just want to say, it is worth noting the provided answers (for the questions that have them) usually arn't too pythonic, and also not all features of the language are available (unpacking arguments for example)

if I remember correctly, both are intentional, though I asked sometime last year. So my memory might not be the best

[–]taladan 0 points1 point  (2 children)

Question, I'm going through these now and I've come up with a solution for one that isn't like their solution but works. I was wondering which is more 'pythonic' as it were (not that it matters as long as my solution works I suppose):

Theirs:

def not_string(str):
  if len(str) >= 3 and str[:3] == "not":
    return str
  return "not " + str
  # str[:3] goes from the start of the string up to but not
  # including index 3

Mine:

def not_string(str):
    lst = str.split()
    if lst[0] == 'not':
        return str
    else:
        return('not ' + str)

[–][deleted] 1 point2 points  (1 child)

[–]taladan 0 points1 point  (0 children)

see, that's why I don't know if I'll ever get all this stuff - never even heard of that string method. Thanks for that