1. Numbers, Python

    Write a program which tells you:

    • how many hours are in a year?
    • how many minutes are in a decade?
    • how many seconds old are you?

    Code:

    print 24*365, 60*24*365*10, (60*60)*((365*32)+(30*8))
    

    Output:

    8760 5256000 42912000
    

    Harder: If I am 1031 million seconds old, how old am I?

    print '1,031,000,000 seconds = %s minutes' % (1031000000/60)
    print 'or %s hours' % (1031000000/(60*60))
    print 'or %s days' % (1031000000/(60*60*24))
    print 'or %s years' % (1031000000/(60*60*24*365.25))
    

    Output:

    1,031,000,000 seconds = 17183333 minutes
    or 286388 hours
    or 11932 days
    or 32.6704185363 years
    
     
  2. Numbers, PHP

    Write a program which tells you:

    • how many hours are in a year?
    • how many minutes are in a decade?
    • how many seconds old are you?

    Code:

    <?php
      echo 24*365, ", ", 60*24*365*10, ", ", (60*60)*((365*32)+(30*8));
    ?>
    

    Output:

    8760, 5256000, 42912000
    

    Harder: If I am 1031 million seconds old, how old am I?

    <?php
      echo "1,031,000,000 seconds = ", 1031000000/60, " minutes<br>";
      echo "or ", 1031000000/(60*60), " hours<br>";
      echo "or ", 1031000000/(60*60*24), " days<br>";
      echo "or ", 1031000000/(60*60*24*365.25), " years";
    ?>
    

    Output:

    1,031,000,000 seconds = 17183333.333333 minutes
    or 286388.88888889 hours
    or 11932.87037037 days
    or 32.670418536264 years
    
     
  3. Numbers, Ruby

    Write a program which tells you:

    • how many hours are in a year?
    • how many minutes are in a decade?
    • how many seconds old are you?

    Code:

    puts 24*365, 60*24*365*10, (60*60)*((365*32)+(30*8))
    

    Output:

    8760
    5256000
    42912000
    

    Harder: If I am 1031 million seconds old, how old am I?

    minutes = 1031000000/60
    puts '1,031,000,000 seconds = ' + minutes.to_s + ' minutes'
    hours = minutes/60
    puts 'or ' +hours.to_s + ' hours'
    days = hours/24
    puts 'or ' + days.to_s + ' days'
    years = days/365.25
    puts 'or ' + years.to_s + ' years'
    

    Output:

    1,031,000,000 seconds = 17183333 minutes
    or 286388 hours
    or 11932 days
    or 32.6680355920602 years
    

    Notes: Not sure why the decimals are different from perl.

     
  4. Numbers, Perl

    Write a program which tells you:

    • how many hours are in a year?
    • how many minutes are in a decade?
    • how many seconds old are you?

    Code:

    #!/usr/local/bin/perl
    print 24*365, ", ", 60*24*365*10, ", ", ((60*60)*((365*32)+(30*8))); 
    print "\n";
    

    Output:

    8760, 5256000, 42912000
    

    Harder: If I am 1031 million seconds old, how old am I?

    #!/usr/local/bin/perl
    print "1,031,000,000 seconds = ", 1031000000/60, " minutes", "\n";
    print "1,031,000,000 seconds = ", 1031000000/(60*60), " hours", "\n";
    print "1,031,000,000 seconds = ", 1031000000/(60*60*24), " days", "\n";
    print "1,031,000,000 seconds = ", 1031000000/(60*60*24*365.25), " years", "\n";
    

    Output:

    1,031,000,000 seconds = 17183333.3333333 minutes
    1,031,000,000 seconds = 286388.888888889 hours
    1,031,000,000 seconds = 11932.8703703704 days
    1,031,000,000 seconds = 32.6704185362638 years
    

    Notes: Easier to tie a string with an integer than in Ruby.

     
  5. The Point

    This site is an attempt to work through Learn to Program by Chris Pine.

    I don’t know the fundamentals as well as I should. I’m very good with HTML, CSS, and JS, but web technologies have rapidly moved far beyond these client-side languages. I don’t have a firm grounding in any server-side language.

    Learn to Program teaches Ruby, but its concepts are language-agnostic. I’ll take each exercise and attempt to solve them with the following languages, all installed with OS X:

    • Perl
    • PHP
    • Python
    • Ruby

    Code samples will be formatted as such:

    puts 2 + 3
    

    Outputs will be formatted similarly.

    Each post will be tagged with the subject of the post (numbers, strings, etc., corresponding to the chapter name in the book) and the language used.

    Of course, I welcome criticism: tell me how I can do better.