-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcoding-cheats.py
More file actions
114 lines (94 loc) · 4.31 KB
/
coding-cheats.py
File metadata and controls
114 lines (94 loc) · 4.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
===============================================================================================
Inputs::
==============================================================================================
n,c = input().split also works
n,c = map(int,input().split())
n,k = [int(x) for x in input().split()]
input().split(maxsplit=1) #Accept int and string at once
******************************************************************************
Array Inputs Single Line::
import sys --> Single Line Input in Python
f = sys.stdin
#n = int(f.readline())
A = [int(x) for x in f.readline().split()]
li = [int(x) for x in input().split()]
********************************************************************************
=============================================================================================
string.lower() ==> Converts upper case to lower case
String.upper()
Converting string into list...!
str = "nagaraj"
list1 = list(str)
===============================================================================================
Shortcuts & Techiniques::
===============================================================================================
sum(range(n)) --> Sum of 'n' natural numbers
list(range(n)) --> List of 'n' numbers
List Operations:
list.append('param')
list.extend('param')
list.count('param')
list.reverse()
list.sort()
list.index('param')
list.remove('param')
list.clear()
list.copy()
Sets Operations::
a = set('abracadabra')
b = set('alacazam')
a # unique letters in a
{'a', 'r', 'b', 'c', 'd'}
a - b # letters in a but not in b
{'r', 'd', 'b'}
a | b # letters in a or b or both
{'a', 'c', 'r', 'd', 'b', 'm', 'z', 'l'}
a & b # letters in both a and b
{'a', 'c'}
a ^ b # letters in a or b but not both
{'r', 'd', 'b', 'm', 'z', 'l'}
Stacks & Queues::
stack = [] ***************** #Last In - First Out Algo **************
stack.append('param')
stack.pop()
from collections import deque
queue = deque(["Eric", "John", "Michael"])
queue.append('param')
queue.popleft()
Squares calculation::
squares = list(map(lambda x: x**2, range(10)))
squares = [x**2 for x in range(10)]
*************Command Line Args*******************
if __name__ == "__main__":
import sys
fib(int(sys.argv[1]))
===============================================================================================
=================================================================================================
Floatin Points Output::
=================================================================================================
print('%.2f'%a) --> decimal precission upto 2 floating points
print('{0:.2f}'.format(a)) --> another way for floating point representaiton
***************************************
==================================================================================================
==================================================================================================
Sample Code::
====================================================================================================
******************Python Code for String reversal***********
for _ in range(int(input())):
s = input()
words = s.split('.')
strings = []
for word in words:
strings.insert(0,word)
print(".".join(strings))
*************************************************************
******************Shortcut for Fibonacci series:**********************
a,b = 0,1
while a < 100:
print(a,',')
a,b = b,a+b
***********************************************************************
======================================================================================================
To overcome Time Limit Exceeded Error::-
import psyco
psyco.full()