Skip to content

Commit c342bb2

Browse files
Added useful algorithms
1 parent 2c1a97d commit c342bb2

File tree

1 file changed

+42
-0
lines changed

1 file changed

+42
-0
lines changed

README.md

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,13 @@ print('Hello there {}, {}'.format(name1, name2))# Hello there Andrei and Sunny
111111
print('Hello there %s and %s' %(name1, name2)) # Hello there Andrei and Sunny --> you can also use %d, %f, %r for integers, floats, string representations of objects respectively
112112
```
113113

114+
```python
115+
#Pallindrome check
116+
word = 'reviver'
117+
p = bool(word.find(word[::-1]) + 1)
118+
print(p) #True
119+
```
120+
114121
Boolean
115122
----
116123
**True or False. Used in a lot of comparison and logical operations in Python**
@@ -196,6 +203,14 @@ max([1,2,3,4,5])# 5
196203
sum([1,2,3,4,5])# 15
197204
```
198205

206+
```python
207+
# Get First and Last element of a list
208+
mList = [63, 21, 30, 14, 35, 26, 77, 18, 49, 10]
209+
first, *x, last = mList
210+
print(first) #63
211+
print(last) #10
212+
```
213+
199214
```python
200215
# Matrix
201216
matrix = [[1,2,3], [4,5,6], [7,8,9]]
@@ -236,6 +251,11 @@ sorted_by_key = sorted([
236251
key=lambda el: (el['name']))# [{'name': 'Andy', 'age': 18}, {'name': 'Bina', 'age': 30}, {'name': 'zoey', 'age': 55}]
237252
```
238253

254+
```python
255+
# Read line of a file into a list
256+
with open("myfile.txt") as f:
257+
lines = [line.strip() for line in f]
258+
```
239259

240260
Dictionaries
241261
----------
@@ -297,6 +317,12 @@ my_tuple.count('grapes') # 2
297317
list(zip([1,2,3], [4,5,6])) # [(1, 4), (2, 5), (3, 6)]
298318
```
299319

320+
```python
321+
# unzip
322+
z = [(1, 2), (3, 4), (5, 6), (7, 8)] #some output of zip() function
323+
unzip = lambda z: list(zip(*z))
324+
unzip(z)
325+
```
300326

301327
Sets
302328
---
@@ -541,6 +567,22 @@ Lambda
541567
# lambda <argument1>, <argument2>: <return_value>
542568
```
543569

570+
```python
571+
# Factorial
572+
from functools import reduce
573+
574+
n = 3
575+
factorial = reduce(lambda x, y: x*y, range(1, n+1))
576+
print(factorial) #6
577+
```
578+
579+
```python
580+
# Fibonacci
581+
fib = lambda n : n if n <= 1 else fib(n-1) + fib(n-2)
582+
result = fib(10)
583+
print(result) #55
584+
```
585+
544586
Comprehensions
545587
------
546588
```python

0 commit comments

Comments
 (0)