The Problem
Think CS C++

As an exercise, character classification and conversion library to write functions named apstringToUpper and apstringToLower that take a single apstring as a parameter, and that modify the string by converting all the letters to upper or lower case. The return type should be void.

The Code

/* For my benefit, given that my compiler does not have apstring.cpp,
 I will be using an array of characters. */

#include <iostream>
#include <ctype.h>

using namespace std;

int index;

void stringToUpper(string s){
 index = 0;
 while (index <= s.length()){
 if (isalpha(s[index])){
 char l = toupper(s[index]);
 else {

void stringToLower(string s){
 index = 0;
 while (index <= s.length()){
 if (isalpha(s[index])){
 char l = tolower(s[index]);
 else {

int main()
 // Test cases
 string string1 = "somewhere";
 string string2 = "SOMETIME";
 string string3 = "someHOW386";



 return 0;

/* OK, I used string. When I tried using arrays, the compiler gives
 me an error which I do not yet understand. */

The Problem
MIT OCW 6.189 A Gentle Introduction to Programming Using Python
Lab 6 Problem 2 – Pig-Latin Converter

Write a program that lets the user enter in some English text, then converts the text to Pig-Latin. To review, Pig-Latin takes the first letter of a word, puts it at the en and appends “ay”. The only exception is if the first letter is a vowel, in which case we keep it as is and append “hay” to the end.

E.g. “hello” -> “ellohay”, and “image” -> “imagehay”

It will be useful to define a list or tupple at the top called VOWELS. This way you can check if a letter x is a vowel with the expression x in VOWELS

It’s tricky for us to deal with punctuation and numbers with what we know so far, so instead, ask the user to enter only words and spaces. You can convert their input from a string to a list of strings by calling split on the string:

"My name is John Smith".split(" ")
-> ["My", "name", "is", "John", "Smith"]

Using this list, you can go through each word and convert is to Pig-Latin. Also, to get a word except for the first letter, you can use word[1:].

Hints: It will make your life much easier – and your code much better – if you separate tasks into functions, e.g. have a function that converts one word to Pig-Latin rather than putting it into your main program code.

The Code

VOWELS = ("a", "e", "i", "o", "u", "A", "E", "I", "O", "U")

print "\nEnglish - Pig-Latin Converter v0.1"
print "\nThis program converts English text to Pig-Latin."
print "\nNote: Punctuations and numbers are not yet supported."
print "      Input only letters and spaces."
print "      Uppercase is not yet fully supported."

def conv_consonant_start(word):
 pig_latin_word = word[1:] + word[0] + "ay"
 return pig_latin_word

def conv_vowel_start(word):
 pig_latin_word = word + "hay"
 return pig_latin_word

engl = raw_input("Input the text you would like to convert: ")
engl_words = engl.split(" ")

pig_latin_words = []

for i in engl_words:
 first_letter = i[0]
 vowel = False
 if first_letter in VOWELS:
 vowel = True
 if vowel == True:

print "\n\tEnglish:", engl
print "\tPig-Latin:", " ".join(pig_latin_words)

The Problem
MIT OCW 6.189 A Gentle Introduction to Programming Using Python
Lab 6 Problem 1 – Collision detection of balls

Many games have complex physics engines, and one major function of these engines is to figure out if two objects are colliding. Weirdly-shaped objects are often approximated as balls. In this problem, we will figure out if two balls are colliding.

We will think in 2D to simplify things, though 3D isn’t different conceptually. For calculating collision, we only care about a ball’s position in space and its size. We can store position with its center x-y coordinates, and we can use its radius for size. So a ball is a tupple of (x, y, r).

To figure out if two balls are colliding, we need to compute the distance between their centers, then see if this distance is less than the sum of their radii. If so, they are colliding.

Write a function that takes two balls and computes if they are colliding. Then call the function with two sets of balls. The first set is (0, 0, 1) and (3, 3, 1); these should not be colliding. The second set is (5, 5, 2) and (2, 8, 3); these should be colliding.

The Code

from math import sqrt, pow

def balls_collide(ball1, ball2):
 distance = sqrt(pow((ball1[0] - ball2[0]), 2) + pow((ball1[1] - ball2[1]), 2))
 sum_radii = ball1[2] + ball2[2]
 if distance < sum_radii:
 return True
 return False

print balls_collide((0, 0, 1), (3, 3, 1))
print balls_collide((5, 5, 2), (2, 8, 3))

Themes, Skins, Feathers

Ever since I found out about the source code highlighting feature in WordPress, I have become uncertain about using the Motion theme in Hack.Origin.

Yesterday, I have decided to do a theme rotation to suit the blogs better.

Lightstreams will now use the Tarski theme. I thought the theme is best suited in this blog because, seeing as how the blog is called “Lightstreams”, I thought, that the blog should have a lighter feel.

Lights, Wings, Flight, which I felt needed a change of themes, will use the Motion theme. I know WordPress has introduced a few more themes after Motion but none of them felt like I can use them for this blog.

Though, the theme poses some problems for me, they’re easier to deal with than having to deal with using a theme that I don’t love.

And, Hack.Origin, which needed the change the most, will now use the Monochrome theme which was previously used by Lightstreams.

This was due to the fact that the source code highlighter uses a white background for the source code space – which is very reasonable. Motion, which has a dark shade, does not match well with the source code highlighter.

Thus, the rotation has been set.

The Problem
#MIT OCW 6.189 A Gentle Introduction to Programming Using Python
#Problem 18

Write a definition for a function that takes a list and returns the sum of all the elements in that list.

The Code

def sum_elements(my_list):
    &quot;&quot;&quot;Returns the sum of all the elements in my_list&quot;&quot;&quot;
    i = 0
    sum = 0
    while i &lt; len(my_list):
        sum += my_list[i]
        i += 1
    return sum

The Problem
#MIT OCW 6.189 A Gentle Introduction to Programming Using Python
#Problem 15

“Write a definition for the following functions. shiftreverse_shift does the opposite – it moves the last element to the beginning of the list.

Neither function returns anything!

The Code

def shift(my_list):
    """Shifts the list [x1, x2, ..., xn] to [x2, x3, ..., xn, x1]"""
    n = len(my_list)
    i = 0
    first = my_list[0]
    while i < n:
        if (i + 1) < n:
            my_list[i] = my_list[i + 1]
        i += 1
    my_list[n-1] = first

def reverse_shift(my_list):
    """Shifts the list [x1, x2, ..., xn] to [xn, x1, x2, ..., x(n-1)]"""
    n = len(my_list)
    i = n - 1
    last = my_list[n - 1]
    while i >= 0:
        if (i - 1) >= 0:
            my_list[i] = my_list[i - 1]
        i -= 1
    my_list[0] = last