Monday, January 4, 2016

Code Scribbling with Fizz Buzz

There's this thing that I like to do called code scribbling.  Code scribbling is about letting loose.  You noodle around with a piece of code for no particular reason.  Fun is the only goal here.  But it's an important goal.  Fun helps loosen you up for the important programming work you do.  The code scribbling session below is one such example of me just enjoying myself with a little Python 3.4.

A Code Scribbling Session


My mad plan here is to play a little Katamari Damacy with the Fizz Buzz problem.  I want to start simple and just add thing after thing until it turns into a big ball of chaos.  We'll start with a basic loop and modulus operators.  Here is that.




Let's shorten this up with some ternary operators.




Okay, that was concise.  Time to make it more interesting.  What if the two ternary operators were concatenated together and thrown into a list comprehension?  The fizz and buzz ternary expressions have to be wrapped in their own sets of respective parenthesis.  This ensures that it evaluates correctly.  The "==" aspect of the modulus expression can be reduced to something more "truthy" so that it fits nicer in a blog post.  Here's what that looks like.




Interesting if a bit of a space eater.  A generator expression would make it lazier.  At the very least, it won't make the code look any worse than before.  Here's that code.




What else could be done with this?  Here's an idea.  Those ternary expressions could be wrapped into a lambda function.  Here's the result of that.




What else?  We could take the string formatting out of the print statement and put that in the expression.  Then, the for loop just has to loop through the resulting list of strings. 




Hmm.  Come to think, why have the for loop at all?  The string join method could just join them all together with newline characters to ensure it prints like before.  Then, the print statement just has to print one big string all at once.




Hooray, no more normal for loop!  But this is interesting.  That entire expression we have could be made into it's own lambda expression in and of itself.  You could even pass an argument and print the result of that.  Here's what that looks like.




Okay, what's next?  There's no reason why we can't just move the entire expression and evaluation into the print statement itself.




It's all one big blob now.  The print statement uses the expression here.  But wait!  There's no reason the print couldn't be a PART of the expression.




Where do we take it from here?  Let's see.  Meta-programming perhaps?  There's nothing saying we can't create a nameless class with a __call__ method.  Then it's just a matter of making an instance.  From there, it's just a matter of taking advantage of __call__ and invoking the instance as if it was just another function. 




This cake is baked.  Let's decorate!  I'll just wrap the __call__ function in a staticmethod decorator and make that troublesome self argument go away.




Awesome!  All wrapped and decorated.  And we're done with this scribbling session.

What a Mess!

This is obviously not meant for production code.  This is essentially unfettered silliness enjoying some time in the sun.  Such is what creativity is.  It might accidentally lead to actual innovation but that's besides the point.  This is about taking some time to set your brain free.

So make it a priority this year to relax.  Have fun.  Scribble.

19 comments:

  1. Mic Drop: https://gist.github.com/ryanseddon/2631637

    ReplyDelete
  2. How about this? https://github.com/tacticiankerala/elixir-haskell-ruby/blob/master/fizz-buzz/fizz_buzz.rb

    PS: This is written in ruby but, hardly takes any time to convert to python

    ReplyDelete
  3. The output is not as per the requirements; it is supposed to be "FIZZ" or "BUZZ" or "FIZZBUZZ" or the number itself

    ReplyDelete
  4. Power BI Course Online
    Enrolling in a Power BI course online can really boost your career prospects. This course is worth exploring: power bi certification training. It covers dashboards, reports, and data modeling. The hands-on approach makes learning effective. Great for beginners and professionals alike.

    ReplyDelete
  5. Salesforce Admin Course
    Salesforce admin course is a good career option. I found this salesforce admin course useful. It explains concepts clearly. Easy to understand. Helpful for beginners.

    ReplyDelete
  6. Great overview! devops and aws training
    helps in understanding deployment pipelines and automation strategies. It enhances productivity and speeds up delivery cycles.

    ReplyDelete
  7. I found this servicenow online training content very useful and easy to understand.servicenow training

    ReplyDelete
  8. MuleSoft developer training focuses on building APIs and integrating systems. It is ideal for developers looking to expand their skill set.mulesoft developer training

    ReplyDelete
  9. MuleSoft developer training is essential for API integration roles. Nice content!mulesoft developer training

    ReplyDelete
  10. Insightful article! Our devops with aws training
    focuses on real-world skills like CI/CD, monitoring, and infrastructure as code, helping learners streamline software delivery efficiently.

    ReplyDelete
  11. The ServiceNow administrator course covers everything from basics to advanced configurations. Definitely a good starting point for IT professionals.servicenow administrator course

    ReplyDelete
  12. Great read! Learn dell boomi training
    with practical experience in real-world integrations, helping you build strong iPaaS skills for career growth.

    ReplyDelete
  13. Salesforce CPQ training helps you learn pricing, quoting, and sales automation to improve business efficiency.servicenow course

    ReplyDelete
  14. The ServiceNow training classes offer practical knowledge with expert guidance.service now training classes

    ReplyDelete
  15. Very informative discussion. Many IT professionals choose mongodb training programs to enhance their technical expertise.

    ReplyDelete
  16. Interesting insights shared here. Flexible programs like a mongodb online course allow learners to study at their own pace.

    ReplyDelete
  17. ServiceNow training classes offer hands-on experience with real-world scenarios.servicenow training classes

    ReplyDelete
  18. servicenow training classes – ServiceNow training classes are well-organized and help in understanding core ITSM concepts step by step.servicenow training classes

    ReplyDelete