Monthly Archives

July 2013

Cool Stuff, Infographics

The Digital Public Library of America Launches

July 22, 2013

Spring marked the opening of the Digital Public Library of America (DPLA), a collection of over 2.4 million (and growing) works of art and history, the largest in the world. We can read Mozart’s letters or take a gander at a rare Edgar Degas sketch in the DPLA's digital halls. The DPLA is emerging as an invaluable resource for those interested in accessing and preserving our history, culture and humanity.

An Infinite Global Reference Source

The DLPA is the brainchild of Robert Darnton, a librarian at Harvard University who dreamed of combining the holdings of America’s great libraries, museums, historical societies and archives. Two years later, with the cooperation of the Smithsonian institution, New York Public Library, University of Illinois, Digital Commonwealth in Massachusetts, Minnesota Digital Library and many others, the DLPA is off the ground and ready to be explored. The DPLA’s original aim was to be a “living heritage” to educate, inform and empower both current and future generations, yet it has already begun to evolve into much more. The DPLA offers an open framework where knowledge, art and culture can be parlayed into power and positive change.

Link to the Unknown: Art and Authors in a New Light

The DPLA will help rare, meaningful media surface, like this rare 1919 home movie of an African-American baseball game in a backyard, points out DPLA executive director Dan Cohen. Photos, paintings, archives and slide-scanning bring the world's highlights onto our screens. Imagine if Picasso had access to millions of paintings that came before him, or if Shakespeare could have browsed the entire works of Virgil or Homer from his desk. How could that have affected the history of art and storytelling?

The Virtual Future: An Alexandrian Rival?

As the most comprehensive house of scholarly material and art, the DPLA is sort of like a modern Library of Alexandria, except more difficult to burn down. The digital structure serves a two-fold purpose: It is both a destination and source as well as an aggregator. The DPLA is a library and a hub that helps build exposure for other hubs, like ARTsor, an image contributor for many well-known museums. This structure also allows the DPLA to avoid copyright issues, as each content provider must provide the rights field in the metadata.

Anyone can access the DPLA through a laptop, tablet or cell phone—users simply need an Internet connection. The DPLA’s vision for the future is to contain the full breadth of human expression without commercial interests, which sets it apart from almost all other major media endeavors. The pursuit of knowledge makes growth possible, and when ulterior motives are stripped away, we all reap the benefits. Aspiring artists, digital virtuosos and academicians can find their Shangri-la in the DPLA.

Random

Software Development Stages, According to Richard

July 19, 2013

how_software_is_dev

Advertisement

Links from around the web

July 18, 2013

Create Your Own Programming Language
A System To Achieve Every Programmer's Dream. Learn How To Create A Simple Programming Language In A Few Days With This Easy Step-by-step Guide.

How-to: Convert Psd To Html/css, A Step By Step Guide
A Step-by-step Guide On How To Convert .psd Designs Into Functional Html/css Files.

The Art Of Privacy: Top Secret Level PC Security For Non-geeks
Discover How To Use Top Secret Level Privacy Techniques For Your PC Without Becoming A Geek! The Art Of Privacy Is A Guide To Help Mobile Laptop Users Guard Their Privacy By Showing Exactly How To Set Up Their PC For Maximum Security With Minimal Effort

The Complete Job Interview Answers Guide
Hot New Product To Market - Learn all the secrets on how to master the Job Interview. Get you dream Job Today!

Simple php in less than a day!
Learn php in only 17 Hours. Master another language and grantee that edge for the next interview.

Learn Cocos2d
Learn how to build your own iOS game with Cocos2d Source Code, Developer Documentation And Services.

Linux For Desktop PC And Notebook Computers
Download Linux For Free! Linux Is A Bulletproof Operating System For Your Computer. Linux Is Fast, Stable, And Immune To Virus And Spyware. Over 30 Games. Firefox Browser. Flash Video. Image Editor. MS Office Compatible. So Simple, Grandma Can Use It!

Video Game Tester Jobs
Get Paid To Play Video Games! That’s right! Get Paid to try out new games and be part of the game industry. Limited spots open, join today!

Tycoon World Of Warcraft Gold add-on
The Tycoon Wow Gold add-on Puts Gold Making On Autopilot. Get all the gold you will ever need for WoW.

Xbox 360 Ring Of Death Fix
James Dean Xbox 360 3 Red Light Fix. Get your Xbox 360 back working and get back to you game.

Problems

Kth Number Generator in Python

July 16, 2013

A while back we looked at how to generate prime numbers in python. I was looking around for some similar problems that uses prime numbers and I came across an interview question. The question goes something lie this:

        Assume you have a function

f(x,y,z) = a^x * b^y * c^z

Where,

a,b,c - constants
x,y,z - larger than zero integers

The problem: find the kth number this function generates.

I do realize that this sounds very abstract. Let me simplify it. Assume a,b,c are 2,3,5 respectfully. If we start plugging in values to x,y,z starting from (0,0,0) we will get the following result:

 2^0 * 3^0 * 5^0 = 1
 2^1 * 3^0 * 5^0 = 2
 2^0 * 3^1 * 5^0 = 3
 2^1 * 3^1 * 5^0 = 6
 2^0 * 3^0 * 5^1 = 5
 2^1 * 3^0 * 5^1 = 10
 2^0 * 3^1 * 5^1 = 15
 2^1 * 3^1 * 5^1 = 30
 2^2 * 3^0 * 5^0 = 4
 ...

You will notice that the numbers generated are not in order. That means that brute force is almost for sure out of the question. Why? Because there is always a slight chance that we did not generate a number. For example, if we considered only the values 0,1 for x,y,z, we will never get the value 4. So what can we do? We need to think. Keep in mind that this is an interview question for programmers and mathematicians. You may see it in the future. When you do see it, you have about 30 minutes to figure it out. When you are in that scenario, do not worry about getting to the perfect solution (unless it is required). The interview is looking for a process, a thinking mind. So think out loud.

Let us look at a brute force approach to this problem:

import sys

def main(argv):

if len(argv) != 4:
sys.exit('Usage: kth_brute.py <a> <b> <c> <k>')

a = int(sys.argv[1])
b = int(sys.argv[2])
c = int(sys.argv[3])
k = int(sys.argv[4])
list = []

for x in range(k):
for y in range(k):
for z in range(k):
list.append((a**x * b**y * c**z))

list.sort()
print '\nThe kth number is: ' + str(list[m-1]) + '\n'

if __name__ == "__main__":
main(sys.argv[1:])

And the output:

wpid-python_kth_brute-2013-07-16-09-02.png

Now that we saw a solution, let us try to do better. After all, this solution will take a while to run for a large k. In addition, it has the chance to miss a number because we are not generating the numbers in order. If you sit for long enough and keep generating numbers on paper you may start to notice something. The numbers the function generate do have some basic factors that are easy to miss. They are products of constant (prime numbers in his case). That means that we can write the numbers we get as factors:

 1
 2
 3
 2*3
 5
 2*5
 3*5
 2*3*5
 2*2
 ...

How about that. What if instead of trying to generate the next number based on the exponent values, we looked at what would be the minimum value? That will save us from sorting in the end and we won’t have to generate a whole bunch of extra numbers. To further use this idea, let us create 3 queues for each of the constant. Every iteration we will add or remove from the queue values based on the current value. here is a general algorithm:

        let q2,q3,q5 be queues
add 1 to q2
set val to 0
for i in k (k being the kth number)
for each q
if q_m is not empty, pop q_m to v_m
else, set v_m to MAX_INT
set val to the min value of v2,v3,v5

if val is from q2
append (2*val) to q2
append (3*val) to q3
elif val is from q3
append (3*val) to q3

append (5*val) to q5 (always)

return val

Seems simple right? Here is the code in Python:

import sys
from collections import deque

def main(argv):

if len(argv) != 5:
sys.exit('Usage: kth.py <a> <b> <c> <k> <echo>')

# get bases
a = int(sys.argv[1])
b = int(sys.argv[2])
c = int(sys.argv[3])
k = int(sys.argv[4])
echo = int(sys.argv[5])

# setup queue
q1 = deque([])
q2 = deque([])
q3 = deque([])

# init variables
q1.append(1)
val = 0

for i in range(k):

# set v to the next value in queue or to MAX_INT if queue empty
if len(q1) > 0: v1 = q1[0]
else: v1 = 2**32

if len(q2) > 0: v2 = q2[0]
else: v2 = 2**32

if len(q3) > 0: v3 = q3[0]
else: v3 = 2**32

# choose the next minimum value from the 3 queues
val = min(v1,v2,v3)

# add next values to queue
if val == v1:
q1.popleft()
q1.append(a*val)
q2.append(b*val)
elif val == v2:
q2.popleft()
q2.append(b*val)
elif val == v3:
q3.popleft()

# always add the largest
q3.append(c*val)

# if echo is True print every number
if echo: print str(i+1) + ": " + str(val)

print '\nThe kth number is: ' + str(val) + '\n'

if __name__ == "__main__":
main(sys.argv[1:])

And here are some example runs:

wpid-pyhton_kth_number_short-2013-07-16-09-02.png

wpid-pyhton_kth_number_long-2013-07-16-09-02.png

Cool Stuff

London Film & Comic Con - July 2013

July 14, 2013

Computer Programming I

A Quick Tutorial for Python Syntax

July 8, 2013

For some, Python is far from their first language. For others, it is where they had started programming. I have received several comments and emails from readers asking how to proceed after understanding the basic concepts in Python. I am hoping to put together a road-map into Python in the next few weeks. Whether you know Python already or want to learn the basic quickly, I hope you will find this post (and the rest) useful. This is a very quick paste, high overview of the language. You are expected to know the basics of programming. Think of this as fast Python syntax review. For this tutorial I will use Python2.7. I highly recommend you consult the Python2.7 documentation while coding or download Python2.7 documentation for offline use. Linux and Mac come with Python already pre-installed. If you require help setting up a Python environment, please consult the Python installation guide. I am going to assume from this point on you already have Python 2.7 at least installed on your system. So let us get to it:

Running Python

There are 3 ways to execute Python code:

  1. The Python Interpreter - an interactive terminal. Statements are evaluated as entered.
  2. Loading a file into the Python Interpreter - save any text file as a .py and execute the statements.
  3. Compiled Python - this is not for speed of execution, rather for sharing a program without sharing code.

For any of the ways, we are going to focus on command line only. There are IDEs for Python, however they are an overkill for this tutorial. To run Python, simply lunch your terminal and type ‘python’. If you want to run a .py file in the terminal type ‘python <file_name.py>’, where file_name should be replaced with your file name.

Comments

In Python, any line starting with the ‘#’ symbol is considered a comment and will be ignored by the Interpreter.

Python Basic Data Types

Similar to other languages, Python supports the idea of basic data types. Unlike most low-level languages, Python does not require the programmer to specify the type. Python interrupter takes case of the data type for you. You may also change to type throughout the program. Thankfully, Python has a built in function type() that can clarify what type of data Python ‘thinks’ the variable is. The basic data types are:

* integer
* long
* float
* complex
* string
* boolean

Here is an output from the Python interpreter:

wpid-pythonbasicdatatypes-2013-01-20-10-09.png

Python Data Structures

Python has 4 data structures that could be at your disposal. These are complex data types that are built from the basic data types we saw before. Each of the data structure types has different usage and attributes. We will discuss:

* Lists
* Tuples
* Dictionaries
* Sets

Starting from the top, Lists are the most common data structure used in Python. With Lists you can group basic datatypes or even data structures together. You can preform numbers standard library function on lists. The 2 (2things) most important things to remember about lists are:

* They are 0 and reversed numeric indexed
* They are mutable

Let us look at some list example operations:

wpid-pythonlists2-2013-01-20-10-09.png

Moving on to tuples, they are very similar to lists but with one thing apart. Tuples are immutable. You may think of tuples as immutable lists, in other words lists that can not be changed once you create them. All the operations we have done on lists apply to Tuples, however, we cannot remove or add elements. This means that when we initiate a Tuple we initiate it with values that may never change. Let us look at some simple Tuple operations:

wpid-pythontupleexamples2-2013-01-20-10-09.png

So now that we have some data types we can move on to the next vital piece of information you need to know, Python Standard Library. Python include a huge array of functions you can use without anything special. The type() and len() functions are some examples of the functions that are included in standard library. This also includes function such as input() and raw_input(), string and number manipulation and many more functions. The best advice if you are looking for a function, check if it is included in stadard library before going to far.

So now what do we do if a function is not in standard library. For example, lets say we need to comute a sin() of an angle, or require the value of pi for calculation, what do we do then? Well, in that case we can import a library. That is right, in addtion to the huge standard library there is also a "secondary library" with everything you might think about, at least in the beginning. Again, you can refer to the Python2.7 documentation for any reference to these libraries. In our case we can import the math library and directly call the math.sin() function or access the value of math.pi. Let us take a look:

 

python_math_import_example3

 

Python is truly a fun language to begin to code with. I did not begin with Python myself, but I highly recommend it to others. Why? well, first I do not think anyone should start in PASCAL any more. Although it was a good educational languages, it is pointless to learn today. Second, Python is natural. It is easy to ease in to programming with python far more than any other language. (This last point might be debatable on the person and the teacher).

Holiday

Happy 4th of July

July 4, 2013
Wishing everyone a safe and Happy 4th

Wishing everyone a safe and Happy 4th

Cool Stuff

Simple GUI Text Editor in Python

July 3, 2013

I wrote this simple text editor a while back and came across it while cleaning out my hard drive. I know this is a little more advance than what I normally post, but I wanted to show off what Python can do. Do not worry if you do not understand the code at this point. Just enjoy the fact that in less than 100 lines of code, you can have a working replica of notepad or any other GUI text editor.

 

What can you do in under 100 lines of code?

 


from Tkinter import *
from tkSimpleDialog import askstring
from tkFileDialog   import asksaveasfilename

from tkMessageBox import askokcancel

class Quitter(Frame):
    def __init__(self, parent=None):
        Frame.__init__(self, parent)
        self.pack()
        widget = Button(self, text='Quit', command=self.quit)
        widget.pack(expand=YES, fill=BOTH, side=LEFT)
    def quit(self):
        ans = askokcancel('Verify exit', "Really quit?")
        if ans: Frame.quit(self)

class ScrolledText(Frame):
    def __init__(self, parent=None, text='', file=None):
        Frame.__init__(self, parent)
        self.pack(expand=YES, fill=BOTH)
        self.makewidgets()
        self.settext(text, file)
    def makewidgets(self):
        sbar = Scrollbar(self)
        text = Text(self, relief=SUNKEN)
        sbar.config(command=text.yview)
        text.config(yscrollcommand=sbar.set)
        sbar.pack(side=RIGHT, fill=Y)
        text.pack(side=LEFT, expand=YES, fill=BOTH)
        self.text = text
    def settext(self, text='', file=None):
        if file:
            text = open(file, 'r').read()
        self.text.delete('1.0', END)
        self.text.insert('1.0', text)
        self.text.mark_set(INSERT, '1.0')
        self.text.focus()
    def gettext(self):
        return self.text.get('1.0', END+'-1c')

class SimpleEditor(ScrolledText):
    def __init__(self, parent=None, file=None):
        frm = Frame(parent)
        frm.pack(fill=X)
        Button(frm, text='Save',  command=self.onSave).pack(side=LEFT)
        Button(frm, text='Cut',   command=self.onCut).pack(side=LEFT)
        Button(frm, text='Paste', command=self.onPaste).pack(side=LEFT)
        Button(frm, text='Find',  command=self.onFind).pack(side=LEFT)
        Quitter(frm).pack(side=LEFT)
        ScrolledText.__init__(self, parent, file=file)
        self.text.config(font=('courier', 9, 'normal'))
    def onSave(self):
        filename = asksaveasfilename()
        if filename:
            alltext = self.gettext()
            open(filename, 'w').write(alltext)
    def onCut(self):
        text = self.text.get(SEL_FIRST, SEL_LAST)
        self.text.delete(SEL_FIRST, SEL_LAST)
        self.clipboard_clear()
        self.clipboard_append(text)
    def onPaste(self):
        try:
            text = self.selection_get(selection='CLIPBOARD')
            self.text.insert(INSERT, text)
        except TclError:
            pass
    def onFind(self):
        target = askstring('SimpleEditor', 'Search String?')
        if target:
            where = self.text.search(target, INSERT, END)
            if where:
                print where
                pastit = where + ('+%dc' % len(target))
               #self.text.tag_remove(SEL, '1.0', END)
                self.text.tag_add(SEL, where, pastit)
                self.text.mark_set(INSERT, pastit)
                self.text.see(INSERT)
                self.text.focus()

if __name__ == '__main__':
    try:
        SimpleEditor(file=sys.argv[1]).mainloop()
    except IndexError:
        SimpleEditor().mainloop()