Hone Your Powers Part 1 (Programming)


Prepare the code you will need to process the API response

The Twitter API search query returns a list of results. For this challenge, you will be putting together some basic code that will help when processing the results of your API call.

You may use Ruby, Python, or any other scripting language of your choice. This challenge is an opportunity for you to demonstrate basic proficiency in that scripting language. If you need to brush up, or if you are using this challenge as a way to guide your learning, that's great too! There's no rush here. Here are some introductory resources for Python and Ruby.

If you have suggestions for other resources, for these languages or others, please submit them as comments below. P2PU and many other sites also offer free courses and tutorials to introduce these languages. 

Make sure to read about for loops, dictionaries (Python) or hashes (Ruby), and if statements. Both Python and Ruby have command line interpreters. Type "python" at the command prompt to enter the Python interpreter, and type "irb" at the command prompt to enter the Ruby interpreter. 

Make a simple dictionary/hash, add some items to it, and experiment with retrieving items by key and by value

Make a list and experiment with adding and retrieving items from it. 

What is the difference (syntactically) between retrieving an item from a dictionary/hash, and a list? 

Write an if statement to check for a value in a list. Then use an if statement to check for a specific key, and return the associated value if that key exists. 

Finally, write a for loop, and iterate over your dictionary/hash. Within the body of the for loop print out each key-value pair.

 How would you insert an if-statement into the for loop? 

Task Discussion


  • Eenvincible said:

    Hi guys,

    I just completed this level of the challenge and I am very excited to jump into the next leve and explore farther. Instead of posting my results here, I decided to post them on my awesome blog that I purposefully started for this challenge.

    Please check it out here and remember to leave a link to yours so I can check it out!

    http://simpledeveloper.com

    If anyone has questions, they can ask and we can discuss and learn together! Thanks and see you around!

    on Jan. 2, 2013, 4:51 p.m.
  • govindreddy said:

    1.What is the difference (syntactically) between retrieving an item from a dictionary/hash, and a list?
    To retrieve item in list you can retrieve it by offset as list is elements of ordered sequences.
    ex-  li=['a','b','c','d']
    to retrieve c  --- li[2]
    Dictionaries are unordered sequences and container of key, value pairs. To retrive a value  from dictionary list technique do not work.
    following example
    dict={ 'a':1,'b':2,'c':3,'d':4}
    to retrieve value of c---dict[c]
    2. Write an if statement to check for a value in a list. Then use an if statement to check for a specific key, and return the associated value if that key exists
    f 'c' in li:
        if 'c'==li[2]:
            print li.index('c')

    3.Finally, write a for loop, and iterate over your dictionary/hash. Within the body of the for loop print out each key-value pair.
    dict={'a':1,'b':2,'c':3,'d':4}
    for key,value in dict.items():
        print key,value

     4.How would you insert an if-statement into the for loop?
    i would insert if-statement into the for loop as a nested loop.
     

    on Dec. 5, 2012, 1:23 a.m.

    Eenvincible said:

     

    Hi ,

    I just completed this level of the challenge and I am very excited to jump into the next leve and explore farther. Instead of posting my results here, I decided to post them on my awesome blog that I purposefully started for this challenge.

    Please check it out here and remember to leave a link to yours so I can check it out!

    http://simpledeveloper.wordpress.com/2013/01/02/hone-your-powers-programming-101/

    If anyone has questions, they can ask and we can discuss and learn together! Thanks and see you around!

    on Jan. 2, 2013, 4:52 p.m. in reply to govindreddy
  • trbuh said:

    not 100% sure if i did it right, but if it helps, here go my answers ;)

     

    What is the difference (syntactically) between retrieving an item from a dictionary/hash, and a list?

    Retreiving from a list one needs to specify the (exact) location of the element to be retreived, as lists are sorted by indices. E.g. list[9] would produce the 10th element of the list -if the list contains 10 or more entries.
    Dictionaries on the other hand don't have an evident sorting. They store pares of entries and thus one has to "guess" the right key to retreive an element from a dictionary (e.g. dictionary[9] would only then produce a valid result if a key `9´ exists - independent of its size.).
    Further one can use indices/keys of type other than integers (e.g. stings) to address elements witin a dictionary.



    Write an if statement to check for a value in a list. Then use an if statement to check for a specific key, and return the associated value if that key exists.

    # if value 9 is in list "testList" its index is printed
    if 9 in testList :
        print testList.index(9), '\n'
    else :
        print 'do the cookie-dance \n'

     

    # if integer '99' is a key in the dictionary, then it's value is printed
    if 99 in myDict :
        print myDict[99]
    else :
        print "no '99' in dictionary"



    Finally, write a for loop, and iterate over your dictionary/hash. Within the body of the for loop print out each key-value pair.

    for int in myDict.keys() :
        print int,'\t', myDict[int]



    How would you insert an if-statement into the for loop?

    for int in myDict.keys() :
        print int,'\t', myDict[int]
        toTest = myDict[int]
        if "pi" in toTest :
            print "that's a bingo"

    on Sept. 26, 2012, 10:39 a.m.

    Eenvincible said:

     

    Hi mate,

    I just completed this level of the challenge and I am very excited to jump into the next leve and explore farther. Instead of posting my results here, I decided to post them on my awesome blog that I purposefully started for this challenge.

    Please check it out here and remember to leave a link to yours so I can check it out!

    http://simpledeveloper.wordpress.com/2013/01/02/hone-your-powers-programming-101/

    If anyone has questions, they can ask and we can discuss and learn together! Thanks and see you around!

    on Jan. 2, 2013, 4:52 p.m. in reply to trbuh
  • Alvaro said:

    Hey there! I'm trying to learn Python so I will use it for all the examples. 

    I found very similar to work with dictionaries and lists, but it depends of the complexity of your model.

    For this snippet of code I used the python tutorial from his webpage: http://docs.python.org/tutorial/index.html

    You can find the code I wrote here: http://pythonfiddle.com/hone-your-powers-part-one

    I found an interesting link: http://people.csail.mit.edu/pgbovine/python/tutor.html#mode=edit

    There you can visualize the execution step by step and see how the memory works. Furthermore, it comes with lots of examples for all levels. 

    See you!!

    on Aug. 22, 2012, 4:47 p.m.
  • Will C. said:

    I'm a little confused here.
    do I have to know Ruby or Python to complete this course?

    I have been learning PHP, Will PHP work for this?

    on June 18, 2012, 2:04 p.m.

    Jessy Kate Schingler said:

    at p2pu, a "challenge" is a mechanism to guide you towards things to learn, but doesn't attempt to teach it to you directly. there are other courses and study groups on p2pu, that focus on peer learning and studying. but the chalenge is more of an opportunity to demonstrate knowledge you have, or as you get it (whether through a course on p2pu or just online on your own). 

    that said, you are welcome to have a go in PHP. the challenge only includes explicit documentation for ruby and python but others have done it in javascript (node.js) and other languages.  so please feel free!

    hope that all makes sense...!

    on June 18, 2012, 2:16 p.m. in reply to Will C.
  • manicphase said:

    I find the official python documentation (docs.python.org) to be pretty good at explaining things, although they do sometimes seem intimidating.

    Just copy the examples, change them, break them, then fix them again. You'll get a better understanding of how they work by seeing what happens when they don't.

    on June 1, 2012, 6:20 p.m.
  • Jessica Ledbetter said:

    Oops. I shoulda read ahead on the tasks cause I did a few of these on the last. I picked Javascript to show the tweets. 

    on April 19, 2012, 3:39 p.m.
  • phaser said:

    Hi all,

    I have just jumped into this challenge and like it very much.

    Having just started learning PHP, I don't want to confuse myself too much with learning Python or Ruby on top just yet. So, I thought I post some links for others who might like to check out PHP solutions for this.

    I reckon you need to know how to load a url: file_get_contents, how to decode JSON strings: json_decode, how to call objects in PHP and how to iterate through an array of objects: foreach.

    That should get you going.

    BTW There is a PHP library available for ease of use with Twitter API. This simple tutorial shows you how to use it.

    Have fun.

    Steffen

    on April 18, 2012, 4:11 a.m.
  • Anonym said:

    i think i go with nodejs like jos. it seem interesting

    on April 3, 2012, 10:05 a.m.

    Jos said:

    Go  for it Lorenzo!

    If you need any help just shout! :D

    on April 11, 2012, 4:45 p.m. in reply to Anonym
  • Newman5 said:

    Hey Gang,

    I'm going to get in my comment before shutting down for the night, but I've not finished everything yet.

    I used Python and the dive into python book (Mark Pilgram!).  Also, this is EXACTLY what we are going over in the Udacity course, Unit 5.29... but there server went Kaput so I'm over here. 

    Just a note: I'm using the python IDLE visual interpreter.  Not really sure about those commands "python" and "ruby".

    On to the tasks:

    Simple enough to create a dictionary - it's chapter 3 in the Dive into Python book - http://screencast.com/t/UMVffo5TyF

    About retrieving items by Key and by value:

    I can do one but not the other.  What gives, teach?  Is this a trick question? How can you look up a value in a dictionary?

    Simple enough to add items to the dictionary - It's like adding something to a list... ah ha!  No it's not like adding something to a list.  To add something to a list, you use the .append or other methods, I'm just now becoming aware of... hmmmm, The Jedi never told me of THIS!

    Lists are cooler than dictionaries.  You can literally Slice a list.  You can += and multiply a list. [Edit: Oh! I see a bunch of cool stuff with dictionaries now -  the 'in' and the assignments are cool]

    Retrieving items: I know more about lists from the DiveIntoPython, than I do about Dictionaries.  To retrieve a value from a list, you have to know it's index number.  For a Dic, you need to know the key. 

    I've done a for loop on  lists, but not dictionaries.  I'll tackle this tomorrow.

     

    Now, it's off to 'Princess of Mars'. 

    on April 1, 2012, 11:57 p.m.

    Newman5 said:

    I found Pythonfiddle.com.  LO and BEHOLD, the learning!

     

    Here is my work:

    http://pythonfiddle.com/function-for-iterating-thru-a-list

    not quite finished yet...

     

    Well, it seems that's a bad link... and my work is gone.  Oh, the highs and lows of learning.  lunchtime.

    on April 2, 2012, 11:50 a.m. in reply to Newman5

    Jessy Kate Schingler said:

    d'oh! sorry about that :(. 

     

    on April 2, 2012, 1:44 p.m. in reply to Newman5

    Jessy Kate Schingler said:

    @newman5 cool, nice work!

    there's a couple of ways to look up a value in a dictionary but it's true, it's a bit harder than doing it by key. 

    one way is to use the dictionary's built-in .values() method to get a list of all the values in the dict. then you just have a regular python list to work with. you can use the "in" search method, ie,

    my_list = d.values()

    5 in my_list

    will return True or False. and as you point out, if you know the index, that works for retrieving the specific item. 

    you could also just query the dict values directly: 3 in dict.values()

    without creating an intermediary list. 

    check out the dict method "itemitems" for iterating over the each key-value pair, as a tuple... 

    on April 2, 2012, 1:56 p.m. in reply to Newman5

    Newman5 said:

    first off here is my 'learning pattern', tell me if you recognise it.  Many times, I'll read something and think "What the fudge (radio edit) does that mean? There is no way I can comprehend that.  It's like a foreign language." 

    It's like the time I was dropped off 400 miles (7hours by car) away from my house with the idea I was going to ride my bike home.  Crazy.  Does not compute.  My brain wouldn't let me accept it in reality...until I pedalled 50 miles the first day, until I was halfway home,  until I pedaled up to my front door. 

    Another example of a learning a foreign language... and this one is good because it illustrates your intention.  Okay, if you live in a foreign place where you don't speak the language, my advice is watch TV. At first you will feel like an idiot.  You don't understand. But if you keep you attention on understanding the language, something will happen. Slowly you will understand. 

    What once was Gibberish Gibberish Gibberish.  Will become Bibberish Libberish Tibberish.  Then, Bibberish 'WORD YOU EFFEN UNDERSTAND' Tibberish.  And, that feels good, oh yea. 

    If you don't do it with focus and attention, it won't happen.  You have to stress your brain a bit.  The struggle it seems is important.  And here is a tip for teachers - make the struggle germain. That is make it relevant to the thing you are learning.  Don't make the stress about some random wacky ass thing you probably made up (cough.grades.cough). Make it about whatever is on the title of the class website.

    Long story, but When I first read thru your reply, I was like WTF?  But I duckduckgo'ed some stuff.  I think you meant - iteritem dict method.  And 'tuple' is an ordered list of elements.  Slowly, I worked it out and I understood what you are saying.  Perhaps, next time my brain won't imediately jump to "Hey buddy.  You can't program. That's programmer talk.  You can't understand that, so don't even try."

    but I digress...

     

    on April 4, 2012, 5:07 p.m. in reply to Jessy Kate Schingler

    Newman5 said:

    Okay - pythonfiddle is promising but isn't ready for primetime.  It's not saving files for me or something.

    I'm using the IDLE python interpreter to write and play around with the code.  I suppose I can I can upload and link to the files. 

    By way of sharing learning:  I had the problem of 'python idle no color' and this link helped me - http://www.python-forum.org/pythonforum/viewtopic.php?f=17&t=11768

    on April 6, 2012, 12:21 a.m. in reply to Newman5

    Eenvincible said:

     

    Hi there

    I just completed this level of the challenge and I am very excited to jump into the next leve and explore farther. Instead of posting my results here, I decided to post them on my awesome blog that I purposefully started for this challenge.

    Please check it out here and remember to leave a link to yours so I can check it out!

    http://simpledeveloper.wordpress.com/2013/01/02/hone-your-powers-programming-101/

    If anyone has questions, they can ask and we can discuss and learn together! Thanks and see you around!

    on Jan. 2, 2013, 4:54 p.m. in reply to Newman5
  • Jos said:

    I've used node.js for this task. Some resources:

    - The node beginner book

    - video tutorials at nodetuts

    - all kinds of node goodies at howtonode

    If you want a head start you can look at using the get call in the HTTPS library included in node. For this taks you can either use the node interactive console or a file and run it from command line.

    on March 15, 2012, 7:18 a.m.

    Jessy Kate Schingler said:

    jos, it's *awesome* that you're doing this with a different language, and perfect that you included references so that others can do the same. great way to bootstrap learning a new language, i might even try this with node.js myself :D. 

    on March 15, 2012, 12:41 p.m. in reply to Jos

    Jos said:

    cool! Hope you like it! :D

    The only difference is that you have to provide a callback 'inside' the GET call itself cause of the non-blocking, asynchronous architecture. Same idea as if you were working in the browser with jquery or the likes. Have fun!

    on March 15, 2012, 1:45 p.m. in reply to Jessy Kate Schingler

    Eenvincible said:

     

    Hi 

    I just completed this level of the challenge and I am very excited to jump into the next leve and explore farther. Instead of posting my results here, I decided to post them on my awesome blog that I purposefully started for this challenge.

    Please check it out here and remember to leave a link to yours so I can check it out!

    http://simpledeveloper.wordpress.com/2013/01/02/hone-your-powers-programming-101/

    If anyone has questions, they can ask and we can discuss and learn together! Thanks and see you around!

    on Jan. 2, 2013, 4:54 p.m. in reply to Jessy Kate Schingler