# #Quiz3

The third quiz of the course comes with two more situations to be solved.

The first of these consists in defining a function that finds the distance between two points, therefore, it should accept four parameters(an x and y for each point) and return one output value. To get the distance between two points you should first get the difference between the two values of x and the two values of y, respectively, get the square of each, add them and get the square root of that whole value. As in the pythagorean theorem, where c = √(a² + b²). Powers in python are expressed with the same symbol as multiplication, but doubled. For example, 6 ** 3 = 216. In this case, instead of a and b, we have (x1-x2) and (y1-x2).

Our defined function should look like this :

————————————————————————–

————————————————————————–

As you can see, I have imported the math module to use sqrt, short for square root. Although this could be easily replaced by **(1/2), as raising the power of a number to a half is the same as getting its square root.

This is our defined function, once we call it with a set of user input parameters, we’ll be able to test if our definition was correct. First, let’s type in those inputs :

————————————————————————–

Editor …

————————————————————————–

As you could have already noticed, I called float on each input, this is because the user may want to input decimals and not only integers, plus we want the square root’s output, and that of the function in general, to be as exact as possible.

Next, we should call our function with those inputs as values for the function’s parameters:

————————————————————————–

Editor :

————————————————————————–

Of course, we have to print to actually see

# #Quiz2

Well, first of all, what the quiz asks us to do is create a file and write down a function called “superpower”. It should accept two parameters and return the value of first parameter elevated to the power of the second parameter, for example, superpower(a, b) should return the value of a^b power. This was my answer :

————————————————————————–

Editor :

Terminal :

————————————————————————–

There. So the code works perfectly, maybe we could add some user-friendliness with some more strings.

————————————————————————–

Editor :

Terminal :

————————————————————————–

Nice going.

The next part of the quiz is to write a function called stars that accepts one parameter and prints as many stars in the same line. For example, stars(3) should print “***”, and stars(9) should print “*********” .

————————————————————————–

Editor :

Terminal :

————————————————————————–

Done, right? The stars are printed in the same line because of the comma after the string. That should be enough. Not quite, the stars are printed, but the comma causes a space between each star. After thinking about this for a little, I figured we could append each star on a list and join them, but actually it’s easier to join them as it is, as a string. Talking about easier solutions, now that I think about it, multiplying the string would be way easier:

————————————————————————–

Editor :

Terminal :

————————————————————————–

Yes, it works. Well, there must be more ways of doing this but I don’t think the solution can get any easier, so… I’m leaving this up to here.

This is my post for the #quiz2 on my Programming Fundamentals course.

# #WSQ09 – Fahnniest with numbahs

Yes. I know, the “fun/funnier/funniest” pun, It’s done, get over it.

Today’s practice is about factorials. In math, these are expressed as “n!” and the expression equivalent to it is n! = (n)*(n-1)*(n-2)… and so on, until 1. for example 3! = (3)(2)(1) = 6, 4! = (4)(3)(2)(1) = 24, and 5! = (5)(4)(3)(2)(1) = 120.

So… As you might expect, our code needs to take an input an produce it’s factorial as an output. We can do this with a loop… or recursively, and yes, I’ll go over both.

Factorial using loops is kind of the easy way and… It works best for your computer, let’s just say you’ll save your computer a lot of resources. Plus, we kind of already did this with loops, let’s open up wsq07, remove one of the limits and rename the other one as the user’s input, replace the initial value of c with 1 instead of 0, and inside the loop replace the addition with multiplication. Like this :

————————————————————————–

Editor :

Terminal :

————————————————————————–

Great! it works. What pass does is literally nothing, we just don’t want i to multiply c when it equals 0, range(1, a+1) would work as well. What if we want to use a while loop instead of a for?

Well, we’d have to use c as a counter instead of a printable variable and use the input variable as the printable variable :

————————————————————————–

Editor :

Terminal :

————————————————————————–

c -= 1 is the same as c = c -1. We use c > 1 as conditional instead of c > 0, because when c = 1, then the loop’s output would be 0.

Our time is now. Actually… not our time, it’s more like… recursion time. Python supports recursion, this

# WSQ07 – Funnier with numbahs

The pun in the name, yes I know, I had to do it, get over it.

So. Today’s practice obviously includes numbers… and also loops. If you’re keeping up with my posts, you should know about while loops, but now we’re deep into for loops. What are these? Well, if while loops go on until their condition isn’t met, for loops are kind of the opposite, they go on until their conditions are met… for example, if I want to print each letter in a string I’d do something like :

————————————————————————–

for letter in “string” :

print letter

————————————————————————–

And the output would be :

————————————————————————–

s

t

r

i

n

g

————————————————————————–

Now that we’re all about for loops, let’s go on with the show.

Our code should ask for two values, integers, and the output should be the sum of all the numbers between those two values, including them. For example, for the input 2 and 8, the code should make the operation 2 + 3 + 4 + 5 + 6 + 7 + 8 and return 35.

That’s simple, right? The first thing that came to my mind was a scene about the School of rock movie, the second was this :

————————————————————————–

Editor :

Terminal :

————————————————————————–

What range does is exactly that, make a for loop run a certain number of times, the loop will run the number of times equivalent to the number of values between the first and the second input, and the value of i will be each and every one of those numbers, so our code easy does it, right?

So we’re done! Ok, cool.

But wait! What if the user inputs integers in an inverted order? for example,

# WSQ06 – Randomising randomness

Hello, people. We’re feeling random today. Random as in python’s random library. Think about libraries as groups of functions that python doesn’t always have at hand. You see, when you use python, your code may have many purposes and python is no medium to tell which functions you are going to use, you actually can do a lot with just python’s basic functions but for example, if you’re working on a  math-related code you might as well import the math library, if you’re just generating random numbers or using a specific distribution, then you should import the random library, as we’re doing today :

————————————————————————–

Editor :

————————————————————————–

Now we can use any of the functions included in the random library. You might be asking what kind of wicked code is it that we’re working on today, but it’s not that crazy, and not hard at all. Today’s code just needs to pick a random integer from 1 to 100, then ask for a user input from 1 to 100 to make the user guess the random generated number :
————————————————————————–

Editor :

————————————————————————–

Notice that every time you use a function from a library you’ve imported, you need to type the name of a library and a period before calling the function. There’s a way to avoid this which is to import a specific function from any library, for example :

from random import randint

In this case, you wouldn’t need to type “random.” in your current code, but it is recommended to use the first method, as it lets you remember, and anyone know, where each function was imported from.

Anyways, back on our practice, this code let’s the user play only one time. Maybe we’d like the user to play until he’s got the number

# WSQ05 – Checking Temperatures

We’re back on python and straight into our practices. You might wonder why I’m leaving WSQ04 undone, that’s because that practice isn’t really about coding but something more of an opinion essay. I really want to concentrate on the real work right now and then move on to that.

Today’s practice is about temperature’s units. The goal of this is to take a user input number as Fahrenheit degrees and transform it into Celsius units. So, here goes our first try :

———————————————————————-

———————————————————————-

I’ll edit this later on.

# Quiz01 – Defining and Testing

First quiz, and as I had no computer to work with, I answered it on paper. The first step was to create a python file named prog1.py that contains a function that has a cylinder’s radius and a height as parameters and outputs the cylinder’s volume. Now let’s test what I did:

———————————————————————-

———————————————————————-

There’s the function, but that’s just the beginning, let’s test it, of course :

———————————————————————-

Editor :

Terminal :

———————————————————————-

To prove my answer as correct I  called my function with a and b as parameters, when a and b are inputs by the user, raw_input returns data as strings, that’s why I call float on it. Here I am using python 2.7.11, if you are using python 3 you’d have to change raw_input for input and add parentheses when calling print on volume: print(volume(a, b)).

Next is the second part. As expected, the new python file is named prog2.py, it should ask for two numbers and return their product, their integer division, and the remainder from their division. This is what I turned in :

———————————————————————-

Editor :

Terminal :

———————————————————————-

This second part didn’t ask to use float numbers, but integers, thus int is called on the input. It wasn’t necessary to edit this any further to prove the answer correct, as it proves itself.

This is my post for the first quiz on my Programming Fundamentals course.

# WSQ03-Fahn with numbers

In today’s post we’re having fahn with numbers. We’re back on our elementary school days, learning how to sum, subtract, multiply, divide, get the division’s remainder, etc., we know our stuff when coming  to this, right? But what about python?… Well, doing arithmetical operations in python is much more easy, just learn how to ask for it and python will do it for you :

———————————————————————-

———————————————————————-

There, we’re making a sum, right? but wait!

———————————————————————-

———————————————————————-

Our program doesn’t do anything at all… Well, duh, we’re not printing anything, so this should do it :

———————————————————————-

Editor :

Terminal :

———————————————————————-

Yeah, there. 2 + 1 = 3, that’s right… right?… google cal… 2 + 1… yep, that’s 3. Great, so that’s how it’s done. now lets try all the other operations:

———————————————————————-

# WSQ02 – “Hello World”

This one’s pretty simple, just print “Hello World”. It’s a very common thing to do when you’re learning a new programming language, and in regards to this I’ve found a like for codecademy, I think it’s great for learning languages as it safely takes you step by step in a very interactive way. I used it to learn python and finished the course in less than a 24 hours, and although I can’t say I master the language, I do know how to do what I need.

But back to coding issue, python is very very simple. For your program to print “hello world” you just literally need to type:

———————————————————————-

———————————————————————-

Yes, as easy as that. This should work for both, python 2 and 3. I do rather work with the former, as most of the forums and help you may be looking for are most likely found in that format, but the differences are minimal. The big difference about both versions in regards to this hello world code would be that python 2 accepts print with or without parentheses, while print will only work in python 3 if it has them. input and raw_input is also another big difference, and speaking about it I might as well start using these:

———————————————————————-

Python 2:

Python 3:

———————————————————————-

Now, if you’re new to python, I’d like to encourage you to try to figure out what this means before reading the explanation.

Here, whatever you write in between the parentheses will show on the terminal/shell. raw_input/input is for the user to write and input something into the program, the code right here will print whatever the user inputs. But it only does it once… so what if the user wants to print more than just one message? Worry

# WSQ01-First Real Homework

First real homework I’ll do on my blog.

Nope, just kidding, I won’t actually code anything on this one, I’ll just download the stuff I need to start coding. I can’t believe I have to write about this but, well, there’s a reason for this… I hope so.

I didn’t have an available computer to work at last semester, now I do, and so my excuses to fail the course have vanished. I must add that the machine at my service is a very decent MacBook and I’m very, very happy working with it, except for a few things… I bet it is relevant to say that I hadn’t ever used a mac before. Well, after a few web-surfing I found out how to use mac’s terminal “bash” and realised I didn’t have to download python, as mac has python’s 2.7 version pre-installed. That was great news… buuuut!

Yes, there’s a but. I couldn’t run anything on the terminal because apparently it had “no Xcode selected”. So I looked for the version of Xcode that corresponded to my OS version, OS X Lion or 10.7 and found out that the Xcode’s version compatible with it wasn’t supported any more, and the current version of Xcode wasn’t compatible with my OS.

I finally decided to download the latest version of OS X, Xcode and Atom, my editor of choice. It took hours, almost all the afternoon. I’m completely done for the day. I hate this.

This post is for my #WSQ01 on the programming fundamentals course.