bf16da83dfa2e1e70a21b1e217041b36d3e29ec7019d948714
{EAV_BLOG_VER:a6b6a0065a96b431}
The Problem
Think CS C++
7.14
As an exercise, character classification and conversion library to write functions named
apstringToUpper
andapstringToLower
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 bevoid
.
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]); cout<<l; //cout<<toupper(s[index]); } else { cout<<s[index]; } index++; } cout<<"\n"; } void stringToLower(string s){ index = 0; while (index <= s.length()){ if (isalpha(s[index])){ char l = tolower(s[index]); cout<<l; //cout<<tolower(s[index]); } else { cout<<s[index]; } index++; } cout<<"\n"; } int main() { // Test cases string string1 = "somewhere"; string string2 = "SOMETIME"; string string3 = "someHOW386"; stringToUpper(string1); stringToUpper(string2); stringToUpper(string3); stringToLower(string1); stringToLower(string2); stringToLower(string3); 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 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 else: return False print balls_collide((0, 0, 1), (3, 3, 1)) print balls_collide((5, 5, 2), (2, 8, 3))
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 Code
... ... void setup(){ int sqSize = 180; size(sqSize, sqSize); frameRate(80); } float dm; int x = 0; int y = 0; int count = 0; void draw(){ if (count == 0){ background(255); } if (count ((width * 3) + 10)){ fill(255); stroke(255); dm = ((width/20) * 5) + 3; } point(x, y); point(x, (y + (width - 3))); point(y, x); point((y + (width - 3)), x); point((y + ((width/3) - 2)), x); point((y + ((width/3) * 2) - 2), x); point(x, (y + (width/3) - 2)); point(x, (y + ((width/3) * 2) - 2)); float[] drawWhich = new float[5]; drawWhich[0] = (width * 3) - (((width * 3) / 15) * 6); drawWhich[1] = (width * 3) - (((width * 3) / 30) * 9); drawWhich[2] = (width * 3) - (((width * 3) / 15) * 3); drawWhich[3] = (width * 3) - ((width * 3) / 10); drawWhich[4] = (width * 3); if (count == drawWhich[0] || count == (drawWhich[4] + (width*3) - 80)){ ellipse((width/2),(width/6),dm,dm); } else if (count == drawWhich[1] || count == (drawWhich[3] + (width*3) - 80)){ ellipse(((width/6)*5 + 1),(width/2),dm,dm); } else if (count == drawWhich[2] || count == (drawWhich[2] + (width*3) - 80)){ ellipse(((width/6)*5 + 1),((width/6)*5),dm,dm); } else if (count == drawWhich[3] || count == (drawWhich[1] + (width*3) - 80)){ ellipse((width/2),((width/6)*5),dm,dm); } else if (count == drawWhich[4] || count == (drawWhich[0] + (width*3) - 80)){ ellipse((width/6 + 1),((width/6)*5),dm,dm); } y++; x++; count++; if (x > width){ x = 0; } if (y > 2){ y = 0; } if (count == ((width*3) + 10)){ x = 0; y = 0; } if (count > ((width*6) + 10)){ x = 0; y = 0; count = 0; } } ...
The ellipses mean that there’s more code before, in between, and after.
Also, it’s from an HTML page so, you are pretty certain that there really is more…
S | M | T | W | T | F | S |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | |||
5 | 6 | 7 | 8 | 9 | 10 | 11 |
12 | 13 | 14 | 15 | 16 | 17 | 18 |
19 | 20 | 21 | 22 | 23 | 24 | 25 |
26 | 27 | 28 | 29 | 30 | 31 |