fizzbuzzyo

This commit is contained in:
Armen Vartan 2014-12-02 14:27:35 -05:00
parent 54bb07c4bc
commit 7a562bb597
3 changed files with 82 additions and 71 deletions

View File

@ -63,12 +63,9 @@ Ok, now onto FizzBuzz.
Open the main.js file and find the loop inside the document ready.
Write code that does the following:
* if i is divisable by 3, append a line that reads "Fizz"
* if i is divisable by 5, append a line that reads "Buzz"
* if i is divisable by 3 & 5, append a line that reads "FizzBuzz"
Write code that does the following:
* if i is divisible by 3, append a line that reads "Fizz"
* if i is divisible by 5, append a line that reads "Buzz"
* if i is divisible by 3 & 5, append a line that reads "FizzBuzz"
All this should be done as usual, to the printout div.

View File

@ -1,73 +1,76 @@
####Conditionals and For Loops
Conditionals and For Loops
==========================
Get ready for FizzBuzz!
Get ready for FizzBuzz, again!
This is a classic programming test - that yes, you will actually see at job interviews.
#### For Loop
It basically just checks if you can write a for loop and if you know what [modulo](http://en.wikipedia.org/wiki/Modulo_operation) is.
For loop syntax in Python is slightly different.
The 'for in' pattern is still available in Python:
```
>>> for thing in list:
... print(thing)
```
Python also has what's known as a range:
```
>>> lst = ['a', 'b', 'c']
>>> for i in range(len(lst)):
... print(i)
0
1
2
```
If we wanted to print the element in the list, rather than the index, we could do it like this:
```
>>> lst = ['a', 'b', 'c']
>>> for i in range(len(lst)):
... print(lst[i])
a
b
c
```
With range, you have the option to choose the start and stop points, as well as increment.
```
>>> for i in range(2, 10, 2):
... print(i)
2
4
6
8
```
As you can see, the end point is not included as part of the range.
###For Loop
Fire up your Python interpreter and try it out.
This is the most basic loop we write in Javascript, and most languages for that matter. The ubiquitous For Loop.
#### Conditionals - Switch
for(var i = 0; i<=100; i++){ console.log(x) }
Switch statements are basically the same in Python. You can write if statements in two ways:
```
if(some condition):
do something
```
or
```
if some condition:
do something
```
for else clauses, it's also pretty similar.
Here is an example:
```
if condition:
do something
elif another condition:
do something else
else:
catch all of the other possibilities
```
In the first argument, we declare an incrementing variable, i, and where it will start. We could have called it anything.
#### FizzBuzz
In the second argument, we state the conditions under which the following code should be executing. In this case, it is while i is less than or equal to 100.
Time for FizzBuzz in Python.
In the third argument, we say that every time the code is run, i should increment by 1. i++ is shorthand, also known as [syntactic sugar](http://en.wikipedia.org/wiki/Syntactic_sugar) for i += 1, or i = i + 1. They all do the same thing.
To get a slightly different view of it, we could do it this way.
for(var x = 500; x > 150; x-=5){ console.log(x) }
Try it in your browser or node console and watch it work. What is the last number it prints? Why?
###Conditionals - If / Else
The most simple conditional statement is the if statement. This is also ubiquitous across many programming languages.
Basically it is written like this:
if(some condition){
do this
}
You can make it more powerful by specifying an else. This is a catch all for things to do if the first condition isnt met. For example:
if(cute store clerk has brown hair){
give phone number
} else {
leave store
}
Finally, we can specify multiple outcomes using an else if.
if(cute clerk has brown hair){
give phone number
} elseif(cute clerk has blond hair) {
get phone number
} else {
buy playstation
}
Instead of this plain english example, also known as [Pseudocode](http://en.wikipedia.org/wiki/Pseudocode)
Get the idea?
###Fizzbuzz
Ok, now onto FizzBuzz.
Open the main.js file and find the loop inside the document ready.
Write code that does the following:
if i is divisable by 3, append a line that reads "Fizz"
if i is divisable by 5, append a line that reads "Buzz"
if i is divisable by 3 & 5, append a line that reads "FizzBuzz"
All this should be done as usual, to the printout div.
Write code that does the following:
* if i is divisible by 3, print "Fizz"
* if i is divisible by 5, print "Buzz"
* if i is divisible by 3 & 5, print "FizzBuzz"
* if i is not divisble by 3 or 5, print i

View File

@ -0,0 +1,11 @@
def fizzbizz(num):
def assertion(actual, expected):
print(str(actual) + " == " + str(expected) + " : " + str(actual==expected))
assertion(fizzbuzz(33), "Fizz")
assertion(fizzbuzz(20), "Buzz")
assertion(fizzbuzz(30), "FizzBuzz")
assertion(fizzbuzz(32), 32)