WSQ08 “I am 196”

--Originally published at Python fundamentals

Instrunctions

Your job is to create a program that asks the user for two pieces of data:

  • The lower bound of the sequence
  • The upper bound of the sequence

Then you check the values from the lower bound (inclusive) to the upper bound (inclusive) and make a report of them. During the analysis of each number, if a Lychrel number is found it should be reported immediately with something like “Found a Lychrel number: 196”.

The report must show:
  • The range of numbers analysed (lower to upper bound)
  • The number of natural palindromes (no addition to inverse needed)
  • The number of non-Lycherels encountered (become palindromes)
  • The number of Lycherel number candidates (that did not converge to palindrome)

Since you will not be able to prove that a number is Lycherel (since you cannot computer forever to check), our definition for a Lycherel candidate will be if a number does not converge after 30 iterations of applying the addition to the inverse.

Procedure

We will create a function that using a for loop and a palindrome funcion, (wich we are going to import) will determine if a number its a natural palindrome or not.

Then in the for loop we will use a while loop that will be execute 30 times, we will put two ifs in this while loop, one that will tell us if the numer after 30 iteration or less, results being palindrome and another one that will tell us if it doesn’t happen.

wsq08(2)

wsq08


#quiz06

--Originally published at Python fundamentals

Instructions

Return to the 46 exercises page and solve the exercises 6 up to 10.

Procedure

Exercise 6:

Define a function sum() and a function multiply() that sums and multiplies (respectively) all the numbers in a list of numbers. For example, sum([1, 2, 3, 4]) should return 10, and multiply([1, 2, 3, 4]) should return 24

We will create 2 funcitons, one for addition and other for multiplication, in the first one we will create a loop that will go through ach one of the numbers on the list and add into a new variable know as total.

In the second one, we will do the same but instead of addition we will use substraction.

exercise6.png

Exercise 7:

Define a function reverse() that computes the reversal of a string. For example, reverse("I am testing") should return the string "gnitset ma I".

We will create a function that uses a for loop to go through each one of the characters of a string and add it to a new string callled “word” in an inverse order.

exercise7.png

Exercise 8:

Define a function is_palindrome() that recognizes palindromes (i.e. words that look the same written backwards). For example, is_palindrome("radar") should return True.

We will use our current reverse function to find the reversal of a string, if the reversal its the same as the original string, then palindorme its true, we will do this second part in another function called palindrome.

Exercise8

Exercise 9:

Write a function is_member() that takes a value (i.e. a number, string, etc) x and a list of values a, and returns True if x is a member of a, False otherwise. (Note that this is exactly what the in operator does, but for the sake of the exercise you should pretend Python did not have this

Exercise9
Exercise10
Continue reading "#quiz06"

#quiz05

--Originally published at Python fundamentals

Instructions

You should resolve the first 5 exercises of this page

Procedure

Exercise1:

Define a function max() that takes two numbers as arguments and returns the largest of them. Use the if-then-else construct available in Python. (It is true that Python has the max() function built in, but writing it yourself is nevertheless a good exercise.)

We will create a function that will take 2 paramethers and through a comparison between both will determine wich one its the highest

Exercise 1

Exercise 2:

Define a function max_of_three() that takes three numbers as arguments and returns the largest of them.

We will use our current max_of_two function, but instead of just copy and past it, we will import it as a module using from Exercise1 import max_of_two.

Next we will use max_of_two in a new function, that will takes 3 paramethers and through a binary operation (two by two at a time) will determine wich one of this its higher.

exercise2

Exercise 3:

Define a function that computes the length of a given list or string. (It is true that Python has the len() function built in, but writing it yourself is nevertheless a good exercise.)

We will create a function that contains a for loop wich will be updated each iteration at a time.

 

Exercise 4:

Write a function that takes a character (i.e. a string of length 1) and returns True if it is a vowel, False otherwise.

We will create a function that takes one paramether and  through a boolean comparison will determine if its a vowel.

Exercise4

Exercise 5:

Write a function translate() that will translate a text into “rövarspråket” (Swedish for “robber’s language”). That is, double every consonant and place an occurrence of "o" in between. For example, translate("this is fun") should return the string "tothohisos

exercise5.png
Continue reading "#quiz05"

#WSQ09

--Originally published at Python fundamentals

Instructions

So for this assignment I would like to see you create a function that receives as parameter the name of a file (this would be a string value like data.txt) and your function counts the number of lines and the number of characters in the file which it returns as a single value (but with two values). You will want to look at how to use and return a tuple from a function and how to open and read text files line by line.

Procedure

We will create a text file in the same folder than our code and put some text in it, i personally choose a fragment of “The Aetherium Wars“, a book from the game of the “Elder scroll: Skyrim”.

Once we have our text the next thing that we will do its to use the open method and pass the name of the file as a paramether and create an object from this method.

Then we will create a loop that will iterates the number of lines of our text and in each line, it will count the number of characters.

At the end of the loop will return us the number of total characters in all the text and the number of lines, all this will be in a tuple.

WSQ09

References: https://www.youtube.com/watch?v=S1EjCcZUw34


WSQ07

--Originally published at Python fundamentals

The instructions

Create a program that asks the user for 10 numbers  (floating point). Store those numbers in a list. Show to the user the total, average and standard deviation of those numbers.

The procedure

We will import the statistics module, wich will help us to find the mean and the standard deviation, i didn’t know if its posible to find the total of a list of numbers using a method, but since it its easy to get it by hand, wi will create another function that will take our list and get the total using a for loop.

In  our main function we will use the while loop to iterate n times the loop until the user use the key phrase “no more values”.

The code

WSQ07 (2)

References: https://www.youtube.com/results?search_query=stdev+python


WSQ06(Factorial)

--Originally published at Python fundamentals

The instructions

Create a program that asks the user for a non-negative integer (let’s call that number n) and display for them the value of n! (n factorial).

After showing them the answer, ask them if they would like to try another number (with a simple y/n response) and either ask again (for y) or quit the program and wish them a nice day (if they answered n).

The procedure

Programing its the art of take a problem and cut it into small chunks, we will do this creating 3 functions:

-One that will take the information from the user.

-Another one that will find the factorial of the number given by the user.

-Finally one that will ask if the user wants to find the factorial from another number and if     the answer its positive it will call the first function.

Here is the code:

WSQ06 (2)


“A world without work” My toughts (related post)

--Originally published at Python fundamentals

3 months ago i saw a talk hosted by the World Economic Forum wich was about AI and its implications, the talk was amazing and i deeply recomend it to everyone, especially if someone is thinking in start a career in computer science or engineering.

I will share my personal toughts and a brief explanation of specifically what the talk is about.

The talk happens like this:

The host ( Hiroko Kuniya ) starts presenting the speakers, wich are a business man, a minister and 2 important profesors(from economics and engineering respectively), she questions their guesses about what do they think about the inminent job losses when the AI would become smarther than humans. What will happend with the poverty?the equality? What kind of jobs would stay when that happens? What actions the goverment will have to take?What we are doing to face that in this moment?Are we prepare?

My personal thoughts

I really don’t know what to think, initially its amazing how our specie started living in caves and fighting for its own survival to potentially don’t have to be bother for even making a living.

But like all great oportunity comes with a great risk, forget Terminator or Matrix, the inequality will grow exponentially, even with the basic income, and the distribution of power will be immovable without the innovation component in our side.


How to create an encrypted message (related post)

--Originally published at Python fundamentals

 

Hi! Today we are going to learn: How to create an encrpyted message using a primitive technique called Caesars cipher.

The Caesars cipher is an ansient encryptation technique that was use by Julius Caesars to communicate with his generals.

1487846344_346e900866_q

It consist in take a letter of the alphabet and change it by another letter, then do the same with the next letter of the alphabet, using the alphabetic order, for instance if we are going to replace the a with the b, then we are going to replace the b with the c, c with d, d with e and so one and so forth…

The important here is  to respect the order.

For this exercise, we are going to replace the d with the a.

The programing

The first step to write our code it will be define a function that takes one paramether (the text that is going to be encrypted), this funtion will create a new alphabet string using string.ascii_lowercase and our variable newalphabet =  alphabet[3:] + “abc”.

Once that we have both  alphabets, (the one that holds our code and the normal one) we just need to replace each character of our messege with its equivalent of our newalphabet using a for loop.

We will do the same but changing the newalphabet for the normal one on the loop af a new function called Caesardecrypter, wich will decript our messege.

caesar1

caesar2


Python conventions (Zen of python)

--Originally published at Python fundamentals

15516383916_2141ab2e52_z

image by Tina Leggio

Maybe you didn’t knew it but, python has its own philosophy in the way that the code is written, this phylosiphy its a little extensive but it certainly help us to be more productive.

Here is the complet python pilosophy, wich its called Zen :

Beautiful is better than ugly.
Explicit is better than implicit.
Simple is better than complex.
Complex is better than complicated.
Flat is better than nested.
Sparse is better than dense.
Readability counts.
Special cases aren't special enough to break the rules.
Although practicality beats purity.
Errors should never pass silently.
Unless explicitly silenced.
In the face of ambiguity, refuse the temptation to guess.
There should be one-- and preferably only one --obvious way to do it.
Although that way may not be obvious at first unless you're Dutch.
Now is better than never.
Although never is often better than *right* now.
If the implementation is hard to explain, it's a bad idea.
If the implementation is easy to explain, it may be a good idea.
Namespaces are one honking great idea -- let's do more of those!

See the original on the official site

A bref explanation

Beutifull its a combination of explicit, simple, sparse and complex code that its easy to read, that is actually the mean purpuse of this, make the code easy to understand and read, if we can’t understand our own code, nobody will do it and it can’t be reusable (readability counts).

Here is a deeper explanation of explicit, simple, sparse and flat.

Explicit: Provides a clear explanation of what your program its doing.

Simple: Provides clarity when the code its readed.

Complex: Means that its better to use a simple but complex solution of 4 lines of code than one that have 15 but its

Continue reading "Python conventions (Zen of python)"

Quiz week 8

--Originally published at Python fundamentals

art-1455802_1280.jpg

Image from: Macklay62

Hi, in this # quizz8 we did an algorithm that should return us the fibonacci of n natural number(including 0).
This algorithm could seem tough at first but it’s pretty simple:

Firstly, we make a while loop that will be executing when our i variable is diferent from our n number, this allow us to execute the program as much as we need it

to return the correspondant fibonacci.
In the whille loop we put the fibonacci formula and the exit variable.

When the while loop ends we print the z variable.

Now our code returns us the fibonacci of n natural number, nevertheless it can not take 0 as a parameter, to fix that,  we use an if conditional  that prints 0 when n its less than 1.

screenshot-2017-03-03-11-13-02