Categories
Computer Science School

01 04

Progress

I did not know a way to actually run a .py file. The runfile() does not seem to be a valid command. After floundering around with some commands from the internet, I found out that in Windows you can run scripts directly from the command line (Winkey + R, CMD). When in the DOS box, type python and then the file path. This is easier by locating the python file in Windows Explorer, Shift + right-click the file, and choose “Copy as path”. This took about 2 hours to find.

I looked at the coursework for “A Gentle Introduction to Programming Using Python (January IAP 2011)”. I consider it redundant to what I’m learning in the remedial portion of EECS. It even uses Think Python as its text. I’m dropping the course and will pick something else up in its place.

I looked at the coursework for “The History of Computing”. It’s a lot of reading in books I don’t have. I don’t know if they printed pages from various books or what, but merely getting all of the reading from the library via interlibrary loans would take weeks. I don’t know that the broader view of computing is worth finding all of this. I’m dropping the course and will pick something else.

I looked at “Artificial Intelligence” and watched some of the first lecture. I’m taking this course, I like the professor and think that I’ll like learning more about the subject from him. My only concern is that I might lack prerequisite knowledge.

I’m going to try “Mathematics for Computer Science”. It looks brutal, but it also seems necessary. I have no clue how to prove anything mathematically outside of parroting some Euclidean geometric proof right after reviewing it. Maybe I can change that with this course.

So this semester is looking like:
1. Introduction to CS and Programming in Python
2. Computation Structures
3. Artificial Intelligence
4. Mathematics for CS
5. Programming for the Puzzled
6. Introduction to Electrical Engineering and CS 1

Progress:
1. 4/12 (1/3!)
2. 1/22
3. 0/30
4. 0/4 units. Units: 0/11, 0/11, 0/5, 0/8.
5. 0/11
6. ? In remedial reading, I’m 3/19 (about 1/6).

Notes on coursework order:
1. I pretty much have to complete Intro to CS & Python and Think Python before I can do Programming for the Puzzled or EECS.
2. Based on my experience in Structures so far (1 lecture), I might need a full 22 days to digest it. That said, it doesn’t seem contingent on anything, so I can attack it whenever I feel like it.
3. I’m not sure I’m up to Mathematics for CS this semester, but I have to address the math at some point.

***

String Manipulation, Guess and Check, Approximation, Bisection; Intro to CS and Python lecture #3

s = "abc"
len(s) # is 3

#indexing
s[0]  # is a
s[1]  # is b
s[2]  # is c
s[3]  # is trying to index out of bounds, error
s[-1] # is c
s[-2] # is b
s[-3] # is a

#slicing
#differentiated from indexing by at least one colon
#[start : stop : step], step defaults to 1
s = "abcdefgh"
s[3:6]    # "def"
s[3:6:2]  # "df"
s[::]     # "abcdefgh", equivalent to s[0:len(s):1]
s[::-1]   # "hgfedbca", equivalent to s[-1:-(len(s)+1):-1]
s[4:1:-2] # "ec"

Strings are immutable. You cannot hot-swap letters in a string. You must redefine the string. You can use a string to redefine the string.

s = "hello"
s[0] = 'j' # this gives an error.
s = 'j' + s[1:len(s)]

# Check each letter in string s for an i or a u.
s = "abcdefghi"

for char in s:
    if char == 'i' or char == 'u':
        print("This character is an i or u.")
    else:
        print("This character is not an i or u.")

Cheerleader program Dr. Ana Bell wrote:

# An example written by Dr. Ana Bell

an_letters = "aefhilmnorsxAEFHILMNORSX"
word = input("I will cheer for you!  Enter a word: ")
times = int(input("Enthusiasm level (1-10): "))

for char in word:
    char = word[i]
    if char in an_letters:
        print("Give me an " + char + "! " + char)
    else:
        print("Give me a " + char + "! " + char)
print("What does that spell?")
for i in range(times):
    print(word, "!!!")

Guess-and-check methodology

cube = int(input("Enter an integer for cube root: "))
for guess in range(abs(cube)+1cube = int(input("Enter an integer for cube root: "))
for guess in range(abs(cube)+1):
    if guess**3 >= abs(cube):
        break
if guess**3 != abs(cube):
    print(cube, 'is not a perfect cube.')
else:
    if cube < 0:
        guess = -guess
    print('Cube root of '+str(cube)+' is '+str(guess))

This can be changed to be far more accurate using bisection when guessing, determining if the guess is high or low, and re-bisecting it accordingly. I changed Dr. Bell’s code to have the epsilon much smaller. It takes the program longer, but the results for perfect cubes are more likely to be integers.

cube = int(input("Enter an integer for cube root: "))
epsilon = .0000000000001
num_guesses = 0
low = 0
high = cube
guess = (high + low) / 2.0

while abs(guess**3-cube) >= epsilon:
    if guess**3 < cube:
        low = guess
    else:
        high = guess
    guess = (high + low) / 2.0
    num_guesses += 1
print('num_guesses =', num_guesses)
print(guess, 'is close to the cube root of', cube)

***

Decomposition, Abstraction, Functions; Intro to CS and Python lecture #4

Abstraction: you don’t need to know how a black box works in order to use the black box. E.g. we use computers and very few of us understand from start to finish how typing in a letter results in that letter being printed on the screen and stored in memory. Functions are the same way, they abstract blocks of code into easily read and used single-line entities. Suppress details.

Decomposition: divide the programming. Divide it into modules, functions, and classes.

  • self-contained
  • breaks up code
  • intended to be reusable
  • keeps code organized
  • keeps code coherent

Functions’ characteristics:

  • name
  • parameters (0 or more arguments)
  • docstring (optional but recommended for explaining what the function does to other people and future you)
  • body
  • returns something
def is_even(i):
    """
    Input: i, a positive int
    Returns True if i is even, otherwise False
    """
    print("inside is_even")
    return i%2 == 0

If the function doesn’t return anything, python will add “return None” if you don’t manually put it. (It’s optional.)

***

Leave a Reply

Your email address will not be published. Required fields are marked *