Month: February 2015
shaving
Join Us
Generate a list of primes numbers in Python
This is frequently used as a programming challenge. There are a few ways to do this, but here is one especially interesting way that I’ve found. For this guide, we’ll assume we want a list of all primes under 50.
Tools:
- 1 ea python
1First, generate a list of non-primes
Generating a list of non-primes is much simpler than generating a list of primes. To begin, we need to think about what a prime number is. A prime number is one that is only divisible by 1 and itself. Therefore, if we want to generate a list of non-primes under 50 we can do so by generating multiples. noprimes = set(j for i in range(2, 8) for j in range(i*2, 50, i)) We are using a set in this case because we only want to include each multiple once. The function range(2, 8) will generate the numbers 2-7. In this example we are using set comprehension to iterate through the numbers 2 through 7. During each iteration we will use the number to iterate through the range 2i through 50 with an interval of i. Hopefully that isn’t too complicated! As an example, the first time we iterate through the outer loop, we generate the number 2. Then we iterate through the inner loop using 2 to produce this range: range(4, 50, 2). This produces: 4, 6, 8, 10, etc until we hit 50. Each of these numbers will be added to our noprimes set. The next time through we generate the number 3. Then in our inner loop we will produce range(6, 50, 3) which will add 6, 9, 12, 15, etc to our noprimes set. As you can see, this simply generates multiples. Once we calculate all the multiples through 7 we can be sure that we’ve generated all of the multiples. Anything 8 or higher will simply duplicate the work we’ve already done.
2Now generate primes
The second part is easy. Now that we have a list of non-primes, we can use list comprehension to loop through all numbers less than 50. Then we will check to see if each number exists in our noprimes set. If it doesn’t exist, we can be sure that it is a prime number. primes = [x for x in range(2, 50) if x not in noprimes] This will generate our list of prime numbers less than 50! [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47]
Now learn about:
Discuss this guide
Tools:
- 1 ea python
Join Us
How to sleep in space
Now I lay me down to sleep, I pray the Lord the Earth to keep. And if I find myself in space, I pray I’ll know how to sleep in zero gravity so that I can get a good night’s sleep without smashing into things. You don’t need to be an “end of times”, doom and gloom type to understand the value of knowing how to sleep in space. Should an Interstellar-esque rapid space incursion occur, you will be prepared. In this feature, Canadian Space Agency (CSA) Astronaut Chris Hadfield shares what it’s like to grab a great night’s sleep on the International Space Station. When the ISS was originally conceived, it was decided that there would be one habitable living one — a dorm, if you will — but for privacy reasons this was eventually evolved into six individual bedrooms, or sleeping pods. This is where our brave space travelers spend their nights, with dreams of being back on Earth.
Tools:
- 1 ea Sleeping?pod
- 1 ea Sleeping?bag
- 1 ea Wall?anchors
- 1 ea String
- 1 ea Russian?full-length?space?pajamas
1Watch the Video
Now learn about:
Discuss this guide
Tools:
- 1 ea Sleeping?pod
- 1 ea Sleeping?bag
- 1 ea Wall?anchors
- 1 ea String
- 1 ea Russian?full-length?space?pajamas
Join Us
Set up shortcuts to your SourceTree repos using Alfred (an installable workflow)
This short guide shows you how to set up access to your SourceTree repos through Alfred. While it’s easy to create custom shortcuts in Alfred, the non-tabbed nature of SourceTree makes it more difficult to set up shortcuts to your Git and Mercurial repositories. Luckily, there’s an installable workflow that works great.
Tools:
- 1 ea Alfred for OSX
- 1 ea SourceTree for OSX
1About the tools: Alfred and SourceTree together
Alfred is an amazing workflow management app for OS X. SourceTree is an amazing graphical client that allows you to interact with your Git and Hg repositories without needing to use the command line. By combining the two, you can save tons of time in your normal development workflow.
2Download the workflow
zhaocai has made an awesome installable workflow which allows you to list, search, and open your repos. Download the workflow from the alfred2-sourcetree-workflow Github page.
3Install the workflow
Locate Source Tree.alfredworkflow in Finder and open it to import it into Alfred. Select a category and click “Import”.
4Using the workflow
Opening a specific repo – the shortcut to use is “st repo_name”:st howchooListing all repos – the shortcut to use is “st” (no argument provided):st
Now learn about:
Discuss this guide
Tools:
- 1 ea Alfred for OSX
- 1 ea SourceTree for OSX
