Download as pdf or txt
Download as pdf or txt
You are on page 1of 13

José Manuel Solano Ochoa

Datos 3A
Profesor: Ángel Pech

Algorithms and data structures.


Exercises Unit 1
Abstract
Elementary data is the simplest form of information. To be able to use them when

programming, you need a data structure, which are ways to organize a set of data to be able to

handle them efficiently. Some of the data structures are: array, queue, tree, stacks, sets, tuple.

Exercise #1

Instructions:

Given 2 lists, find the elements that are in one list and not in another list Example: # find

([1,2,3,5,6], [6,3,2,3]) -→[1,5] # find ([1,2,3, 'hello' ]), [3,2] →[1, 'hello ' ]
José Manuel Solano Ochoa
Datos 3A
Profesor: Ángel Pech

Code:

explanation:

Being the first exercise of the project I wanted to do something simple with easy

structures, I only used "for" cycles and comparison structures, in this case "if". The exercise asks

that from two possible lists the values that are in one of the two lists are printed but that it is not

repeated in both.

Operation explanation:
José Manuel Solano Ochoa
Datos 3A
Profesor: Ángel Pech

The first thing I did was put the lists in the code and create a third empty list, in this list

the values that are not repeated in both lists will be added. Next, the next step was to create a

"for" loop that repeats to the equivalent of data numbers in list 1. Within that loop I put an "if "

to buy if there was a data from list 1 that was in list 2, and then added to list 3.

For the next part I did exactly the same, but only in vice versa, data from list 2 that

appeared in list 1, and then added to list three.

In the end just send to print list 3 with the legend: numbers that are not in both lists.

Output:

Exercise #2

Instructions:

Given 2 list of numbers, verify that they are equal, that they contain the same values.

Return True if they are the same, and False if they are different.

Example:

#equals ([1,2,4,5,6]), ([6,5,4,2,1]) →True

#equals ([1,1,2,4,5,6]), ([6,5,4,1,2,1]) →True

#equals ([1,1,2,4,5,6]), ([6,5,4,1,2,3]) →False


José Manuel Solano Ochoa
Datos 3A
Profesor: Ángel Pech

Code:

Code explanation: Define a function that divide the given tuple in two arrays, then, using

.sort() it sort the elements from min to max making easier the comparison between both arrays,

then, using an if clause, it compare if both array (test0 and test1) are the same, determining if the

condition is true or false.


José Manuel Solano Ochoa
Datos 3A
Profesor: Ángel Pech

Operation explanation:

I solved this exercise more simply than the previous one, just add the two lists that must

be compared, and then put the ".sort" characteristic to each list so that they would sort the lists in

an ascending way. Already with the ordered lists, we only have to implement an "if" to compare

that the lists are the same. If the lists are the same, it will throw us the value "True" and if they

are not, it will throw us the value "False".

Output:

Exercise 3:

Instructions:

print a binary tree by levels. On each line, show a list with the elements of that tree level

Code:
José Manuel Solano Ochoa
Datos 3A
Profesor: Ángel Pech
José Manuel Solano Ochoa
Datos 3A
Profesor: Ángel Pech

Code explanation:

Using the same methods as before, but adding a new one, the .pop() function

that generates a new list.

Operation explanation:

Declarating BinaryTree we are generating a “node” with two free spaces, and using the

.pop() we replicate the function of root using insert and insertR, but inside one of the free spaces

of root, and doing it again in the place we want.

Finally, to print all the levels we just call the function “BinaryTree” but using an index.

Output:
José Manuel Solano Ochoa
Datos 3A
Profesor: Ángel Pech

Exercise 4:

Instructions:

Sum of pairs of numbers

Parameters: List of numbers, value

Given a list of numbers, display the pairs of numbers that add up to the value

passed as parameter. The function, in addition to showing the pairs of numbers,

will return the number of pairs of numbers that add up to that value. For example,

add pairs ([1,3,2,2],4) →(1,3) (2,2)

Code:
José Manuel Solano Ochoa
Datos 3A
Profesor: Ángel Pech

Code explanation:

In this code we have the purpose that in a list of two values which the first is another list

and the other value is only a number, so you have to grab the last value of the list so that the

values of the other list add that number but in pair, and print them with the legend that these are

the pairs that add that number.

Operation explanation:

We start by adding the list provided by the exercise, and we add a new variable to be able

to get the values that are from the list that is within the main list. Already with that we can begin

our peer selection process. We create a "for" cycle that repeats the times equivalent to the

number of data in the "values" list, within loop, we create another "for" cycle but that is to try all

possible combinations and from those select those that add that selected number. We also create

an empty list with the name "Pairs" so that in that list the pairs that add that number are saved
José Manuel Solano Ochoa
Datos 3A
Profesor: Ángel Pech

and printed, this list is empty again after each tour so that the pairs are updated in the final

printing.

Output:

Exercise #5

Given the general tree of exercise #3, classify and affirm its structure taking into

account the Root, Son, Father, Siblings, Terminal Nodes or Leaves, Interior Nodes.
José Manuel Solano Ochoa
Datos 3A
Profesor: Ángel Pech

Code:
José Manuel Solano Ochoa
Datos 3A
Profesor: Ángel Pech

Code explanation:

Using the same methods as before, but adding a new one, the .pop() function

that generates a new list.

Operation explanation:

Declarating BinaryTree we are generating a “node” with two free spaces, and using the

.pop() we replicate the function of root using insert and insertR, but inside one of the free spaces

of root, and doing it again in the place we want.

Finally, to print all the levels we just call the function “BinaryTree” but using an index.

Output:
José Manuel Solano Ochoa
Datos 3A
Profesor: Ángel Pech

You might also like