-
Notifications
You must be signed in to change notification settings - Fork 0
/
For Lectures 1042T.py
1285 lines (947 loc) · 25.3 KB
/
For Lectures 1042T.py
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#Set Tuples Done
a_list = [0, 1, 0]
size_list = len(a_list)
a = a_list[0]
a_list.append(2)
a_list.append(2)
a_set = {0, 1, 0}
#a = a_set[0]
size_set = len(a_set)
a_set.add(2)
a_set.add(2)
for i in a_list:
print (i,end=",")
print ()
for i in a_set:
print (i,end=",")
##
a_list = [0, 1, 0]
b_list = [0, 0, 1]
a_set = {0, 1, 0}
b_set = {1, 0}
if ( a_list == a_set ):
print ("1")
elif ( a_set == b_set):
print ("2")
elif (a_list == b_list):
print ("3")
elif ( b_list == b_set):
print ("4")
else:
print ("5")
##
a_list = [0, 1, 0]
a_tuple = (0, 1, 0)
c = a_tuple[1]
a_list[0] = c*2
# a_tuple[0] = c*2
##
def symmetric_about_x (x, y ) -> tuple:
"""
returns the symmetric point aboutthe x axis:
>>> symmetric_about_x ( 3, 5 )
(3, -5)
>>> symmetric_about_x ( -3, 5 )
(-3, -5)
>>> symmetric_about_x ( 3, -5 )
(3, 5)
>>> symmetric_about_x ( 0, 0 )
(0, 0)
"""
return (x,-y)
eg1 = symmetric_about_x ( 3, 5 )
eg2 = symmetric_about_x ( -3, 5 )
eg3 = symmetric_about_x ( 3, -5 )
eg4 = symmetric_about_x ( 0, 0 )
x1, y1 = symmetric_about_x ( 3, 5 )
x2, y2 = symmetric_about_x ( -3, 5 )
x3, y3 = symmetric_about_x ( 3, -5 )
x4, y4 = symmetric_about_x ( 0, 0 )
print (eg1, x1, y1)
print (eg2, x2, y2)
print (eg3, x3, y3)
print (eg4, x4, y4)
##
import math
def quad_root (a:int , b:int , c:int ) -> tuple:
"""
returns the two roots of the quadratic:
ax^2 + bx +c
>>> quad_root(0, 1, 2)
(-math.inf, math.inf)
>>> quad_root(2, 1, 2)
(None, None)
>>> quad_root(2, 4, 2)
(-1.0, -1.0)
>>> quad_root(1, -6, 8)
(4.0, 2.0)
"""
if a == 0:
return (-math.inf, math.inf)
disc = b**2 - 4*a*c
if disc < 0:
return (None, None)
sqrt_disc = math.sqrt(disc)
two_a = 2* a
return ((-b+sqrt_disc)/two_a, (-b-sqrt_disc)/two_a)
equ_1 = quad_root(0, 1, 2)
equ_2 = quad_root(2, 1, 2)
equ_3 = quad_root(2, 4, 2)
equ_4 = quad_root(1, -6, 8)
(root1,root2) = quad_root(0, 1, 2)
(root3,root4) = quad_root(2, 1, 2)
(root5,root6) = quad_root(2, 4, 2)
(root7,root8) = quad_root(1, -6, 8)
##
import math
def quad_root (a:int , b:int , c:int ) -> set:
"""
returns a set of the two roots of the quadratic:
ax^2 + bx +c
>>> quad_root(0, 1, 2)
{-math.inf, math.inf}
>>> quad_root(2, 1, 2)
{None}
>>> quad_root(2, 4, 2)
{-1.0}
>>> quad_root(1, -6, 8)
{4.0, 2.0}
"""
if a == 0:
return {-math.inf, math.inf}
disc = b**2 - 4*a*c
if disc < 0:
return {None, None}
sqrt_disc = math.sqrt(disc)
two_a = 2* a
return {(-b+sqrt_disc)/two_a, (-b-sqrt_disc)/two_a}
equ_1 = quad_root(0, 1, 2)
equ_2 = quad_root(2, 1, 2)
equ_3 = quad_root(2, 4, 2)
equ_4 = quad_root(1, -6, 8)
##
# Import and Modules Done ( from root import quad_root )
# Practice Checkerboard
RED = "R"
BLACK = "B"
checkers = [[BLACK] * 8 for i in range(8)]
for i in range(8):
for j in range(8):
if (i+j) % 2 != 0:
checkers[i][j] = RED
for i in range(8):
for j in range(8):
print(checkers[i][j], end=' ')
print()
print()
# Dictioneries
#d = {19:44,13:22}
#print(d[19]) #44
#print(d.get(13)) #22
#print(d[3]) #Error
#print((d.get(3))) #None
#Keys should be unique, not values
# dic = {1:'hello',2:'hello'}
# Without dict
RED = "R"
BLACK = "B"
KING = "K"
QUEEN = "Q"
BISHOP = "B"
ROOK = "R"
KNIGHT = "N"
PAWN = "P"
board = [[BLACK] * 8 for i in range(8)]
for i in range(8):
for j in range(8):
if (i+j) % 2 != 0:
board[i][j] = RED
board[0][0] = (RED, QUEEN)
board[7][7] = (BLACK, KNIGHT)
def get_position(colour, rank):
for i in range(8):
for j in range(8):
if board[i][j] == (colour, rank):
return (i, j)
return None
print(get_position(RED, QUEEN))
print(get_position(BLACK, KNIGHT))
#With dict
RED = "R"
BLACK = "B"
KING = "K"
QUEEN = "Q"
BISHOP = "B"
ROOK = "R"
KNIGHT = "N"
PAWN = "P"
board = {(RED, QUEEN):(0,0), (BLACK, KNIGHT):(7,7)}
def get_position(colour, rank):
return board.get((colour, rank))
print(get_position(RED, QUEEN))
print(get_position(BLACK, KNIGHT))
# create a dictionary
#Keys #Values
video_games = {101: ("Resident Evil", 1996),
102: ("Sonic the Hedgehog", 1991),
103: ("Super Mario 2", 1993),
104: ("Super Mario 64", 1996),
105: ("Final Fantasy VII", 1996)}
# get a key's value
name = video_games[101]
year = video_games.get(101)
# get an element from key's value
name = video_games[101][0]
year = video_games.get(101)[1]
#get a list of all keys
keys = video_games.keys()
for key in keys:
print (key)
#get a list of all values
values = video_games.values()
for value in values:
print (value)
#get a list of all items (key:value pairs)
items = video_games.items()
for item in items:
print(item)
#Update an item
video_games.update({105: ("Final Fantasy VII", 1997)})
#Update items (if key doesn't exist, add to the dictionary)
video_games.update({105: ("Final Fantasy VII", 1997), 106:("Donky Kong Country", 1994)})
# Text Icrementing
#String Split
txt = "welcome to the jungle"
x = txt.split()
print(x)
['welcome', 'to', 'the', 'jungle']
##
#String Strip
#!/usr/bin/python
str = "0000000this is string example....wow!!!0000000";
#print str.strip( '0' )
#When we run above program, it produces following result −
#this is string example....wow!!!
##
# String Punctuation
#https://docs.python.org/2/library/string.html
##
#Simple One-by-One:
word = word.strip(string.punctuation)
word = word.lower()
# Compound Big-Bang:
word = word.strip(string.punctuation).lower()
##
#Python list sort
cars = ['Ford', 'BMW', 'Volvo']
cars.sort()
print(cars)
#['BMW', 'Ford', 'Volvo']
##
# Set to list
Input : {1, 2, 3, 4}
Output : [1, 2, 3, 4]
Input : {'Geeks', 'for', 'geeks'}
Output : ['Geeks', 'for', 'geeks']
Approach #1 : Using list(set_name).
#Typecasting to list can be done by simply using list(set_name).
# Python3 program to convert a
# set into a list
my_set = {'Geeks', 'for', 'geeks'}
s = list(my_set)
print(s)
#Output:
['Geeks', 'for', 'geeks']
# Python3 program to convert a
# set into a list
def convert(set):
return list(set)
# Driver function
s = set({1, 2, 3})
print(convert(s))
#Output:
[1, 2, 3]
##
# Chess board using list
RED = "R"
BLACK = "B"
KING = "K"
QUEEN = "Q"
BISHOP = "B"
ROOK = "R"
KNIGHT = "N"
PAWN = "P"
board = [[BLACK] * 8 for i in range(8)]
for i in range(8):
for j in range(8):
if (i+j) % 2 != 0:
board[i][j] = RED
board[0][0] = (RED, QUEEN)
board[7][7] = (BLACK, KNIGHT)
def get_position(colour, rank):
for i in range(8):
for j in range(8):
if board[i][j] == (colour, rank):
return (i, j)
return None
print(get_position(RED, QUEEN))
print(get_position(BLACK, KNIGHT))
##
RED = "R"
BLACK = "B"
KING = "K"
QUEEN = "Q"
BISHOP = "B"
ROOK = "R"
KNIGHT = "N"
PAWN = "P"
board = {(RED, QUEEN):(0,0), (BLACK, KNIGHT):(7,7)}
def get_position(colour, rank):
return board.get((colour, rank))
print(get_position(RED, QUEEN))
print(get_position(BLACK, KNIGHT))
##
#Unit Testing
#Fibonacci
def fibonacci(n: int) -> int:
'''Return the Fibonacci number F_n for positive
values of n, where F_1 = 1, F_2 = 1,
and F_n = Fn-1 + F_n-2, n > 2
>>> fibonacci(1)
1
>>> fibonacci(2)
1
>>> fibonacci(3) -> 2
>>> fibonacci(7) -> 13
'''
a = 1
b = 1
for i in range (1, n + 1): # Bug! 2nd argument should be n
a, b = b, a+b
return a
print(fibonacci(1))
print(fibonacci(2))
print(fibonacci(3))
print(fibonacci(7))
##
#https://wiki.python.org/moin/UnitTests
##
#....
#Nested Loops
n=4
for i in range(1,n+1):
for j in range(1,i+1):
print(j,end=' ')
print()
'''
1
12
123
1234
'''
#range(1,1) = None
n=4
for i in range(1,n+1):
for j in range(1,i):
print(j,end=' ')
'''
1
12
123
'''
# Chessboard Example
RED = "R"
BLACK = "B"
KING = "K"
QUEEN = "Q"
BISHOP = "B"
ROOK = "R"
KNIGHT = "N"
PAWN = "P"
# wrong way
board = [[None] * 8] * 8
print("All", board)
board[0][0] = (RED, QUEEN)
board[7][7] = (BLACK, KNIGHT)
print("All 2", board)
# right way - 1 Using nested loops
board = [None] * 8
for i in range(8):
board[i] = [None] * 8
board[0][0] = (RED, QUEEN)
board[7][7] = (BLACK, KNIGHT)
print("All 3", board)
# right way - 2 Using Generator
board = [[None] * 8 for i in range(8)]
board[0][0] = (RED, QUEEN)
board[7][7] = (BLACK, KNIGHT)
print("All 4", board)
def get_piece(x,y):
return board[x][y]
print("The piece at (7,7) is", get_piece(7,7))
def get_number_pieces(colour):
num = 0
for x in range(8):
for y in range(8):
if board[x][y] != None:
c,r = board[x][y]# or c = board[x][y][0]
if (c == colour):
num += 1
return num
def get_winner():
num_black = get_number_pieces(BLACK)
num_red = get_number_pieces(RED)
if num_black == num_red:
return "T"
elif num_black < num_red:
return RED
else:
return BLACK
print(get_winner())
##
# Version 1 (Poor)
ecor1042 = [["100123456", 8, 9, 0, 5, None],
["101987654", 3, 8, 8, 7, None]]
for student in range(len(ecor1042)):
total = 0
for quiz in range(1, 5):
total += ecor1042[student][quiz]
print(total)
ecor1042[student][5] = total / 4
print(ecor1042)
# Version 2 - With sub-lists
ecor1042 = [["100123456", [8, 9, 0, 5], None],
["101987654", [3, 8, 8, 7], None]]
for stNum in range(len(ecor1042)):
total = 0
student = ecor1042[stNum]
quizzes = student[1]
for quiz in range(len(quizzes)):
# (also works with) for quiz in quizzes:
#total += quiz
total += quizzes[quiz]
print(total)
student[2] = total / len(quizzes)
print(ecor1042)
# Version 3 - With tuples, but with a hidden range
# Remember tuples are immutable!
ecor1042 = [("100123456", [8, 9, 0, 5], None),
("101987654", [3, 8, 8, 7], None)]
for stNum in range(len(ecor1042)):
total = 0
student = ecor1042[stNum]
num, quizzes, average = student
for quiz in range(len(quizzes)):
total += quizzes[quiz]
print(total)
average = total / len(quizzes)
ecor1042[stNum] = (num, quizzes, average)
print(average)
# without this line, the average is not updated in the list
print(ecor1042)
# Dictionary Version
ecor1042 = {"100123456":[[8, 9, 0, 5], None], "101987654":[[3, 8, 8, 7], None]}
for student in ecor1042:
##
# Numeric Algorithyms
# Numerical Analysis
#https://en.wikipedia.org/wiki/Numerical_analysis
#root_x = math.sqrt(x)
#Exhaustive Enumeration
#def exhaustive_sqrt(x:float)->float:
EPSILON = 0.01
step = EPSILON **2
guess = 0.0
num_guesses = 0
while abs(guess**2-x)>=EPSILON and guess <= x:
guess += step
num_guesses += 1
print('# of guesses =',num_guesses) # Debug statement
if guess > x:
return None
else:
return guess
##
#Bisection Method
def bisection(x:float)->float:
EPSILON = 0.01
num_guesses = 0
low = 0.0 #Window = [low,high]
high = x
guess = (low + high) / 2
while abs(guess ** 2 - x) >= EPSILON:
print('low =', low, 'high =', high, 'guess =', guess)
num_guesses += 1
if (guess ** 2 <x):
low = guess
else:
high = guess
guess = (low + high) / 2
print('# of guesses =', num_guesses)
return guess
##
#Heron's Algorithm
def heron(x:float) ->float:
EPSILON = 0.00001
guess = 1
num_guesses = 0
while abs(guess *guess -x) >EPSILON:
guess = (guess + x /guess) /2
num_guesses += 1
print('# of guesses =', num_guesses)
return guess
##
# Root Finding General problem
# Exhaustice Enumaration
def exhaustive_x3_x2_2(guess:float, max_guess:float)->float:
EPSILON: 0.01
step = EPSILON / 2
func_sol = guess **3 -guess **2 -2
while abs(func_sol) >= EPSILON and guess <= max_guess:
guess += step
func_sol = guess ** 3 - guess ** 2 - 2
if guess > max_guess:
return None
else:
return guess
#>>>exhaustive_x3_x2_2(0,10)
# 1.6949999999999859
##
# Bisection Search
import numpy
def bisection_x3_x2_2(guess_l:float,guess_h:float)->float:
EPSILON = 0.01
#Window = [guess_l,guess_h]
guess = (guess_l + guess_h) / 2
func_g_l = guess_l **3-guess_l**2-2
func_g_h = guess_h **3-guess_h**2-2
func_g = guess**3-guess**2-2
while abs(func_g) >= EPSILON:
print('low =',guess_l,'high =',guess_h,'guess =',guess)
if (numpy.sign(func_g) == numpy_sign(func_g_l)):
guess_l = guess
func_g_l = func_g
else:
guess_h = guess
func_g_h = func_g
guess = (guess_l + guess_h) / 2
func_g = guess**3 - guess**2 - 2
return guess
# Sorting Elements
# Selection Sort
def SelectionSort(A):
#Traverse through all array elements
for i in range(len(A)):
#Find the minimum element in remaining
#Unsorted array
min_idx = i
for j in range(i+1,len(A)):
if A[min_idx] > A[j]:
min_idx = j
#Swap the found minimum element with the first element
A[i],A[min_idx] = A[min_idx],A[i]
##
# Bubble Sort
def bubbleSort(arr):
n=len(arr)
#Traverse through all array elements
for i in range(n):
#Last i elements are already in place
for j in range(0,n-i-1):
#traverse the array from 0 to n-i-1
#Swap if the element found is greater than the next element
if arr[j] > arr[j+1]:
arr[j],arr[j+1] = arr[j+1],arr[j]
##
x = 'Hello'
y = 'HEllo'
if x > y:
print('Y') # Y is printed by ASCII
elif x == y:
print('N')
else:
print(None)
##
# Insertion Sort
def insertionSort(arr):
#Traverse through 1 to len(arr)
for i in range(1,len(arr)):
key = arr[i]
#Move elements of arr[0..i-1], that are greater than key, to one position ahead of their current position
j = i-1
while j>=0 and key < arr[j]:
arr[j+1] = arr[j]
j -=1
arr[j+1] = key
##
#[3,2|,5,4] -> [2,3,5|,4] -> [2,3,4,5]
##
# Merge Sort
def mergeSort(arr):
if len(arr) >1:
mid = len(arr) //2 # Finding the mid of array
L = arr[:mid] # Dividing the array elements
#into 2 halves
R = arr[mid:]
mergeSort(L) #Sorting the first half
mergeSort(R) #Sorting the second half
i=j=k=0
#Copy data to temp arrays L[] and R[]
while i<len(L) and j<len(R):
if L[i] < R[i]:
arr[k] = L[i]
i+=1
else:
arr[k] = R[j]
j+=1
k+=1
#Checking if any element was left
while i<len(L):
arr[k] = L[i]
i+=1
k+=1
while j<len(R):
arr[k] = R[j]
j+=1
k+=1
##
# Interactive Calculator
# Limited error handling
# It is to demontrate UI
'''
This is calculator.py which we are going to import to other UI file
list_of_supported_operators = ['+','*']
def add(op1:int, op2:int)->int:
return op1 + op2
def multiply(op1:int, op2:int)->int:
return op1 * op2
def execute_command(command:tuple)->int:
'''
'''
Return the result of the execution of the given command tuple in the form (operator,op1,op2)
Return None if the operator is not recognized
>>>execute_command('+',10,20)
30
>>>execute_command('*',10,20)
200
>>>execute_command('-',10,20)
None
####
operator, op1, op2 = command
if operator == '+':
return add(op1,op2)
elif operator == '*':
return multiply(op1,op2)
else:
None
'''
# Another Version
dict_operator_function = {'+':add,'*':multiply}
def execute_command(command:tuple)->int:
operator,op1,op2 = command
func = dict_operator_function.get(operator)
return func(op1,op2)
##
#UI part 1
from calculator import *
def get_command()->tuple:
'''
Prompts the user for a legal command, returning a tuple
(operator,op1,op2)
'''
operator = input('Please enter an operator (+ or *): ')
while len(operator) != 1 and operator not in list_of_supported_operators:
operator = input('Illegal Format\nPlease enter an operator (+ or *): ')
op1 = input('Please enter the first integer operand: ')
op1 = int(op1) # No error handling shown
op2 = int(input('Please enter the second operand: ')) # No error handling shown
return (operator,op1,op2)
#Main Script
# We are not checking if the user enters anything other than Y or N
more = 'Y'
while more.upper() == 'Y':
command = get_command()
result = executed_command(command)
print(command, ' = ', result)
more = input('Shall we go again? Y or N: ')
#UI is not very useful in the case of program testing
# Thats why we use batch UI
# calculator_batch.txt file is created for automated testing
'''
+ 1 2
+ 3 5
* -8 9
+ -7 9
+ 7 -9
* 1 0
'''
from calculator import *
filename = 'calculator_batch.txt'
batch_file = open(filename)
for line in batch_file:
items = line.split(' ') # Automatically creates a list of items
command = (items[0], int(items[1]), int(items[2]))
result = execute_command(command)
print(command, ' = ', result)
batch_file.close()
'''
Sample output
('+', 1, 2) = 3
('+', 3, 5) = 8
('*', -8, 9) = -72
('+',-7,9) = 2
///
'''
##
# General Interpolation
numpy.polyfit(x,y,deg)
ax+b
#Quadratic Interpolation
ax^2+bx+c
#First order - 2 data
#Seconmd order - 3 data
#...
#x 0 1
#y 2 3
import numpy as np
x = [0,1] #or range(1,10)
y = [2,3]
z = np.polyfit(x,y,1)
print(z)
#y = ax+b
#x 0 1 2
#y 2 3 2
x2 = [0,1,2]
y2= [2,3,2]
z2 = np.polyfit(x,y,2)
print(z2)
#Regression
'''
#x y
#1 5.47
#2 7.08
#3 8.36
///
'''
import numpy as np
import matplotlib.pyplot as plt
x = np.linspace(1,3,3)
y = [5.47,7.08,8.36]
plt.scatter(x,y)
plt.show()