Wednesday, December 10, 2014

Activity 1.3.8 - While Loops

Number Guesser Code:

I always have to shrink these pictures down a lot so they don't mess with the blog's appearance

Conclusion Questions:

1.) If you change between 1 and 20 from the previous program to between 1 and 6000, how many guesses will you need to guarantee that you have the right answer? Explain.

The most efficient way to guess would be to guess the number directly in the middle of the low and high to see if that guess is too high/too low. That allows you to remove half of the numbers from what you have to guess next. For example, between 1 and 20, you would guess 10. If 10 is too high, then your next guesses would be limited to 1-9, because you're told that 10+ is too high. Then you would keep checking the new number that is halfway between the new high and low, and continue doing that until you're left with two numbers to guess from. Cutting the total number of possible answers in half each time, this method creates a logarithmic pattern - e.g. 256, 128, 64, 32, 16, 8, 4, 2, 1. So, the number of guesses you'd need has to be the lowest power of two that's greater than the number of choices. 2^13 = 8192, which is higher than 6000 and the lowest power of two that creates such a number. So, with this method, it'd only take 13 guesses to get the number!

2.) Describe the difference between a while loop and a for loop.

A for loop is often used for lists and tuples. It runs a set of instructions for each item within an array of values. A while loop is used with conditional expressions that could run for an indefinite amount of time, unlike for loops, which simply run through a list.

Monday, December 8, 2014

Activity 1.3.7 - For Loops

Hangman Code:

Click on the image to enlarge it.


Lottery Ticket Code:

Click on the image to enlarge it.


Conclusion:

1.       Sometimes code using an iterative loop can be written without a loop, simply repeating the iterated code over and over as separate lines in the program. Explain the disadvantages of developing a program this way.

This program would run slower, as there are more lines that the computer has to run through. It also clogs up the code and makes it more difficult for programmers to read. Iterative loops help keep the code concise.

2.      Name a large collection across which you might iterate.

If you were to update the grades of everyone in the high school, you would be dealing with a large amount of students - a lot of data. Using iterative loops would help to streamline the process.

3.      What is the relationship between iteration and the analysis of a large set of data?

An iteration is some set of tasks completed on a particular object, one at a time. Iterative loops can be used to analyze large sets of data, using a relatively small amount of code to iterate each bit of data.

Thursday, December 4, 2014

Activity 1.3.6 - Tuples and Lists

Dice Roller/Letter Picker Code:


Conclusion Questions:

1.       Consider a string, tuple, and list of characters.

In []: a = 'acbde'
In []: b = ('a', 'b', 'c', 'd', 'e')
In []: c = ['a', 'b', 'c', 'd', 'e']

The values of a[3], b[3], and c[3] are all the same. In what ways are a, b, and c different?

a[3] is in a string, and cannot be indexed like a list or tuple. b[3] is in a tuple, which means it's immutable - unable to be altered in any way. c[3] is in a list, which means it works like a tuple (an array of indexed values), but it can also be changed (mutable).


2.      Why do computer programming languages almost always have a variety of variable types? Why can't everything be represented with an integer?

There are many variable types because we need a way to manipulate integers and values in a way that allows us to create functions and solve problems. While the computer eventually transforms the code into integers - 1's and 0's - it is manipulated at the programming level by substituting integers with letters and other symbols, which allows us to do more complex things with it.

Tuesday, December 2, 2014

Activity 1.3.5 - Strings

Tweet Verifier Code:


Conclusion Questions:

1.       How many characters are in this sentence? Does it matter whether Python is storing the string as one byte per character or four bytes per character?

There were 41 characters in the first sentence of the question. It shsouldn't matter how the string is stored, as long as it's at least one byte, since all alphabetical characters can be stored via ASCII code within a single byte.

2.      This question asks you about something you have not learned. In fact, the question is asking about details that go beyond what you will learn in this course. However, wondering what is going on at a lower level of abstraction – and talking about it – can be a useful strategy when learning about computing.

Describe what you think occurs in memory when the following code is executed.

In []: a = 'one string'
In []: b = 'another'
In []: c = a[:3] + ' and ' + b
In []: print(c[6:10])

This code concatenates several pieces of string together and prints a section of the concatenation. "a" and "b" are stored in the memory as 'one string' and 'another', respectively. Then the memory is storing a different variable, "c", as the concatination of the first three letters in "a" ('one'), ' and ', and all of "b" ('another'). This creates the string 'one and another', which is what the memory stores "c" as. Then part of c is printed, from the 6th to 10th character, counting the former and having the first character be counted as the 0th.

This piece of code should print the string 'd an', and, after testing in Canopy, it does. Yay!