-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDataStrcuturesExtra.py
More file actions
1376 lines (1195 loc) · 49.8 KB
/
Copy pathDataStrcuturesExtra.py
File metadata and controls
1376 lines (1195 loc) · 49.8 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
# Write a function called count_characters. count_characters
# should take as input a single string, and return a
# dictionary. In the dictionary, the keys should be
# characters, and the values should be the number of times
# each character appeared in the string.
#
# For example:
#
# count_characters("aabbccc") -> {'a': 2, 'b': 2, 'c': 3}
# count_characters("AaBbbb") -> {'A': 1, 'B': 1, 'a': 1, 'b': 3}
#
# You should not need to make any assumptions about the
# characters in the string: spaces, punctuation, line breaks,
# and any other characters should be handled automatically.
# You may count upper and lower case separately.
# Write your function here!
from itertools import product
def count_characters(str):
dict_char = {}
for i in str:
if i in dict_char:
dict_char[i] += 1
else:
dict_char[i] = 1
return dict_char
# Below are some lines of code that will test your function.
# You can change the value of the variable(s) to test your
# function with different inputs.
#
# If your function works correctly, this will originally
# print (although the order of the keys may vary):
#
# {'a': 2, 'b': 2, 'c': 3}
print(count_characters("aabbccc"))
print()
# Write a function called find_range. find_range should take
# as input a string representing a filename. The file
# corresponding to that filename will be a list of integers,
# one integer per line. find_range should return a tuple
# containing the smallest and largest numbers in the file
# (the smallest first, then the largest).
#
# For example, in the dropdown in the top left you'll find a
# file named FindRangeInput.txt. The smallest number in that
# file is 2, and the largest is 37. So, if you called
# find_range("FindRangeInput.txt"), the function would return
# (2, 37), a tuple with two integers.
#
# You may assume that all lines in the file will contain a
# positive integer (greater than 0). There may be duplicates.
#
# Hint: Remember, if you loop through all the lines in a file
# then you have to close and reopen the file to read it again,
# or by use file.seek(0) to start from the top. However, you
# can do this problem without having to read the file twice.
# Write your function here!
def find_range(find_range):
min_value = 0
max_val = 0
range_tuple = ()
integers = open(find_range, "r")
numbers = {int(line) for line in integers}
max_val = max(numbers)
min_val = min(numbers)
range_tuple = min_val, max_val
return range_tuple
integers.close()
# Below are some lines of code that will test your function.
# You can change the value of the variable(s) to test your
# function with different inputs.
#
# If your function works correctly, this will originally
# print: (2, 37)
print(find_range("FindRangeInput.txt"))
print()
# Write a function called pivot_library. pivot_library takes
# as input one parameter, a list of 3-tuples. Each tuple in
# the list has three items: the first item is a book title
# (a string), the second item is the book's author (a
# string), and the third item is the book's ISBN number (a
# string).
#
# pivot_library should return a dictionary. In the dictionary
# that it returns, the keys should be the ISBN numbers, and
# the values should be 2-item tuples. In each tuple, the first
# item should be the book title, and the second item should
# be the author's name.
#
# Hint: Unpack the tuple to variables first, then create the
# new dictionary item.
#
# For example:
#
# books = [("Of Mice and Men", "John Steinbeck", "978-0-140-17739-8"),
# ("Introduction to Computing", "David Joyner", "978-1-260-08227-2")]
# pivot_library(books)
# -> {"978-0-140-17739-8": ("Of Mice and Men", "John Steinbeck"),
# "978-1-260-08227-2": ("Introduction to Computing", "David Joyner")}
# Write your function here!
def pivot_library(books):
book_dict = {}
new_tuple = ()
for book in books:
(book_title, author, isbn_no) = book
book_dict[isbn_no] = book_title, author
return book_dict
# Below are some lines of code that will test your function.
# You can change the value of the variable(s) to test your
# function with different inputs.
#
# If your function works correctly, this will originally
# print (although the order of the keys may vary):
#
# {"978-0-140-17739-8": ("Of Mice and Men", "John Steinbeck"), "978-1-260-08227-2": ("Introduction to Computing", "David Joyner")}
books = [("Of Mice and Men", "John Steinbeck", "978-0-140-17739-8"),
("Introduction to Computing", "David Joyner", "978-1-260-08227-2")]
print(pivot_library(books))
print()
# Write a function called write_book_info. write_book_info
# will take as input two parameters: a string and a list of
# 3-tuples.
#
# The string will represent the filename to which to write.
#
# Each 3-tuple in the list will contain three strings: a
# book title, the book's author, and the book's ISBN number.
#
# write_book_info should write the list of books to the file
# given by the filename using the following format:
#
# ISBN: Title by Author
#
# So, for this list:
#
# [("Of Mice and Men", "John Steinbeck", "978-0-140-17739-8"),
# ("Introduction to Computing", "David Joyner", "978-1-260-08227-2")]
#
# The file printed would look like this:
#
# 978-0-140-17739-8: Of Mice and Men by John Steinbeck
# 978-1-260-08227-2: Introduction to Computing by David Joyner
#
# We've included Sample.txt to show you what a longer version
# of one of these files might look like.
# Write your function here!
def write_book_info(book_file, books):
my_file = open(book_file, "w")
for book in books:
(title, author, isbn_no) = book
book_string = isbn_no + ": " + title + " by " + author
my_file.writelines(str(book_string) + "\n")
my_file.close()
# Below are some lines of code that will test your function.
# You can change the value of the variable(s) to test your
# function with different inputs.
#
# If your function works correctly, this will originally
# print nothing -- however, it should write the same contents
# as Sample.txt to Test.txt.
books = [("Of Mice and Men", "John Steinbeck", "978-0-140-17739-8"),
("Introduction to Computing", "David Joyner", "978-1-260-08227-2")]
write_book_info("Test.txt", books)
print()
# APA citation style cites author names like this:
#
# Last, F., Joyner, D., Burdell, G.
#
# Note the following:
#
# - Each individual name is listed as the last name, then a
# comma, then the first initial, then a period.
# - The names are separated by commas, including the last
# two.
# - There is no space or comma following the last period.
#
# Write a function called names_to_apa. names_to_apa should
# take as input one string, and return a reformatted string
# according to the style given above. You can assume that
# the input string will be of the following format:
#
# First Last, David Joyner, and George Burdell
#
# You may assume the following:
#
# - There will be at least three names, with "and" before
# the last name.
# - Each name will have exactly two words.
# - There will be commas between each pair of names.
# - The word 'and' will precede the last name.
# - The names will only be letters (no punctuation, special
# characters, etc.), and first and last name will both be
# capitalized.
#
# Hint: You can use the string replace() method to delete
# text from a string. For example, a_string.replace("hi", "")
# will delete all instances of "hi". There are multiple ways
# you might choose to use this.
# Write your function below!
def names_to_apa(names):
names = names.replace(', and ', ', ')
names = names.split(', ')
names = [name.split() for name in names]
names = [name[-1] + ', ' + (''.join(initial[0] + '.' for initial in name[:-1])) for name in names]
names = ', '.join(names[:-1]) + ', ' + "& " + names[-1]
return names
# Below are some lines of code that will test your function.
# You can change the value of the variable(s) to test your
# function with different inputs.
#
# If your function works correctly, this will originally
# print: Last, F., Joyner, D., & Burdell, G.
print(names_to_apa("First Last, David Joyner, and Sophia Wilson"))
print()
# Write a function called check_formula. The check_formula
# function should take as input one parameter, a string. It
# should return True if the string holds a correctly
# formatted arithmetic integer formula according to the rules
# below, or False if it does not.
#
# For this problem, here are the rules that define a
# correctly-formatted arithmetic string:
#
# - The only characters in the string should be digits or
# the five arithmetic operators: +, -, *, /, and =. Any
# other characters, including spaces, periods, commas,
# or any letters, are not permitted.
# - There may not be any consecutive arithmetic operators.
# Any arithmetic operator must have a number on either
# side of it.
# - There must be an equals sign in the formula.
#
# You do not need to worry about negative numbers or
# parentheses, and you do not need to worry about whether
# the equation is accurate. You may also assume all the
# numbers in the string will be only one digit.
#
# Here are some examples of valid and invalid arithmetic
# formulas:
#
# Valid Invalid
# 5*3=5+2 5*3+5+2 (no equals)
# 5=7 5= (equals sign isn't in the middle)
# 5=2-5 50=-5 (consecutive arithmetic operators)
# 6/2=5/2 a=51 (illegal character)
# -5=5+2 (starts with an operator)
#
# Hint: Remember, as soon as you find *one* thing wrong
# with the string, you know it's invalid and can return
# False. So, go character-by-character through the string
# checking everything that could be wrong. If you don't
# find anything wrong, return True!
# Write your function here!
def check_formula(expression):
"""returns True if expression holds a correctly formatted formula"""
operators = ['+', '-', '*', '/', '='] # allowed operators
found_equals = False
found_operator = False
for i in range(len(expression)):
character = expression[i] # look at each character
# checks if character is an allowed operator
if character in operators:
if found_operator: # checks if found_operator flag is already True
return False # character is a consecutive operator
if i == 0 or i == len(expression) - 1: # checks if operator is at the beginning or end
return False
# checks if there is a digit on either side of the operator
if not expression[i - 1].isdigit() or not expression[i + 1].isdigit():
return False
found_operator = True # sets found operator flag as True
if character == '=': # operator is an equals symbol
found_equals = True
# checks if character is a digit
elif character.isdigit():
found_operator = False
# returns False if the character is neither an allowed operator nor a digit
else:
return False
# returns False if an = symbol was not found in the expression
if not found_equals:
return False
# all rules correct, so returns True
return True
# Below are some lines of code that will test your function.
# You can change the value of the variable(s) to test your
# function with different inputs.
#
# If your function works correctly, this will originally
# print True, then several Falses.
print(check_formula("5*3=5+2"))
print(check_formula("5*3+5+2"))
print(check_formula("5="))
print(check_formula("5=-5"))
print(check_formula("a=5"))
print(check_formula("-5=5+2"))
print()
print()
# In the game tic-tac-toe, two players take turns drawing
# Xs and Os on a 3x3 grid. If one player can place three of
# their symbols side-by-side in a row, column, or diagonal,
# they win the game.
#
# For example:
#
# X Wins: X Wins: X Wins: No Winner:
# X|O|X O|X|X O|O| X|O|O
# -+-+- -+-+- -+-+- -+-+-
# O|O|X X|O| X|X|X O|X|X
# -+-+- -+-+- -+-+- -----
# O|X|X | |O | | X|X|O
#
# Write a function called check_winner. check_winner will
# take one parameter as input, a 2D tuple (that is, a tuple
# of tuples). The 2D tuple represents the game board: each
# smaller tuple in the larger tuple is a row of the board,
# and each item in the smaller tuple is a spot on the
# board. There will always be three tuples in the larger
# tuple, and three items in each of the smaller tuples.
#
# Each item in the smaller tuple will always be one of three
# values: the string "X", the string "O", or the value None.
#
# check_winner should return one of three values: the string
# "X" if X has won the game; the string "O" if O has won the
# game; or the value None if there is no winner. None should
# NOT be the string "None"; it should be the value None,
# like the boolean values True and False.
#
# You may assume a player has won the game if and only if
# the board has three of their symbols in a row: you do not
# need to worry about whether the input is a valid game
# otherwise (e.g. a board of nine Xs still counts as X
# winning). You may assume that there will only be one
# winner per board.
#
# Hint: There are only eight possible places to win (three
# rows, three columns, two diagonals).
#
# Hint 2: If you're comfortable on time, you may want to
# check out the last problem before doing this one. It's
# only worth 1 point, but you might be able to design
# one solution that works for both problems!
# Write your function here!
def check_winner(tuples):
for i in range(len(tuples)):
if tuples[0][0] == tuples[0][1] == tuples[0][2]:
return tuples[0][0]
elif tuples[1][0] == tuples[1][1] == tuples[1][2]:
return tuples[1][0]
elif tuples[2][0] == tuples[2][1] == tuples[2][2]:
return tuples[2][0]
elif tuples[0][2] == tuples[1][2] == tuples[2][2]:
return tuples[0][2]
elif tuples[0][1] == tuples[1][1] == tuples[2][1]:
return tuples[0][1]
elif tuples[0][0] == tuples[1][0] == tuples[2][0]:
return tuples[0][0]
elif tuples[0][0] == tuples[1][1] == tuples[2][2]:
return tuples[0][0]
elif tuples[0][2] == tuples[1][1] == tuples[2][0]:
return tuples[0][2]
# The code below shows how the tic-tac-toe tuples are
# created and tests your code with three games: one where
# X wins, one where O wins, and one where there is no winner.
# Remember, the line breaks in xwins and owins are optional:
# they're just to make the declarations more readable. They
# could be written the same as nowins.
xwins = (("X", "O", "X"),
("O", "O", "X"),
("O", "X", "X"))
owins = (("O", "X", "X"),
("X", "O", None),
(None, None, "O"))
nowins = (("X", "O", "O"), ("O", "X", "X"), ("X", "X", "O"))
print(check_winner(xwins))
print(check_winner(owins))
print(check_winner(nowins))
print()
# Write a function called average_rainfall. average_rainfall
# should have one parameter, a list of integers. The list
# represents daily rainfall measurements for a certain area.
#
# However, at some point in the list, there will be a -1.
# This indicates that you should stop averaging, and ignore
# any subsequent values.
#
# For example:
#
# average_rainfall([1, 2, 3, 4, 5, -1, 6, 7]) -> 3.0
#
# The function would only average 1, 2, 3, 4, and 5, and
# ignore any values after the -1.
#
# You may assume all the items in the list are integers,
# that -1 is guaranteed to occur somewhere in the list,
# and that -1 will not be the first item in the list.
# Add your code here!
def average_rainfall(a_list):
total = 0
avg = 0
new_list = []
for num in a_list:
if num >= 0:
new_list.append(num)
else:
num < 0
break
avg = sum(new_list) / len(new_list)
return avg
# Below are some lines of code that will test your function.
# You can change the value of the variable(s) to test your
# function with different inputs.
#
# If your function works correctly, this will originally
# print: 3.0
a_list = [1, 2, 3, 4, 5, -1, 6, 7]
print(average_rainfall(a_list))
print()
# Write a function called volume_and_area. volume_and_area
# will take in a dictionary. This dictionary is guaranteed to
# have three keys: "length", "width", and "height", whose
# values are integers representing three attributes of a
# rectangular prism (also known as a box).
#
# Modify this dictionary to add two keys: "volume" and "area".
# The values associated with these keys should be the volume
# and surface area of the box.
#
# The formula for volume is:
# length * width * height
#
# The formula for surface area is:
# 2 * ((length * width) + (length*height) + (width*height))
#
# Because length, width, and height are integers, and because
# these formulas have no division, your results should be
# integers as well.
# Add your code here!
def volume_and_area(rectangle):
new_dict = {}
val_prod = 1
surface_area = 1
for key, value in rectangle.items():
val_prod *= value
surface_area = 2 * ((rectangle["length"] * rectangle["width"]) + (
rectangle["length"] * rectangle["height"]) + (
rectangle["width"] * rectangle["height"]))
rectangle["volume"] = val_prod
rectangle["area"] = surface_area
return rectangle
# Below are some lines of code that will test your function.
# You can change the value of the variable(s) to test your
# function with different inputs.
#
# If your function works correctly, this will originally
# print 6 and 22, each on a different line.
rectangle = {"length": 1, "width": 2, "height": 3}
rectangle = volume_and_area(rectangle)
print(rectangle["volume"])
print(rectangle["area"])
print()
# Write a function called imdb_dictionary. imdb_dictionary
# should have one parameter, a string representing a
# filename.
#
# On each row of the file will be a performer's name, then
# a semicolon, then a comma-separated list of movies the have
# been in. For example, one file's contents could be:
#
# Robert Downey Jr.; Avengers: Infinity War, Sherlock Holmes 3, Spider-Man: Homecoming
# Scarlett Johansson; Avengers: Infinity War, Isle of Dogs, Ghost in the Shell
# Elizabeth Olsen; Avengers: Infinity War, Kodachrome, Wind River, Ingrid Goes West
#
# You may assume that the only semi-colon will be after the
# performer's name, and that there will be no commas in the
# movie titles.
#
# Return a dictionary where the keys are each actor's name,
# and the values are alphabetically-sorted lists of the movies
# they have been in. For example, if imdb_dictionary was called
# on the file above, the output would be:
# {"Robert Downey Jr.": ["Avengers: Infinity War", "Sherlock Holmes 3", "Spider-Man: Homecoming"],
# "Scarlett Johansson": ["Avengers: Infinity War", "Ghost in the Shell", "Isle of Dogs"],
# Elizabeth Olsen": ["Avengers: Infinity War", "Ingrid Goes West", "Kodachrome", "Wind River"]}
#
# Make sure the list of movies is sorted alphabetically. Don't
# worry about the order the keys (names) appear in the dictionary.
### write file here:
with open("some_performers.txt", "w") as imbd_file:
imbd_file.write('''Robert Downey Jr.; Avengers: Infinity War, Sherlock Holmes 3, Spider-Man: Homecoming
Scarlett Johansson; Avengers: Infinity War, Isle of Dogs, Ghost in the Shell
Elizabeth Olsen; Avengers: Infinity War, Kodachrome, Wind River, Ingrid Goes West''')
# Add your code here!
# spent a whole trying to crack, pulled my hair out copied codes, after hours of debugging
# came up with my own code...You can not sort this after creating a dictionary, it must be sorted before, with
# the right spaces, commas, and semicolons,if not you will never get the right output....pheeew!!!
def imdb_dictionary(filename):
lines = open(filename, "r")
movie_dict = {}
for line in lines:
line = line.strip()
line = line.split("; ")
name, movie_title = line
movie_title = movie_title.split(", ") # This split makes a difference when sorting, ....
movie_title.sort()
# new_movie_title = ", ".join(movie_title) # reduntant code not needed
# movie_dict[name] = [] redundant not needed
movie_dict[name] = movie_title
lines.close()
return movie_dict
# below are some lines of code that will test your function.
# You can change the contents of some_performers.txt from
# the dropdown in the top left to test other inputs.
#
# If your function works correctly, this will originally print (although the order of the keys may vary): {"Robert
# Downey Jr.": ["Avengers: Infinity War", "Sherlock Holmes 3", "Spider-Man: Homecoming"], "Scarlett Johansson": [
# "Avengers: Infinity War", "Ghost in the Shell", "Isle of Dogs"], Elizabeth Olsen": ["Avengers: Infinity War",
# "Ingrid Goes West", "Kodachrome", "Wind River"]}
print(imdb_dictionary("some_performers.txt"))
print()
# Write a function called digit_count. digit_count should
# take as input a number, which could be either a float or an
# integer. It should return a dictionary whose keys are digits,
# and whose values are the number of times that digit appears
# in the number.
#
# The dictionary should NOT contain any numerals that do not
# occur at all in the number, and it should also note contain
# the decimal point character if the number is a decimal.
#
# For example:
#
# digit_count(11223) -> {1: 2, 2: 2, 3: 1}
# digit_count(3.14159) -> {3: 1, 1: 2, 4: 1, 5: 1, 9: 1}
#
# Hint: You should probably convert the number to a string to
# count the digits, but convert the individual digits back to
# integers to use as keys to the dictionary.
# Write your function here!
# steps:
# Create function.
# convert num to strings
# replace decimal point with ""
# replace "" with " " to create spaces between numbers
# split strings further for char in word ...
# remove first and last blanks using index slicing
# remove empty blanks using list comprehension strip
# get the items in the cleaned list, convert item to int
# assign dict key...to frequency of numbers using if else/statement
# return new dict
def digit_count(num):
num_dict = {}
num_list = []
num = str(num)
num = num.replace(".", "")
num = num.replace("", " ")
num_list = [char for char in num]
num_list = num_list[1::]
num_list = num_list[:-1:]
new_list = [ele for ele in num_list if ele.strip()]
for item in new_list:
item = int(item)
if item in num_dict:
num_dict[item] += 1
else:
num_dict[item] = 1
return num_dict
# Below are some lines of code that will test your function.
# You can change the value of the variable(s) to test your
# function with different inputs.
#
# If your function works correctly, this will originally
# print (although the order of the keys may vary):
#
# {1: 2, 2: 2, 3: 1}
# {3: 1, 1: 2, 4: 1, 5: 1, 9: 1}
print(digit_count(11223))
print(digit_count(3.14159))
print()
# Write a function called pivot_library. pivot_library takes
# as input one parameter, a list of 3-tuples. Each tuple in
# the list has three items: the first item is a book title
# (a string), the second item is the book's author (a
# string), and the third item is the book's ISBN number (a
# string).
#
# pivot_library should return a dictionary. In the dictionary
# that it returns, the keys should be the ISBN numbers, and
# the values should be 2-item tuples. In each tuple, the first
# item should be the book title, and the second item should
# be the author's name.
#
# Hint: Unpack the tuple to variables first, then create the
# new dictionary item.
#
# For example:
#
# books = [("Of Mice and Men", "John Steinbeck", "978-0-140-17739-8"),
# ("Introduction to Computing", "David Joyner", "978-1-260-08227-2")]
# pivot_library(books)
# -> {"978-0-140-17739-8": ("Of Mice and Men", "John Steinbeck"),
# "978-1-260-08227-2": ("Introduction to Computing", "David Joyner")}
# Write your function here!
def pivot_library(books):
book_dict = {}
for book in books:
(book_title, author, isbn_no) = book
book_dict[isbn_no] = book_title, author
return (book_dict)
# Below are some lines of code that will test your function.
# You can change the value of the variable(s) to test your
# function with different inputs.
#
# If your function works correctly, this will originally
# print (although the order of the keys may vary):
#
# {"978-0-140-17739-8": ("Of Mice and Men", "John Steinbeck"), "978-1-260-08227-2": ("Introduction to Computing",
# "David Joyner")}
books = [("Of Mice and Men", "John Steinbeck", "978-0-140-17739-8"),
("Introduction to Computing", "David Joyner", "978-1-260-08227-2")]
print(pivot_library(books))
print()
# Write a function called write_class_data. write_class_data
# will take as input two parameters: a string and a list of
# 3-tuples.
#
# The string will represent the filename to which to write.
#
# Each 3-tuple in the list will contain three values:
#
# - A string representing the major of a college class (e.g.
# "CS", "CHEM", "ENGL")
# - An integer representing a course number (e.g. 1301, 2241,
# 4001)
# - A string representing the name of a course (e.g.
# "Introduction to Computing", "Organic Chemistry", etc.)
#
# write_class_data should write the list of classes to the file
# given by the filename using the following format:
#
# [major][number]: [class name]
#
# So, for this list:
#
# [("CS", 1301, "Introduction to Computing"),
# ("CHEM", "1310", "General Chemistry")]
#
# The file printed would look like this:
#
# CS1301: Introduction to Computing
# CHEM1310: General Chemistry
#
# Note the specifics of that format: no space between the major
# and course number, no space between the course number and the
# colon, one space between the colon and the course name.
#
# We've included Sample.txt to show you what a longer version
# of one of these files might look like.
# Write your function here!
def write_class_data(filename, tuple_list):
with open(filename, "w") as string_file:
for item in tuple_list:
course_code, course_no, course_title = item
new_course = course_code + str(course_no) + ":" + " " + course_title
string_file.write(new_course + "\n")
# Below are some lines of code that will test your function.
# You can change the value of the variable(s) to test your
# function with different inputs.
#
# If your function works correctly, this will originally
# print nothing -- however, it should write the same contents
# as Sample.txt to Test.txt.
classes = [("CS", 1301, "Introduction to Computing"), ("CHEM", "1310", "General Chemistry")]
write_class_data("Test.txt", classes)
with open("FirstLastInput.txt", "w") as first_file:
first_file.write('''8
rjbvm
6
16
26
10
rcubr
18
10
27
lrgfd
13
ldfsw
fsszd
22
6
35
32
36
fseog
37
2
14
14
appsp
dafdo
16
fdsww
zzffs''')
print()
# Write a function called first_to_last. first_to_last should
# take as input a string representing a filename. Inside the
# file will be some text on each line; some lines will contain
# integers, while others will contain strings of other
# characters.
#
# first_to_last should return a tuple containing the first and
# last strings in the file alphabetically. It should ignore any
# lines that contain integers.
#
# For example, in the dropdown in the top left you'll find a
# file named FirstLastInput.txt. Ignoring numerals, the first
# string alphabetically is appsp, and the last string
# alphabetically is zzffs. So, first_to_last("FirstLastInput.txt")
# would return the tuple ("appsp", "zzffs").
#
# You may assume that every line in the file contains either
# all numerals or all lower-case letters; there will be no lines
# with both numerals and letters, nor any capital letters.
# Write your function here!
def first_to_last(filename):
with open(filename, "r") as first_file:
line = first_file.read() # read file use .read
line_list = [] # create empty lists
newline_list = []
line = line.strip("\n") # strip the trailing new line
line = line.split() # convert to a list
line.sort() # sort list
result = [i for i in line if not i.isdigit()] # extract the letters
new_tuple_list = (result[0], result[-1])
return (new_tuple_list)
# Below are some lines of code that will test your function.
# You can change the value of the variable(s) to test your
# function with different inputs.
#
# If your function works correctly, this will originally
# print: ("appsp", "zzffs")
print(first_to_last("FirstLastInput.txt"))
print()
# Write a function called complete_profile. complete_profile
# will take as input a dictionary. This dictionary will have
# four keys: first, middle, last, and title. The function
# should return a dictionary with those four keys, and three
# more: name, full_name, short_name. The values for those
# keys should be:
#
# - name: the first and last name, separated by a space
# - full_name: the title, first, middle, and last names,
# with a space between each pair of strings
# - short_name: the first letter of the first name, a space,
# and their last name
#
# For example:
#
# complete_profile({"first": "David", "middle": "Andrew",
# "last": "Joyner", "title": "Dr."})
#
# would return:
#
# {"first": "David", "middle": "Andrew", "last": "Joyner",
# "title": "Dr.", "name": "David Joyner",
# "full_name": "Dr. David Andrew Joyner",
# "short_name": "D Joyner"}
#
# You may either modify the dictionary that is passed in,
# or create a new one. Either way, make sure to return the
# dictionary at the end of the function.
# Add your code here!
def complete_profile(new_prof):
dict_profile = {}
dict_profile["name"] = new_prof["first"] + " " + new_prof["last"]
dict_profile["full_name"] = new_prof["title"] + " " + new_prof["first"] + " " + new_prof["middle"] + " " + new_prof[
"last"]
get_letter = new_prof["first"][0]
dict_profile["short_name"] = get_letter + " " + new_prof["last"]
new_prof.update(dict_profile)
return new_prof
# Below are some lines of code that will test your function.
# You can change the value of the variable(s) to test your
# function with different inputs.
#
# If your function works correctly, this will originally
# print "David Joyner", "Dr. David Andrew Joyner", and D Joyner" each on a different line (without the quotes).
prof = {"first": "David", "middle": "Andrew", "last": "Joyner", "title": "Dr."}
prof = complete_profile(prof)
print(prof["name"])
print(prof["full_name"])
print(prof["short_name"])
print()
# Write a function called pivot_library. pivot_library takes
# as input one parameter, a list of 3-tuples. Each tuple in
# the list has three items: the first item is a book title
# (a string), the second item is the book's author (a
# string), and the third item is the book's ISBN number (a
# string).
#
# pivot_library should return a dictionary. In the dictionary
# that it returns, the keys should be the ISBN numbers, and
# the values should be new dictionaries. Each new dictionary
# should have two keys: "title" and "author". Their values
# should correspond to the first and second items from the
# original 3-tuple.
#
# For example:
#
# books = [("Of Mice and Men", "John Steinbeck", "978-0-140-17739-8"),
# ("Introduction to Computing", "David Joyner", "978-1-260-08227-2")]
# pivot_library(books)
# -> {"978-0-140-17739-8": {"title": "Of Mice and Men", "author": "John Steinbeck"},
# "978-1-260-08227-2": {"title": "Introduction to Computing", "author": "David Joyner"}}
# Write your function here!
def pivot_library(new_books):
newbook_dict = {}
for books in new_books:
(title, author, ISBN) = books
new_dict = {} # define within loop....
new_dict["title"] = title
new_dict["author"] = author
newbook_dict[ISBN] = new_dict
return newbook_dict
# Below are some lines of code that will test your function.
# You can change the value of the variable(s) to test your
# function with different inputs.
#
# If your function works correctly, this will originally
# print (although the order of the keys may vary):
#
# {"978-0-140-17739-8": {"title": "Of Mice and Men", "author": "John Steinbeck"}, "978-1-260-08227-2": {"title":
# "Introduction to Computing", "author": "David Joyner"}}
books = [("Of Mice and Men", "John Steinbeck", "978-0-140-17739-8"),
("Introduction to Computing", "David Joyner", "978-1-260-08227-2")]
print(pivot_library(books))
print()
# It's a well-known and indisputable fact that if you want
# to make your name sound fancy, you should list it as only
# your first two initials followed by your last name. For
# example, my full name is David Andrew Joyner, and therefore
# my fancy name is D. A. Joyner. (If you have two middle names,
# it's even better, but we'll assume we have only one -- we're
# C. S. Lewis, not J. R. R. Tolkien).
#
# Write a function called fancy_me. fancy_me should take as
# input a list of strings, each representing a full name (e.g.
# "David Andrew Joyner" or "First Middle Last". fancy_me should
# return a single string, formatting that list of names in this
# fancy style, like this:
#
# F. M. Last, D. A. Joyner, G. P. Burdell
#
# Each individual name is the first initial, then a period, then
# a space, then the second initial, then a period, then a space,
# then the last name, then a comma. There is no comma after the
# last name in the list.
#
# For example:
#
# fancy_me(["First Middle Last", "David Andrew Joyner", "George P Burdell"])
#
# ...would return "F. M. Last, D. A. Joyner, G. P. Burdell"
# Write your function below!
def initialize(name):
first, middle, last = name.split() # note this assumes all names will only be three parts
return f"{first[0]}. {middle[0]}. {last}"
def fancy_me(namelist):
return ", ".join(initialize(name) for name in namelist)
# Below are some lines of code that will test your function.
# You can change the value of the variable(s) to test your
# function with different inputs.
#
# If your function works correctly, this will originally
# print: F. M. Last, D. A. Joyner, G. P. Burdell
print(fancy_me(["First Middle Last", "David Andrew Joyner", "George P Burdell"]))
# Another method if number of names not equals to three:
def initialize(name) -> str:
parts = name.split()
return ". ".join(part[0] for part in parts) + parts[-1][1:]
def fancy_me(namelist) -> str: