Skip to content

Commit 6b26e4a

Browse files
committed
Added algorithms and DAta structues, xrange and number converter
1 parent 0565126 commit 6b26e4a

File tree

10 files changed

+332
-1
lines changed

10 files changed

+332
-1
lines changed

README.md

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
12
## Chicken wings and python
23

34
1. [What you can find in this repository](#What-you-can-find-in-this-repository)
@@ -18,10 +19,13 @@ A part these two main projects, you can find few of the exerices that I have mad
1819
* [Vocal Personal Assistant](personal-assistant/README.md)
1920
* [A byte of Computer Vision](computer-vision/README.md)
2021
* [Rock Scissor or Paper game](rock_scissor_paper/rock_scissor_paper.py)
21-
* Leap year : check if a given year is a leap year or not - [Source Code](LeapYear/leapYear.py)
22+
* [Algorithms and Datastructures in Pyhon](algorithm_and_datastructure/)
23+
* [Leap year : check if a given year is a leap year or not](LeapYear/leapYear.py)
2224
* [Guess the number game](guess_the_number/guessTheNumber.py)
2325
* [Calculate distance between any two points](distance/points.py)
2426
* [given a natural string in input (in English language) and is able to print out the respective numeric encoding](DPOneySolution.py)
27+
* [Convert number a number to string, binary, octal and word](number_converter/number_converter.py)
28+
* [Difference between range and xrange](xrange_or_range/xrange_or_range.py)
2529
* [Form registration GUI with tkinter](form_registration_GUI/first_GUI.py)
2630
* [40 Python Interview questions](https://www.guru99.com/python-interview-questions-answers.html)
2731

algorithm_and_datastructure/array.py

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
'''
2+
Author: Davide Pollicino
3+
Date: 27/01/2020
4+
5+
Summary: Create an array in python using build it python data structure
6+
'''
7+
8+
9+
def display(array):
10+
# Print all the elements of the array in one line
11+
for element in array:
12+
print(element , end= " ")
13+
14+
15+
16+
def main():
17+
# Create an empty list / array
18+
list = []
19+
list.append(5)
20+
list.append(3)
21+
list.append(10)
22+
print("Array created")
23+
display(list)
24+
print("\nSize of the list: {0}".format(len(list)))
25+
print("\nLast element of the array: {0}".format(list.pop()))
26+
27+
if __name__ == "__main__":
28+
main()
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
'''
2+
Author: Davide Pollicino
3+
Date: 05/02/2020
4+
Summary: Console application that stores different numbers in a BST (Binary Search Tree) and print the BST in according
5+
with the inorder, preorder and postOrder algorithms.
6+
'''
7+
8+
class Node:
9+
def __init__(self, key):
10+
self.left = None
11+
self.right = None
12+
self.val = key
13+
14+
15+
# A utility function to insert a new node with the given key
16+
def insert(root,node):
17+
if root is None:
18+
root = node
19+
else:
20+
if root.val < node.val:
21+
if root.right is None:
22+
root.right = node
23+
else:
24+
insert(root.right, node)
25+
else:
26+
if root.left is None:
27+
root.left = node
28+
else:
29+
insert(root.left, node)
30+
31+
32+
# Travers our BST using the inorder algorithm
33+
def inorder(root):
34+
if root:
35+
inorder(root.left)
36+
print(root.val)
37+
inorder(root.right)
38+
39+
40+
# Travers our BST using the preOrder algorithm
41+
def preoder(root):
42+
if root:
43+
print(root.val)
44+
preoder(root.left)
45+
preoder(root.right)
46+
47+
# Travers our BST using the postOrder algorithm
48+
def postorder(root):
49+
if root:
50+
postorder(root.left)
51+
postorder(root.right)
52+
print(root.val)
53+
54+
55+
# drive code for testing purposes
56+
r = Node(50)
57+
insert(r,Node(15))
58+
insert(r,Node(34))
59+
insert(r,Node(56))
60+
insert(r,Node(32))
61+
insert(r,Node(11))
62+
insert(r,Node(99))
63+
# Print inoder traversal of the BST
64+
print('Inorder: ')
65+
inorder(r)
66+
print('Postorder: ')
67+
postorder(r)
68+
print('Preorder: ')
69+
preoder(r)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
'''
2+
Author: Davide Pollicno
3+
Date: 27/01/2020
4+
Summary: Implementation of the most widely used API of python list
5+
'''
6+
7+
# Built-in method: clear() -> It removes all the element from the list
8+
def clear_method(array):
9+
del array[:]
10+
return array
11+
12+
13+
def display(array):
14+
for el in array:
15+
print(el , end=" ")
16+
17+
18+
# Built-in method: len() -> returns the length / number of elements in a list
19+
def length(array):
20+
counter = 0
21+
for i in array:
22+
counter += 1
23+
return counter
24+
25+
# Built-in method: count() -> It returns the number of elements in a list
26+
def count_method(array):
27+
return len(array)
28+
29+
30+
# Built-in method: append() -> It add a new element at the last position of the list.
31+
def append_method(array , element_to_add):
32+
array[len(array):] = [element_to_add]
33+
34+
35+
# Starting point of the program
36+
def main():
37+
array = []
38+
39+
append_method(array , 5)
40+
append_method(array , 3)
41+
append_method(array , 450)
42+
append_method(array , 66)
43+
append_method(array , 12)
44+
45+
print("Number of elements in the array: {0}".format(count_method(array)))
46+
print("Array content: ")
47+
display(array)
48+
49+
print("\nLength of the array: {0}".format(length(array)))
50+
51+
clear_method(array)
52+
print("\nArray content after clear method: ")
53+
display(array)
54+
55+
56+
if __name__ == "__main__":
57+
main()

algorithm_and_datastructure/queue.py

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
'''
2+
Author: Davide Pollicino
3+
Date: 05/02/2020
4+
5+
Summary: Implement a queue in Python using a list
6+
'''
7+
# Create an empty list that we will be used a queue
8+
queue = []
9+
10+
# Add elements in the queue
11+
queue.append(1)
12+
queue.append(2)
13+
queue.append(3)
14+
queue.append(4)
15+
16+
# Print the content of the queue
17+
print(queue)
18+
19+
# Pop elements from the list
20+
print('Elements popped from the queue')
21+
print(queue.pop(0))
22+
print(queue.pop(0))
23+
print(queue.pop(0))
24+
25+
# Print the content of the queue
26+
print('Content of the list: ')
27+
print(queue)

algorithm_and_datastructure/readMe.md

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
## Algorithm And Data-structures
2+
3+
### What you can find in this folder
4+
5+
You will find the implementation of different data structures and algorithm using Python.
6+
7+
### How to contribute
8+
1. Fork the repository
9+
2. Do the desired changes (add/delete/modify)
10+
3. Make a pull request
11+
12+
### Mind that
13+
Constructive criticisms or code reviews of any kind are very much welcome.
14+
15+
If you have any questions about the solutions you can find here, feel free to contact me at: [[email protected]](mailto:[email protected]?subject=[GitHub]%ChickenWingsAndPythonRepo)
16+
17+
### Requirements
18+
19+
* Python interpreter
20+
21+
22+
### Connect with me
23+
24+
* [[email protected]](mailto:[email protected]?subject=[GitHub]%20CompetitiveProgrammigGuide)
25+
* [Linkedin](https://www.linkedin.com/in/davidepollicino7/)
26+
* [www.davidepollicino.com](http://davidepollicino.com/)
27+
28+
### Notes
29+
In python, it is possible to implement a stack using
30+
* list
31+
* deque
32+
* LifoQueue
33+
34+
For our implementation, we have decided to implement a stack using a list
35+
36+
## Queue
37+
Queue in Python can be implemented by the following ways:
38+
39+
* list
40+
* collections.deque
41+
* queue.Queue
42+
43+
* Enqueue: Adds an item to the queue. If the queue is full, then it is said to be an Overflow condition – Time Complexity : O(1)
44+
* Dequeue: Removes an item from the queue. The items are popped in the same order in which they are * pushed. If the queue is empty, then it is said to be an Underflow condition – Time Complexity : O(1)
45+
* Front: Get the front item from queue – Time Complexity : O(1)
46+
*Rear: Get the last item from queue – Time Complexity : O(1)
47+

algorithm_and_datastructure/stack.py

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
'''
2+
Author: Davide Pollicino
3+
Date: 05/02/2020
4+
5+
Summary: Implement a stack in Python using a list
6+
'''
7+
8+
# Create an empty List that is going to be used as Stack
9+
stack = []
10+
11+
# Add (push()) value inside our stack
12+
stack.append(1)
13+
stack.append(2)
14+
stack.append(3)
15+
stack.append(4)
16+
stack.append(5)
17+
18+
# Print the content of the stack
19+
print('\nStack: ')
20+
print(stack)
21+
22+
# Pop elements from the stack
23+
print('\nstart of pop operations: ')
24+
print(stack.pop())
25+
print(stack.pop())
26+
print(stack.pop())
27+
print(stack.pop())
28+
29+
print('\nStack after the pop operations: ')
30+
print(stack)
31+
32+
# Get the size of the stack => Number of elements inside the list
33+
print('Size of the stack:', len(stack))

number_converter/README.md

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
## Number conversion
2+
3+
The number conversion is a python exercise that uses the str(), oct(), hex() built in function.

number_converter/number_converter.py

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#!/usr/local/bin/python
2+
"""
3+
Author: Davide Pollicino
4+
Date: 06/05/2020
5+
Summary: Python script that having in input a number conver it in string, hexadecimal ,
6+
octal string prefixed with “0o” and binary, using the bin() method, that will
7+
provide a string with a 'ob' prefix.
8+
"""
9+
10+
from num2words import num2words # pip install num2words
11+
12+
13+
def get_user_number():
14+
number_to_convert = int(input('Inset a number to convert: '))
15+
return number_to_convert
16+
17+
18+
def convert_number(number_to_convert):
19+
print('Number in string ' + str(number_to_convert))
20+
print('Number in words: ' + num2words(number_to_convert))
21+
print('Number in decimal ' + bin(number_to_convert))
22+
print('Number in oct ' + oct(number_to_convert))
23+
24+
25+
if __name__ == '__main__':
26+
number = get_user_number()
27+
convert_number(number)

xrange_or_range/xrange_or_range.py

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
#!/usr/local/bin/python
2+
"""
3+
Author: Davide Pollicino
4+
Date: 06/05/2020
5+
6+
range() – This returns a range object (a type of iterable).
7+
xrange() – This function returns the generator object that can be used to
8+
display numbers only by looping. Only particular range is displayed on demand
9+
and hence called “lazy evaluation“
10+
"""
11+
12+
# initializing a with range()
13+
a = list(range(1,10000))
14+
15+
# initializing a with xrange()
16+
"""
17+
In python 3 there is not anymore range adn xrange.
18+
If we want a range object of type iterable we now need to do for example list(range(1, 1000))
19+
"""
20+
try:
21+
# Python 2
22+
x = xrange(1,10000)
23+
except NameError:
24+
xrange = range
25+
x = xrange(1,10000)
26+
27+
28+
# testing the type of a
29+
print ("The return type of range() is : ")
30+
print (type(a))
31+
# print (a) -> Output: ALl the numbers between 1 and 1000
32+
33+
# testing the type of x
34+
print ("The return type of xrange() is : ")
35+
print (type(x))
36+
print (x) # Output: range(1,1000)

0 commit comments

Comments
 (0)