-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlist_mysolutions.py
More file actions
1012 lines (817 loc) · 16.5 KB
/
Copy pathlist_mysolutions.py
File metadata and controls
1012 lines (817 loc) · 16.5 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
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
############# I TRIED UPTO 91 QUESTIONS AND I DIDNT DO THE 58,66,76,82,88 AND 91-100
# BECAUSE THEY SEEMS TOO DIFFICULT AWY FROM MY UNDERSTANDING
# AS I MATCHED MT RESULTS TO SOLUTIONS I FOUND THAT I CAN MAKE THEM SHORT AND WITH RETURN STATEMENT
# AND PASS DIIF AGRUNMENTS
# IT WAS MY FIRST TIME DOING IT SOOOOOO
# BUT I AM SATISFIED AND WILL DO OTHER AT MY LEVEL UP I LEARNED A LOT NEW :)
# 1
def que1():
a=list((1,2,3,4,5))
print(a)
#que1()
#2◍
def que2():
a=[10, 20, 30, 40, 50]#given
print(a[2])#for third element
#que2()
#3
def que3():
a=['apple', 'banana', 'cherry', 'date']
print(len(a))#prints length of a
# que3()
#4
def que4():
a=['apple', 'banana', 'cherry']
b="grape"
a.append(b)
print(a)
# que4()
#5
def que5():
a=[10, 20, 30, 40]
b=25
a.insert(2,b)
print(a)
# que5()
#6 ◍
def que6():
a=['apple', 'banana', 'cherry', 'banana']
a.remove("banana")
print(a)
# que6()
#7 ◍
def que7():
a=[5, 10, 15, 20]
a.pop(1)
print(a)
#removes element by index
# que7()
#8
def que8():
a=['apple', 'banana', 'cherry']
print('orange' in a)
# que8()
#9◍ can give value error if not present
def que9():
a=['apple', 'banana', 'cherry', 'date']
print(a.index("cherry"))
#que9()
#10
def que10():
a=['apple', 'banana', 'apple', 'cherry']
c=0
for i in a:
if i=="apple":
c+=1
print(c)
#or
print(a.count("apple"))
# que10()
#11
def que11():
a=[0, 1, 2, 3, 4, 5]
print(a[1:4])
# que11()
#12◍ can give indexerror if l=[]
def que12():
a=[10, 20, 30, 40, 50]
print(a[-1])
# que12()
#13◍ can give indexerror if not valid
def que13():
a=['a', 'b', 'c', 'd']
a[2]="NEW"
print(a)
# que13()
#14
def que14():
a=[1, 2, 3]
b=[4,5,6]
a.extend(b)
print(a)
# que14()
#15
def que15():
a=[1, 2, 3, 4, 5]
a.clear()
print(a)
# que15()
#16
def que16():
a=[1, 2, 3, 4]
b=a.copy()
print(b)
print(id(a),id(b))
# que16()
#17
def que17():
a=[1,2]
b=[3,4]
print(a+b)
# que17()
#18(
def que18():
a=[1,2]
print(a*3)
# que18()
#19◍ or can do l==0
def que19():
a=[]
b=[1,3]
print(a == [])
print(b == [])
# que19()
#20◍
def que20():
a=[45, 12, 78, 23, 67]
b=[]
print(min(a))
print(min(b))#ValueError as b is empty
#return min(lst) if lst else None
# this condition will check whether l is empty then
#return none
# que20()
#21
def que21():
a=[45, 12, 78, 23, 67]
b=[]
print(max(a))
print(max(b))#ValueError
# que21()
#22
def que22():
a=[1, 2, 3, 4, 5]
print(sum(a))#no errors except when non iterable
# que22()
#◍my map version is fast for big data
#23
def que23():
l=[1,2,3,4,5]
a=map(lambda x:x**2,l)
print(list(a))
# que23()
#24
def que24():
a=[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
b=[]
for i in a:
if i%2==0:
b.append(i)
print(b)
# que24()
#25
def que25():
a=[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
b=[]
for i in a:
if i%2!=0:
b.append(i)
print(b)
# que25()
#26
def que26():
a=['red', 'green', 'blue']
for i in a:
print(i)
# que26()
#27
def que27():
a=['hello', 'world', 'python']
b=[]
c=[]
for i in a:
b.append(len(i))
print(b)
#OR
print(list(map(lambda x:len(x),a)))
# que27()
#28◍i should take in account the error possibility
def que28():
a=[10, 20, 30, 40, 50]
b=[]
b.append(a[0])
b.append(a[-1])
print(b)
# que28()
#29◍ i can check whether a list is empty or not by- if l:
def que29():
a=[1, 2, 3, 4, 5]
print(a.pop(),",",a)
# que29()
#30
def que30():
a=[0, 1, 2, 3, 4, 5]
print(a[3:0:-1])
# que30()
#31
def que31():
a=[2, 4, 6, 8]
print(all(i%2==0 for i in a))#all check here a condition it has a generator condition that is useful like lambda
# que31()
#32
def que32():
a=[1, 3, 5, 8]
print(any(i%2==0 for i in a))
# que32()
#33
def que33():
a=['old', 'car', 'old', 'house']
for i in range(len(a)):
if a[i]=="old":
a[i]="new"
print(a)
# que33()
#34◍
def que34():
a=[]
for i in range(1,6):
a.append(i*3)
print(a)
# que34()
#35◍
def que35():
a=[10, 20, 30, 40, 50]
print(sum(a)/len(a))
# que35()
#36
def que36():
a=[1, 2, 3, 4, 5]
b=[]
for i in a:
b.append(str(i))
print(b)
# que36()
#37
def que37():
a='hello'
print(list(a))
# que37()
#38
def que38():
a=['apple', 'banana', 'cherry']
#using th e join() as syntax "".join(iterable)
print(",".join(a))# give output as str
# que38()
#39
def que39():
a="one,two,three,four"
b=a.split(",")
print(b)
# que39()
#40
def que40():
a= [1, 2, 3, 4, 5]
###################### Instance use i dont studied yet
if all(isinstance(x, int) for x in a):
print("TRUE")
#bool is a subclasss of int
# que40()
#41
def que41():
a=[64, 34, 25, 12, 22, 11, 90]
print(a.sort())#returns none sort tht list in place
print(a)
# que41()
#42
def que42():
a=[64, 34, 25, 12, 22, 11, 90]
print(a.sort(reverse=True))#returns none
print(a)
# que42()
#43
def que43():
a=[1, 2, 3, 4, 5]
print(a.reverse())#returns none
print(a)
#reverse in place
#que43()
#44
def que44():
print(list(i**2 for i in range(1,11)))
# que44()
#45
def que45():
n=10
print(list(i**2 for i in range(1, n+1) if i % 2 == 0))#new topic syntax as ( <to print or output> for loop <condition> )
# que45()
#46
def que46():
a=[[1, 2, 3], [4, 5, 6], [7, 8, 9]]
b=a[1][2]
print(b)
# que46()
#47
def que47():
a=[[1, 2, 3], [4, 5, 6], [7, 8, 9]]
a[1][1]=50
print(a)
# que47()
#48
def que48():
a=[1, 2, 2, 3, 4, 4, 5]
print(a)
b=set(a)
print(list(b))
# que48/
#49 should put condition for indexerror
def que49():
a=[45, 12, 78, 23, 67, 89, 34]
a.sort()
print(a[-2])
# que49/
#50
def que50():
a=[45, 12, 78, 23, 67, 89, 34]
a.sort()
print(a[-2])
# que50/
#51
def que51():
a=[1, 2, 3, 4]
b=[3, 4, 5, 6]
print(list(set(a)&set(b)))#first convert list to set aand thenconvert them back
# que51()
#52
def que52():
a=[1, 2, 3, 4, 5]
b=[3, 4, 5, 6, 7]
print(list(set(a)-set(b)))#first convert list to set aand thenconvert them back
#que52()
#53
def que53():
l=[1,2,3,4,5]
position=2
position=position%len(l)
print(l[position:]+l[:position])
#que53()
#54
def que54():
l=[1,2,3,4,5]
n=2
position=len(l)-n
position=position%len(l)
print(l[position:]+l[:position])
#que54()
#55
def que55():
l=[1,2,3,4, 5,6,7,8,9,10]
n=3
a=[]
for i in range(0,len(l),n):
a.append(l[i:i+n])
print(a)
#que55()
#56
def que56():
a=['cat', 'elephant', 'dog', 'python', 'ai']
b=[]
for i in a:
if len(i)>4:
b.append(i)
print(b)
#or filter
print(list((filter(lambda x:len(x)>4,a))))
#que56()
#57◍
def que57():
a=[]
for i in range(1,21):
if i%2==0 and i%3==0:
a.append(i)
print(a)
#que57/
#58
def que58():
a=["a","b","c","d"]
b=[]
for i in enumerate(a):
b.append(i)
print(b)
#que58/
#59
def que59():
a=['name', 'age', 'city']
b=['John', 25, 'NYC']
print(zip(a,b))#give zip object
print(list(zip(a,b)))
#que59/
#60
def que60():#
a = ['name', 'age']
b = [['John', 25], ['Jane', 30]]
c = []
for i in b:
d={}
for j in range(len(a)):
d[a[j]]=i[j]
c.append(d)
print(c)
#or by zip
d=[]
for i in b:
d.append(dict(zip(a,i)))
print(d)
# or list Comprehension
d = [dict(zip(a, i)) for i in b]
print(d)
# or generator obj
d = (dict(zip(a, i)) for i in b) # This is a generator, NOT a list
print(list(d)) # You must convert to list if you want to print all at once ,doesnt store all value at once it just genreate vxalue on call,
#but converted into list so then work same as above
# que60()
#61
def que61():# took me over 15 min and 5 min chatgpt session
a=['apple', 'banana', 'cherry', 'apricot',]
b={}
for i in range(len(a)):
g=[]
for j in a:
if a[i][0].lower()==j[0].lower():
g.append(j)
b[a[i][0]]=g
print(b)
# what i was doing previously was resetiing g evertime
# a = ['apple', 'banana', 'cherry', 'apricot']
# b = {}
# for i in range(len(a)):
# for j in a:
# g = []
# if a[i][0].lower() == j[0].lower():
# g.append(j)
# b[a[i][0]] = g
# que61()
#62 enamurate can also be used
def que62():
a=['a', 'b', 'a', 'c', 'a']
b=[]
for i in range(len(a)):
if a[i]=='a':
b.append(i)
print(b)
# with generator
d=(i for i in range(len(a)) if a[i]=='a')#give the generator obj
# <...> for loop <condition>
#... will be saved in d
print(list(d))
#with list Comprehension
print([i for i in range(len(a)) if a[i]=='a']) # no need to cone=vert ass it returns a list
# que62()
#63
def que63():
a=[1,3]
b=['a', 'b', 'c', 'd', 'e']
c="X"
for i in a:
b[i]=c
print(b)
# in generator
#print(list(()))
# generator or list compression caant do becatuse <...> creates new object not bring change in old one
# que63()
#64
def que64():
a=[1, 2, 3, 2, 1]
# print(a==a.reverse())
#wrong as it returns none it modifies it in place what i was doing use reversed instead but slicing preffered
print(a==list(reversed(a)))
#or
print(a==a[::-1])
# que64()
#65
def que65():
a=[[1, 2, 3], [4, 5, 6]]
b=list(zip(a[0],a[1]))
print(b)
#* is a unpacking operator
# list(zip(*matrix))
#[[1, 2, 3],
# [4, 5, 6]]
# to
# [[1, 4],
# [2, 5],
# [3, 6]]
# is transpose
# que65()
#66
def que66():
a=[1,2,3,]
b=[4,5,6,]
#1st
print(sorted(a+b))
#2nd
a.extend(b)
a.sort()
print(a)
#3rd
# its pretty complcated under the hood version **SKIPs
# que66()
#67
def que67():
a= [1, 2, 3, 4, 5]
b=[]
sum=0
for i in a:
sum+=i
b.append(sum)
print(b)
# que67()
#68
def que68():#from chatgpt
a = [3, 1, 4, 1, 5, 9, 2]
c = []
max_val = float('-inf') # Initialize with the lowest possible value
for i in a:
max_val = max(max_val, i) # update max
c.append(max_val)
print(c)
# que68()
#69
def que69():
a=[1, 2, 2, 3, 3, 3, 4]
b={}
for i in a:
b[i]= a.count(i)
print(b)
#with generator
print(dict(((i,a.count(i)) for i in a )))
# que69()
#70
def que70():
a=[1, 6, 2, 8, 3, 9, 4]
for i in a:
if i>5:
a.remove(i)
print(a)
#filte()
a=[1, 6, 2, 8, 3, 9, 4]
print(list((filter(lambda x : x<5 , a))))
#generator
a=[1, 6, 2, 8, 3, 9, 4]
print(list((i for i in a if i<5)))
#list compression
print([i for i in a if i<5])
# que70()
#71
def que71():
a=[1, 2, 3, 4, 5, 6, 7]
n=3
b=[]
for i in range(len(a) - n + 1):# 5 combination to get 3 window
x=0
for j in range(i,i+n):
x+=a[j]
b.append(x/3)
print(b)
# que71()
#72
def que72():
#it means Take one element from a, then one from b, then the next from a, then next from b, and so on...
a=[1,3,5]
b=[2,4,6]
c=[]
for i,j in zip(a,b):
c.extend([i,j])
print(c)
# que72()
#73
def que73():
a=[1, 2, 3, 4, 5, 6]
e=[]
o=[]
for i in a:
if i%2==0:
e.append(i)
else:
o.append(i)
print((e,o))
# que73()
#74
def que74():
a = [1, 2, 4, 5, 6]
for i in range(a[0], a[-1] + 1):
if i not in a:
print(i)
#or use sum method
#que74()
#75
def que75():
#not the actual method slow for big list , binary ia fast but only works on sorted lists ascending or descending
a=[1,3,5,7,9,11]
n=7
for i in range(len(a)):
if a[i]==n:
print(i)
#actual binary method
a=[1,3,5,7,9,11]
n=7
low=0
high=len(a)-1
is_asc=a[low]<a[high]
while low <= high:
mid = (low + high) // 2
if a[mid] == n:
print(mid)
break
if is_asc:
if a[mid] < n:
low = mid + 1
else:
high = mid - 1
else:
if a[mid] > n:
low = mid + 1
else:
high = mid - 1
# que75()
#76
# def que76():
# a=[10, 9, 2, 5, 3, 7, 101, 18]
# dont know out off brain
#77
def que77():
#not recommended as it will create a bug as list is continously changing it
# #if more than 2 the 2s wiill not be removed
a=[1, 2, 3, 4, 5, 2, 4, 6]
b=[2,4]
for i in b:
for j in a:
if i==j:
a.remove(i)
print(a)
#list comresion , creating another one
a = [1, 2, 3, 4, 5, 2, 4, 6]
b = [2, 4]
a = [x for x in a if x not in b]
print(a)
#filter()
a = [1, 2, 3, 4, 5, 2, 4, 6]
b = [2, 4]
result = list(filter(lambda x: x not in b, a))
print(result)
#if b is large for speeding it up
a = [1, 2, 3, 4, 5, 2, 4, 6]
b = set([2, 4])
a = [x for x in a if x not in b]
print(a)
#set is faster than list for lookups using in because it's an unindexed, unordered data type that uses a hash table for constant-time access.
#Python doesn't search through each item like list It hashes <whatever> and jumps straight to the memory slot where it should be
# que77()
#78
def que78():
a=[1, 3, -1, -3, 5, 3, 6, 7]
n=3
b=[]
for i in range(len(a)-n+1):
e=[]
for j in range(i,i+n):
e.append(a[j])
b.append(max(e))
#b.append(max(a[i:i+n]))
#for directly slice
print(b)
# que78()
#79
def que79():
a=[1, 2, 3, 4]
b=[1, 3, 3, 5]
c=[]
for i,j in zip(a,b):
c.append(i == j)
print(c)
# othr
print(list((x==y for x,y in zip(a,b))))
print([x==y for x,y in zip(a,b)])
# que79()
#80
def que80():
a=[1, 2, 3]
b=[3, 1, 2]
# the both has same datatype
a.sort()
b.sort()
print(a==b)
# que80()
#81
def que81():
a=[[1, 2], [3, 4, 5], [6]]
b=[]
for i in a:
for j in i:
b.append(j)
print(b)
# que81()
#82◍
# def que82():
# a= [1, [2, 3, [4, 5]], 6]
#83
def que83():
#for small lists
a=[3, 1, 4, 1, 5, 9, 2, 6, 5]
b=[]
for i in a:
if i not in b:
b.append(i)
print(b)
# if large data for faster
a = [3, 1, 4, 1, 5, 9, 2, 6, 5]
seen = set()
b = []
for i in a:
if i not in seen: # O(1) check
b.append(i)
seen.add(i)
print(b)
# que83()
#84
def que84():
a=[1, 2]
b=['a', 'b']
c=[]
for i in a:
for j in b:
c.append((i,j))
print(c)
#list compression
d = [(i, j) for i in a for j in b]
print(d)
# que84()
#85
def que85():
#all cartesian pairs
a=[1, 2, 3, 4]
r=2
c=[]
for i in a:
for j in a:
c.append((i,j))
print(c)
#all cartesian pairs excluding (i,i) permmutation
a=[1, 2, 3, 4]
r=2
c=[]
for i in a:
for j in a:
if i!=j:
c.append((i,j))
print(c)
# # cartesian product combination
a = [1, 2, 3, 4]
c = []
for i in range(len(a)):
for j in range(i + 1, len(a)):
c.append((a[i], a[j]))
print(c)
# que85()
# #86
# def que86():
# def permutations(lst):
# def helper(path, used):
# if len(path) == len(lst):
# result.append(tuple(path))
# return
# for i in lst:
# if i not in used:
# helper(path + [i], used | {i})
# result = []
# helper([], set())
# return result
# # Test
# print(permutations([1, 2, 3]))
#87
def que87():
#not > and > or
# if multiple L to R
a,b=2,3
n=20
c=[]
for i in range(1,n+1):
if (i%2==0 or i%3==0) and i>5:
c.append(i)
print(c)
# que87()
#88
# def que88():
#89
def que89():
a=[1, 2, 3, 2, 4, 5, 1, 6, 7, 3]
seen=set()
duplicate=set()
for i in a:
if i in seen:
duplicate.add(i)
else:
seen.add(i)
print(list(duplicate))
#que89()
#90
def que90():
a=[1, 2, 3]