Skip to content

Instantly share code, notes, and snippets.

View BeOleg's full-sized avatar

Oleg Belousov BeOleg

View GitHub Profile
@bearfrieze
bearfrieze / comprehensions.md
Last active December 23, 2023 22:49
Comprehensions in Python the Jedi way

Comprehensions in Python the Jedi way

by Bjørn Friese

Beautiful is better than ugly. Explicit is better than implicit.

-- The Zen of Python

I frequently deal with collections of things in the programs I write. Collections of droids, jedis, planets, lightsabers, starfighters, etc. When programming in Python, these collections of things are usually represented as lists, sets and dictionaries. Oftentimes, what I want to do with collections is to transform them in various ways. Comprehensions is a powerful syntax for doing just that. I use them extensively, and it's one of the things that keep me coming back to Python. Let me show you a few examples of the incredible usefulness of comprehensions.

# 1. Create a Python function that gets an integer between 1 - 12 (as an hour) and returns the exact time in hours
# and minutes at which the hands of the clock overlap at this hour
# (for example, for 1, the result will be close to 1:06)
# 2. create a unit test for this function, using 'assert', or the Python 'unittest' framework
# 3. [BONUS]
# a. add also seconds calculation to the return value of the function.
# b. calculate the angle of the hands in 360 degrees system
# taking 12:00 (noon, midnight) as a point of reference
# ATTENTION: You do not need to import any modules to implement this task
@mdogo
mdogo / fibonacci_itertools.md
Last active October 22, 2022 12:10
Itertools examples with Fibonacci Sequence

By definition, the first two numbers in the Fibonacci sequence are 0 and 1, and each subsequent number is the sum of the previous two. The recursive definition of the fibonacci number in Python is:

def fiboncci(n):
    if n == 0:
      return 0
    elif n == 1:
      return 1
 else
@mhdhejazi
mhdhejazi / CopyAsanaTasks.php
Created October 24, 2012 16:43
Copy/Move project to a new workspace in Asana
function asanaRequest($methodPath, $httpMethod = 'GET', $body = null)
{
$apiKey = 'ASANA_API_KEY_HERE'; /// Get it from http://app.asana.com/-/account_api
$url = "https://app.asana.com/api/1.0/$methodPath";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC ) ;
curl_setopt($ch, CURLOPT_USERPWD, $apiKey);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);