-
Notifications
You must be signed in to change notification settings - Fork 0
/
mypyton.py
1523 lines (1189 loc) · 37.7 KB
/
mypyton.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
#work with command line argument
import sys
address = ' '.join(sys.argv[1:])
#Continue
#how many numbers are missing to set the array continuous
def Consecutive(arr):
#magic solution in one line
print (max(arr)-min(arr)-len(arr)+1,"!!!!")
arr.sort()
compl=1
i=0
while i<len(arr)-1:
n=arr[i]
while i<len(arr) and n<arr[i+1]:
compl+=1
n+=1
i+=1
return(compl-len(arr))
print ("=========",Consecutive([6,20,10,18]),"=====")
#returns the mode popular item in a list (array)
def SimpleMode(arr):
return max(arr, key=arr.count)
print (SimpleMode([10, 4, 5, 2, 4,10,10,4,4,4,10,6]) )
#return the first word with the greatest number of repeated letters.
def LetterCount1(str):
maxC = 0
maxWord = ""
for word in str.split():
count = 0
for i in range(len(word)):
if word[i] in word[i+1:]:
count += 1
if (count > maxC):
maxC = count
maxWord = word
if maxC == 0:
return -1
else:
return maxWord
print (LetterCount1("reeepeatd is a nice coununttrryy with mannny reeppeatd lakes!"))
# returns the first word with the greatest number of repeated letters
def LetterCount(str):
def maxcharrepeats(word): # Canada - 3
newword=sorted(word)
i=1
maxlength=1
while i<len(newword)-1 and newword[i].isalpha():
currentlength=1
while i<len(newword)-1 and newword[i].isalpha() and newword[i]==newword[i+1]:
currentlength+=1
i+=1
if maxlength<currentlength:
maxlength=currentlength
i+=1
if maxlength==1:
return -1
return maxlength
maxlength=1
maxword=""
currentword=""
newarry=str.split(" ")
for word in newarry:
wordlength=maxcharrepeats(word)
if maxlength<wordlength:
maxlength=wordlength
maxword=word
return (maxlength,maxword)
print (LetterCount("Canada is a nice country with many repeated lakes!"))
def BinaryConverter(str1):
return(int(str(str1),2))
print(BinaryConverter(100101))
import itertools
def ArrayAdditionI1(arr):
maxn=max(arr)
arr.remove(maxn)
for n in range(2,len(arr)):
for i in (itertools.combinations(arr,n)):
if sum(i)==maxn:
return i
return -1
print (ArrayAdditionI1([7,3,5,10,2,-5]))
#Returns if array is Arithmetic, Geometric or none (-1)
def ArithGeoII(arr):
diffart=arr[0]-arr[1]
diffmul=arr[0]/arr[1]
i=0
art=True
mul=True
for i in range(0,len(arr)-1):
if diffart!=arr[i]-arr[i+1]:
art=False
if diffmul!=arr[i]/arr[i+1]:
mul=False
if art:
return ("Arithmetic")
if mul:
return "Geometric"
return -1
print(ArithGeoII([4,14,24,34,44]))
#checks if string is substring of bigger string
def StringScramble(str1,str2):
for c in str2:
if not c in str1:
return False
return True
print (StringScramble("helpmaAto","hpAlmot"))
#returns greatest common factor
from fractions import gcd
def Division(num1,num2):
print (gcd(num1,num2))
i=min(int(num1/2),int(num2/2))
while i>1:
if num1%i==0 and num2%i==0:
return i
i-=1
return 1
print (Division(216,448))
#returns true if the string is a palindrome (skipping non-characters)
def PalindromeTwo(str):
newstr=("".join(i.lower() if i.isalpha() else "" for i in str))
return newstr[::-1]==newstr
print(PalindromeTwo("Q1Noel - sees Leon1!!!!Q1726381W"))
#prints how much each char is presented in string
def RunLength(str1):
print(str1)
i=0
newstr=""
while i<len(str1):
counter=0
n=i
while i<len(str1) and str1[i]==str1[n]:
i+=1
counter+=1
newstr+=str(counter)+str1[n]
return(newstr)
print(RunLength("aabbcde"))
#checks if a number is prime
from math import sqrt; from itertools import count, islice
def PrimeTime(n):
if n < 2: return False
return all(n%i for i in islice(count(2), int(sqrt(n)-1)))
#How many times to cycle summarizing number's digits to get less then 10
def AdditivePersistence(num):
def sum_digits(n):
t=n
sum=0
while t>0:
sum+=t%10
t=int(t/10)
return sum
if num<10:
return(0)
AdditiveCounter=1 # total count of cycles
currentsum=sum_digits(num)
while currentsum>9:
currentsum=sum_digits(currentsum)
AdditiveCounter+=1
return(AdditiveCounter)
print(AdditivePersistence(99999999999999999999999999999999999999999999999999999999999999999))
# Have the function ArrayAdditionI(arr) take the array of numbers stored in arr and return the string true if any combination of numbers in the array can be added up to equal the largest number in the array, otherwise return the string false. For example: if arr contains [4, 6, 23, 10, 1, 3] the output should return true because 4 + 6 + 10 + 3 = 23. The array will not be empty, will not contain all the same elements, and may contain negative numbers.
import itertools
def ArrayAdditionI(arr):
maxN = max(arr)
for i in range(2, len(arr)):
for item in itertools.combinations(arr,i):
if sum(item)==maxN:
return "true"
return "false"
print (ArrayAdditionI([32,10,40,5,10,1,3,5,73]))
#Check if number is power of 2
def PowersofTwo(num):
return math.log(num,2)==math.floor(round(math.log(num,2)))
print (PowersofTwo(8192))
#print 3rd longest string in array
def ThirdGreatest(strArr):
print (strArr)
return sorted([(word, -len(word)) for word in strArr], key=lambda x: x[1])[2][0]
ss=[ "hello", "world", "after1", "all"]
print (ThirdGreatest(ss))
#Swap case of characters in string
def SwapCase(str):
#one line solution:
#return "".join([i.lower() if i.isupper() else i.upper() for i in str])
print (str)
news=""
for c in str:
if c.lower()==c:
news+=c.upper()
elif c.upper()==c:
news+=c.lower()
else:
news+=c
return news
print (SwapCase("MarkISl93weR"))
#Sum numbers in string
def NumberAddition(str):
def isnumber(n):
if n>='0' and n<='9':
return True
return False
print (str)
i=0
totalsum=0
while i<len(str):
tnum=0
needincreament=1
while i<len(str) and isnumber(str[i]):
if isnumber(str[i]):
tnum=tnum*10+int(str[i])
i+=1
needincreament=0
if needincreament==1:
i+=1
if tnum: print(tnum,"+")
totalsum+=tnum
return totalsum
print (NumberAddition("Ma45k0120er34tj200s991"))
#insert dashes ('-') between each two odd numbers in str
def DashInsert(str):
def odd(n):
return (int(n)%2==1)
print("checking ",str)
newstr=""
i=0
while i<len(str):
if odd(str[i]) and odd(str[i-1]) and i>0:
newstr+="-"+str[i]
i+=1
if i<len(str):
if odd(str[i]) and odd(str[i-1]) and i>0:
newstr+="-"
newstr+=str[i]
i+=1
return(newstr)
print (DashInsert("777773055544448899281274727777"))
#count number of instances of each character in a string
sentence="This is a nice long sentence with many words"
count={}
for char in sentence:
count.setdefault(char,0) # if we don't have char, it will be added and 0 instances
count[char]+=1 # the previous command set the instance to 0 if this is the first time. now, number of instances is raised by 1;
# so either it will be 1, or increased by 1.
print (count)
#variables
x3=7
def fun01():
print (x3)
#global x3 # by default, Python will scope variable inside block. adding "global" tells to use the global variable x3
#x3=8 # it will take the global x3 and assign to it 8
#x3=x3+5 #now global x3 equels 13
return x3 #returns 13
print ("before fun01",x3) # will be 7
print ("after fun01",fun01()) # will be 13
print ("x3=",x3) # will be 13
exit ()
import calendar
cal = calendar.month(2008, 1)
print ("Here is the calendar:")
print (cal)
exit()
import math # generic import
# from math import * // univeral import, so you don't need to add .math prefix to functions
# from math import sqrt // Import *just* the sqrt function from math
from datetime import datetime
#range is exclusive and starts at 0 by default
for i in range(20): # will print 0..19
print (i,)
#creates list of even numbers
evens_to_50 = [i for i in range(51) if i % 2 == 0]
print (evens_to_50)
#Multiple Assignment
a, b, c = 1, 2, "john"
a = b = c = 1
print ("This is my first " + "Pyton" + " program")
x=5
print (x)
# this is a comment
fifth_letter = "MONTY"[4]
print (fifth_letter)
mystring="hello world"
print (len(mystring))
print (mystring.lower())
print (mystring.upper())
print ("dsfasfas"[-1]) # prints last character in string
monty=True
python=1.234
monty_python=python**2
print (monty_python)
meal = 44.50
tax = 0.0675
tip = 0.15
total=(1+tip)*(meal*(1+tax))
print("%.2f" % total)
string_1 = "Camelot"
string_2 = "place"
print ("Let\'s not go to %s. \'Tis a silly %s." % (string_1, string_2))
now=datetime.now()
print ("The date is:" + str(now.day) + "/" +str(now.month) +"/"+ str(now.year))
print ('The date is: %s/%s/%s' % (now.day, now.month, now.year))
print ('The time is: %s:%s:%s' % (now.hour, now.minute, now.second))
def using_control_once():
if 5==5:
return "Success #1"
def using_control_again():
if 5**2>4:
return "Success #2"
print (using_control_once())
print (using_control_again())
answer = "This but a scratch!"
def black_knight():
if answer == "This but a scratch!":
return True
else:
return False # Make sure this returns False
def french_soldier():
if answer == "Go away, or I shall taunt you a second time!":
return True
else:
return False # Make sure this returns False
def greater_less_equal_5(answer):
"""
This is a multi-line remark and it describes the function
"""
if (answer>5):
return 1
elif (answer<5):
return -1
else:
return 0
print (greater_less_equal_5(4))
print (greater_less_equal_5(5))
print (greater_less_equal_5(6))
pyg = 'ay'
original = input('Enter a word:')
if len(original) > 0 and original.isalpha():
print (original)
word=original.lower()
first=word[0]
new_word=word+first+pyg
new_word=new_word[1:len(new_word)]
print (new_word)
else:
print ('empty')
allfunctionsfrommath = dir(math) # Sets everything to a list of things from math module
print (allfunctionsfrommath) # Prints 'em all!
# def biggest_number(*args): // Function with unkown number of parameters
# biggest_number(-10, -5, 5, 10)
zoo_animals = ["pangolin", "cassowary", "sloth", "dog"]; #Arrays (they call it lists)
if len(zoo_animals) > 3:
print ("The first animal at the zoo is the " + zoo_animals[0])
print ("The second animal at the zoo is the " + zoo_animals[1])
print ("The third animal at the zoo is the " + zoo_animals[2])
print ("The fourth animal at the zoo is the " + zoo_animals[3])
zoo_animals.append("cat") # adding "cat" as the last field
print (zoo_animals[1:4]) # will print fields on index from 1 to 3
print (len(zoo_animals))
#run through list
my_list = [1,9,3,8,5,7]
for number in my_list:
# Your code here
print (number*2)
#list slicing
suitcase = ["sunglasses", "hat", "passport", "laptop", "suit", "shoes"]
first = suitcase[0:2] # The first and second items (index zero and one)
middle = suitcase[2:4] # Third and fourth items (index two and three)
last = suitcase[4:16] # The last two items (index four and five)
# split list
ss="var01=mark;var01=moshe;var03=david;var04=arba;var05=five"
newss=ss.split(";")
for n in newss:
(key, value ) = n.split('=')
first=key
second=value
print( first, ";",second )
#search from string inside other string - recursively
def locations_of_substring(sequence,item):
"""Return a list of locations of a substring."""
newstrsequence = ''.join(sequence)
newitem = ''.join(item)
substring_length = len(newitem)
def recurse(locations_found, start):
location = newstrsequence.find(newitem, start)
if location != -1:
return recurse(locations_found + [location], location+substring_length)
else:
return locations_found
return recurse([], 0)
print(locations_of_substring(["1","2","1","1","4","1","1","5","4","6","1","1","1","3","6","5","1","1","1","2"],["1","1"]))
start_list = [5, 3, 1, 2, 4]
square_list = []
for index in start_list:
square_list.append(index**2)
square_list.sort()
print (square_list)
#Merging two lists into dictionary
list01 = ["Chess","Football","Swimming","Running","Cycling"]
list02=["Dan","Yosi","Julia","Charles","Rafi"]
dict01={}
for i in range (len(list01)):
dict01[list01[i]]=list02[i]
print (dict01)
# Assigning a dictionary with three key-value pairs to residents:
residents = {'Puffin' : 104, 'Sloth' : 105, 'Burmese Python' : 106}
print (residents['Puffin']) # Prints Puffin's room number
print (residents['Sloth'])
print (residents['Burmese Python'])
print (residents.keys()) # prints all dictionary keys
print (residents.values()) # prints all dictionary values
menu = {} # Empty dictionary
menu['Chicken Alfredo'] = 14.50 # Adding new key-value pair
print (menu['Chicken Alfredo'])
# Your code here: Add some dish-price pairs to menu!
menu['bobo']=5.5
menu['number']=12
menu['float']=5.2
print ("There are " + str(len(menu)) + " items on the menu.")
print (menu)
#remove entry from list
backpack = ['xylophone', 'dagger', 'tent', 'bread loaf']
backpack.remove("dagger")
# key - animal_name : value - location
zoo_animals = { 'Unicorn' : 'Cotton Candy House',
'Sloth' : 'Rainforest Exhibit',
'Bengal Tiger' : 'Jungle House',
'Atlantic Puffin' : 'Arctic Exhibit',
'Rockhopper Penguin' : 'Arctic Exhibit'}
# A dictionary (or list) declaration may break across multiple lines
# Removing the 'Unicorn' entry. (Unicorns are incredibly expensive.)
del zoo_animals['Unicorn']
del zoo_animals["Sloth"]
del zoo_animals["Bengal Tiger"]
zoo_animals["Rockhopper Penguin"]="new value"
print (zoo_animals)
inventory = {
'gold' : 500,
'pouch' : ['flint', 'twine', 'gemstone'], # Assigned a new list to 'pouch' key
'backpack' : ['xylophone','dagger', 'bedroll','bread loaf']
}
# Adding a key 'burlap bag' and assigning a list to it
inventory['burlap bag'] = ['apple', 'small ruby', 'three-toed sloth']
# Sorting the list found under the key 'pouch'
inventory['pouch'].sort()
inventory["pocket"]=['seashell', 'strange berry', 'lint']
inventory["backpack"].sort()
inventory["backpack"].remove('dagger')
inventory["gold"]=inventory["gold"]+50
#run through dictionary
webster = {
"Aardvark" : "A star of a popular children's cartoon show.",
"Baa" : "The sound a goat makes.",
"Carpet": "Goes on the floor.",
"Dab": "A small amount."
}
for word01 in webster:
print (webster[word01])
#built in functions to go through dictionary
my_dict ={
"drink":"coffee",
"color":"black",
"size":"medium",
"Weight":45
}
print (my_dict.items())
print (my_dict.keys())
print (my_dict.values())
a = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]
for i in a:
if (i%2==0):
print (i)
a.pop(4) # will remove and return the 5th item (4)
a.remove(5) # will remove any instances of 4
del(a[10]) # removes the item in field #11 (10) but doesn't return it
# Merge two lists into dictionary (manually)
var1 = [1,2,3,4]
var2 = [5,6,7]
d = {1:var1, 2:var2}
#manipulate list and convert it to string
garbled = "!XeXgXaXsXsXeXmX XtXeXrXcXeXsX XeXhXtX XmXaX XI"
print (garbled[::-2]) #will print every second character, backwards
print (''.join(list(filter(lambda x:not x=="X",garbled[::-1])))) # going backwards and filtering 'X'
#Use a list comprehension to create a list, threes_and_fives, that consists only of the numbers between 1 and 15 (inclusive) that are evenly divisible by 3 or 5.
threes_and_fives=list(filter(lambda x: x % 3 == 0 or x%5==0, range(1,16)))
# create new list to include only unique entries from current list
def remove_duplicates(nums):
newarry=[]
for i in nums:
exists=0
n=0
for n in range(len(newarry)):
if (i==newarry[n]):
exists=1
if exists==0:
newarry.append(i)
return newarry
print (remove_duplicates([3,2,1,3,2,2,3,5,6,7,1,1,1,9]))
def fizz_count(x):
count=0
for ind in x:
if (ind=="fizz"):
count=count+1
return count
print (fizz_count(["fizz","cat","fizz"]))
#String looping
for letter in "Codecademy":
print (letter)
# Empty lines to make the output pretty
print
print
word = "Programming is fun!"
for letter in word:
# Only print out the letter i
if letter == "i":
print (letter)
prices={
"banana": 4,
"apple": 2,
"orange": 1.5,
"pear": 3
}
stock={
"banana": 6,
"apple": 0,
"orange": 32,
"pear": 15
}
for ind in prices:
print ("%s " % ind)
print ("price: %s" % prices[ind])
print ("stock: %s" % stock[ind])
animal_sounds = {
"bear": "John",
"cat": ["meow", "purr"],
"dog": ["woof", "bark"],
"quizzes": [88.0, 40.0, 94.0],
"fox": [],
}
print (animal_sounds["cat"])
lloyd = {
"name": "Lloyd",
"homework": [90.0, 97.0, 75.0, 92.0],
"quizzes": [88.0, 40.0, 94.0],
"tests": [75.0, 90.0]
}
alice = {
"name": "Alice",
"homework": [100.0, 92.0, 98.0, 100.0],
"quizzes": [82.0, 83.0, 91.0],
"tests": [89.0, 97.0]
}
tyler = {
"name": "Tyler",
"homework": [0.0, 87.0, 75.0, 22.0],
"quizzes": [0.0, 75.0, 78.0],
"tests": [100.0, 100.0]
}
students=[lloyd, alice, tyler]
for ind in students:
print (ind["name"])
print (ind["homework"])
print (ind["quizzes"])
print (ind["tests"])
#Function to calculate average of array
def average(numbers):
total=sum(numbers)
total=float(total)
return total/len(numbers)
#Manipulate list
def double_first(n):
n[0] = n[0] * 2
numbers = [1, 2, 3, 4]
double_first(numbers)
print (numbers)
#Run through list items
n = [3, 5, 7]
for i in range(0, len(n)):
n[i] = n[i] * 2
# Tuples are like lists, but read only; defined via ( but accessed as list, by [
tuple = ( 'abcd', 786 , 2.23, 'john', 70.2 )
print (tuple[1:3]) # Will print (786, 2.23) ---> places #1,#2
"""
manipulating list
range(6) # => [0,1,2,3,4,5]
range(1,6) # => [1,2,3,4,5]
range(1,6,3) # => [1,4]
range(0,3,1) ==> 0,1,2
n = len(L)
item = L[index]
seq = L[start:stop]
seq = L[start:stop:step]
seq = L[::2] # get every other item, starting with the first
seq = L[1::2] # get every other item, starting with the second
If you need both the index and the item, use the enumerate function:
for index, item in enumerate(L):
print index, item
str = 'Hello World!'
print str # Prints complete string
print str[0] # Prints first character of the string
print str[2:5] # Prints characters starting from 3rd to 5th
print str[2:] # Prints string starting from 3rd character
print str * 2 # Prints string two times
print str + "TEST" # Prints concatenated string
"""
"""
List excerise
-------------
Create a list, to_21, that's just the numbers from 1 to 21, inclusive.
Create a second list, odds, that contains only the odd numbers in the to_21 list (1, 3, 5, and so on). Use list slicing for this one instead of a list comprehension.
Finally, create a third list, middle_third, that's equal to the middle third of to_21, from 8 to 14, inclusive.
"""
to_21=[i for i in range(1,22)]
odds=to_21[0::2]
middle_third=to_21[7:14]
#reverse list
my_list = range(1, 11)
backwards=my_list[::-1]
#loop through list within list
n = [[1, 2, 3], [4, 5, 6, 7, 8, 9]]
def flatten (lists):
results=[]
for numbers in lists:
for num in numbers:
results.append(num)
return results
print (flatten(n))
#Sort string
def AlphabetSoup(value):
list = [c for c in value] #Convert string into list
list.sort() #Sort List
return "".join(list) #Convert List to String
print (AlphabetSoup("hElP"))
#Check if in a string there is 'a' and 'b' 4 chars later
def ABCheck(str):
i=0
while i<len(str)-4:
if str[i]=="a" and str[i+4]=="b":
return True
i+=1
return False
print (ABCheck("Laura sobs"))
#return true if the parameter is a palindrome,
def Palindrome(str):
newstr=str.replace(" ","")
palin=False
i=0
while i<len(newstr)/2:
if not newstr[i]==newstr[len(newstr)-1-i]:
return False
i+=1
return True
print (Palindrome("never odd or even"))
# Arithmetic - same difference between members
# Geometric - same product between members
def ArithGeo(arr):
i=1
ari=True
diff=arr[0]-arr[1]
mult=arr[0]/arr[1]
geo=True
while i<len(arr)-2:
if not diff==arr[i]-arr[i+1]:
ari=False
if not mult==arr[i]/arr[i+1]:
geo=False
i+=1
if ari:
return "Arithmetic"
if geo:
return "Geometric"
return -1
print (ArithGeo([2,4,8,16,32]))
#****List Comprehensions****
#[ expression-involving-loop-variable for loop-variable in sequence ]
#Create a list, squares, that consists of the squares of the numbers 1 to 10.
squars=[x**2 for x in range(1, 11)]
#Print the last letter of each name in the list of names:
#print ( [ name[-1] for name in names ])
#Print the reverse of each name in the list of names:
#print ([ name[::-1] for name in names ])
#Battleship game
from random import randint
# Generates a number from 1 through 10 inclusive
random_number = randint(1, 10)
board = []
for x in range(5):
board.append(["O"] * 5)
def print_board(board):
for row in board:
print (" ".join(row))
print ("Let's play Battleship!")
print_board(board)
def random_row(board):
return randint(0, len(board) - 1)
def random_col(board):
return randint(0, len(board[0]) - 1)
ship_row = random_row(board)
ship_col = random_col(board)
print (ship_row)
print (ship_col)
# Everything from here on should go in your for loop!
# Be sure to indent four spaces!
for turn in range(4):
print ("Turn", turn + 1)
guess_row = int(input("Guess Row:"))
guess_col = int(input("Guess Col:"))
if guess_row == ship_row and guess_col == ship_col:
print ("Congratulations! You sunk my battleship!")
break # continue statement is also available in Python
else:
if (guess_row < 0 or guess_row > 4) or (guess_col < 0 or guess_col > 4):
print ("Oops, that's not even in the ocean.")
elif(board[guess_row][guess_col] == "X"):
print ("You guessed that one already.")
else:
print ("You missed my battleship!")
board[guess_row][guess_col] = "X"
# Print (turn + 1) here!
if turn==3:
print ("Game Over")
print_board(board)
num = 1
while num<11: # Fill in the condition
# Print num squared
# Increment num (make sure to do this!)
print (num**2)
num+=1
else:
print ("I am out of this while loop")
list_a = [3, 9, 17, 15, 19]
list_b = [2, 4, 8, 10, 30, 40, 50, 60, 70, 80, 90]
for a, b in zip(list_a, list_b):
# Add your code here!
if a>b:
print (a)
else:
print (b)
fruits = ['banana', 'apple', 'orange', 'tomato', 'pear', 'grape']
print ('You have...')
for f in fruits:
if f == 'tomato':
print ('A tomato is not a fruit!') # (It actually is.))
break # will exit the whole for structure, so it will also skip "else". It is 'pear' and 'grape' will not be printed
print ('A', f)
else:
print ('A fine selection of fruits!')
for i in range(5):
if i==3:
print ("you are hitting 3!!")
else:
print (i)
else:
print ("Ended the for loop!")
#Run through dictionary
score = {"a": 1, "c": 3, "b": 3, "e": 1, "d": 2, "g": 2,
"f": 4, "i": 1, "h": 4, "k": 5, "j": 8, "m": 3,
"l": 1, "o": 1, "n": 1, "q": 10, "p": 3, "s": 1,
"r": 1, "u": 1, "t": 1, "w": 4, "v": 4, "y": 4,
"x": 8, "z": 10}
def scrabble_score (word):
sum=0
i=0
word=word.lower()
while i<len(word):
sum+=int((score[word[i]]))
i+=1
return sum
print (scrabble_score ("Helix"))
#run through string and replace string by string
def anti_vowel(text):
str=""
i=0
while i<len(text):
if not text[i] in "aeiouAEIOU":
str=str+text[i]
i=i+1
return str
print (anti_vowel ("Hey look Words!"))
#find longest word in a string
def LongestWord(sen):
current=""
longest=""
i=0
sen=sen+" "
for n in range(len(sen)):
if sen[n].upper()>="A" and sen[n].upper()<="Z":