Skip to content

Commit 14fbee5

Browse files
Add files via upload
1 parent 7289f57 commit 14fbee5

File tree

1 file changed

+1
-0
lines changed

1 file changed

+1
-0
lines changed

Assignment04.ipynb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"metadata":{"kernelspec":{"display_name":"Python 3","language":"python","name":"python3"},"language_info":{"codemirror_mode":{"name":"ipython","version":3},"file_extension":".py","mimetype":"text/x-python","name":"python","nbconvert_exporter":"python","pygments_lexer":"ipython3","version":"3.7.6"},"colab":{"name":"CSE110 Assignment04.ipynb","provenance":[],"collapsed_sections":[]}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"# CSE110 Assignment 4\n\n\n## Write the Python code of the following problems:\n**<font color='red'>[MUST MAINTAIN VARIABLE NAMING CONVENTIONS FOR ALL THE TASKS]</font>**\n","metadata":{"id":"U4PRkuyUtDRw","colab_type":"text"}},{"cell_type":"markdown","source":"## Part 1: Tuple","metadata":{"id":"PD425BrvtDRy","colab_type":"text"}},{"cell_type":"markdown","source":"### Task 1\n\nAssume, you have been given a tuple.\n\na_tuple = (\"The Institute\",\\\n (\"Best Mystery & Thriller\", \"The Silent Patient\", 68821),\\\n 75717,\\\n [1, 2, 3, 400, 5, 6, 7],\\\n (\"Best Fiction\", \"The Testaments\", 98291)\\\n)\n\nWrite **one line** of Python code to access and print the value 400.\n\nOutput: 400\n\n","metadata":{"id":"FNOorLYXtDRz","colab_type":"text"}},{"cell_type":"code","source":"a_tuple = (\"The Institute\", (\"Best Mystery & Thriller\", \"The Silent Patient\", 68821), 75717, [1, 2, 3, 400, 5, 6, 7], \n (\"Best Fiction\", \"The Testaments\", 98291))\nprint(a_tuple[3][3])","metadata":{"id":"NDKB1cIMtDR0","colab_type":"code","colab":{},"trusted":true},"execution_count":1,"outputs":[{"name":"stdout","text":"400\n","output_type":"stream"}]},{"cell_type":"markdown","source":"### Task 2\n\nAssume, you have been given a tuple.\n\na_tuple = (10, 20, 24, 25, 26, 35, 70)\n\nWrite a Python program that creates a **new tuple** excluding the first and last two elements of the given tuple and prints the new tuple. \n\nOutput: (24, 25, 26)\n\n*Hint: You need to use tuple slicing.*\n","metadata":{"id":"BXlIw1svtDR-","colab_type":"text"}},{"cell_type":"code","source":"a_tuple = (10, 20, 24, 25, 26, 35, 70)\nnew = a_tuple[2:-2]\nprint(new)","metadata":{"id":"FTJ-5AmNtDR_","colab_type":"code","colab":{},"trusted":true},"execution_count":2,"outputs":[{"name":"stdout","text":"(24, 25, 26)\n","output_type":"stream"}]},{"cell_type":"markdown","source":"### Task 3\n\nAssume, you have been given a tuple. \n\n\nbook_info = (\\\n (\"Best Mystery & Thriller\",\"The Silent Patient\",68,821),\\\n (\"Best Horror\",\"The Institute\",75,717),\\\n (\"Best History & Biography\",\"The five\",31,783 ),\\\n (\"Best Fiction\",\"The Testaments\",98,291)\\\n)\n\nWrite a Python program that prints its size and all its elements.\n\nOutput:\\\nSize of the tuple is: 4\\\n('Best Mystery & Thriller', 'The Silent Patient', 68, 821)\\\n('Best Horror', 'The Institute', 75, 717)\\\n('Best History & Biography', 'The five', 31, 783)\\\n('Best Fiction', 'The Testaments', 98, 291)\n","metadata":{"id":"Z3GS2TE6tDSG","colab_type":"text"}},{"cell_type":"code","source":"book_info = ((\"Best Mystery & Thriller\", \"The Silent Patient\", 68, 821), (\"Best Horror\", \"The Institute\",\n 75, 717), (\"Best History & Biography\", \"The five\", 31, 783), (\"Best Fiction\", \"The Testaments\", 98, 291))\n\nlength = len(book_info)\nprint(\"Size of the tuple is: \", length)\nfor elements in book_info:\n print(elements)","metadata":{"id":"RPFYcAjntDSG","colab_type":"code","colab":{},"trusted":true},"execution_count":3,"outputs":[{"name":"stdout","text":"Size of the tuple is: 4\n('Best Mystery & Thriller', 'The Silent Patient', 68, 821)\n('Best Horror', 'The Institute', 75, 717)\n('Best History & Biography', 'The five', 31, 783)\n('Best Fiction', 'The Testaments', 98, 291)\n","output_type":"stream"}]},{"cell_type":"markdown","source":"### Task 4\n\nAssume, you have been given a tuple. \n\nbook_info = (\\\n (\"Best Mystery & Thriller\",\"The Silent Patient\",68821),\\\n (\"Best Horror\",\"The Institute\",75717),\\\n (\"Best History & Biography\",\"The five\",31783 ),\\\n (\"Best Fiction\",\"The Testaments\",98291)\\\n)\n\nWrite a Python program that prints the information about the Good Reads Choice Awards from a tuple. Your program should print the award category, the book name, and its total votes needed for the win.\n\n**Output:**\\\nThe Silent Patient won the 'Best Mystery & Thriller' category with 68821 votes\\\nThe Institute won the 'Best Horror' category with 75717 votes\\\nThe five won the 'Best History & Biography' category with 31783 votes\\\nThe Testaments won the 'Best Fiction' category with 98291 votes\n\n*Hint: You need to handle the quotation marks as a part of the output.*\n**<font color='red'>[Must use Tuple unpacking for printing.]</font>**\n\n\n","metadata":{"id":"glKgAPlGtDSK","colab_type":"text"}},{"cell_type":"code","source":"book_info = ((\"Best Mystery & Thriller\", \"The Silent Patient\", 68821), (\"Best Horror\", \"The Institute\",\n 75717), (\"Best History & Biography\", \"The five\", 31783), (\"Best Fiction\", \"The Testaments\", 98291))\n\nfor x in range(len(book_info)):\n a, b, c = book_info[x]\n print(b, \"won the\" + \"'\" + str(a) + \"'\" + \"catagory with\", c, \"votes\")","metadata":{"id":"LgTKpSGatDSM","colab_type":"code","colab":{},"trusted":true},"execution_count":4,"outputs":[{"name":"stdout","text":"The Silent Patient won the'Best Mystery & Thriller'catagory with 68821 votes\nThe Institute won the'Best Horror'catagory with 75717 votes\nThe five won the'Best History & Biography'catagory with 31783 votes\nThe Testaments won the'Best Fiction'catagory with 98291 votes\n","output_type":"stream"}]},{"cell_type":"markdown","source":"### Task 5\n\nAssume, you have been given a tuple.\n\na_tuple = ( [1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12])\n\nWrite a Python program that asks the user for input (can be any data type) and **replaces the last element of each of the inner lists with the user given value.**\n\n**Example 1:**\\\nInput: “abc”\\\nOutput: ([1, 2, 'abc'], [4, 5, 'abc'], [7, 8, 'abc'], [10, 11, 'abc'])\n\n**Example 2:**\\\nInput: 1000\\\nOutput: ([1, 2, '1000'], [4, 5, '1000'], [7, 8, '1000'], [10, 11, '1000'])\n\n","metadata":{"id":"hoElNRL6tDSR","colab_type":"text"}},{"cell_type":"code","source":"a_tuple = ([1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12])\nuser_input = input(\"Enter a string: \")\nfor x in range(len(a_tuple)):\n a_tuple[x][-1] = user_input\nprint(a_tuple)","metadata":{"id":"mC-B1fpStDST","colab_type":"code","colab":{},"trusted":true},"execution_count":5,"outputs":[{"output_type":"stream","name":"stdin","text":"Enter a string: 1000\n"},{"name":"stdout","text":"([1, 2, '1000'], [4, 5, '1000'], [7, 8, '1000'], [10, 11, '1000'])\n","output_type":"stream"}]},{"cell_type":"markdown","source":"## Part 2: Dictionary","metadata":{"id":"km0QurTJtDSX","colab_type":"text"}},{"cell_type":"markdown","source":"### Task 6\n\nSuppose there is a dictionary of your shopping list.\n\nshopping_list = {'Chocolate': 'Hersheys', 'Cocoa powder': 'Cadbury', 'Spread': 'Nutella'} \n\nNow write a python program to add a new key called 'Baking powder' and assign 'Foster clarks' as its value.\n\n**Output:**\\\n{'Chocolate': 'Hersheys', 'Cocoa powder': 'Cadbury', 'Spread': 'Nutella', 'Baking powder': 'Foster clarks'}\n","metadata":{"id":"QlIVub1VtDSY","colab_type":"text"}},{"cell_type":"code","source":"shopping_list = {'Chocolate': 'Hersheys','Cocoa powder': 'Cadbury', 'Spread': 'Nutella'}\nshopping_list['Baking powder'] = 'Foster clarks'\nprint(shopping_list)","metadata":{"id":"tCLaSZP-tDSZ","colab_type":"code","colab":{},"trusted":true},"execution_count":6,"outputs":[{"name":"stdout","text":"{'Chocolate': 'Hersheys', 'Cocoa powder': 'Cadbury', 'Spread': 'Nutella', 'Baking powder': 'Foster clarks'}\n","output_type":"stream"}]},{"cell_type":"markdown","source":"### Task 7\nSuppose you are given the following two dictionaries.\\\nmarks_boys = {'Harry':15, 'Draco':8, 'Nevil':19}\\\nmarks_girls = {'Ginie':18, 'Luna': 14}\n\nNow create a new dictionary marks merging the two of the above dictionaries, **so that the original two dictionaries remain unchanged.**\n\n**Output:**\\\n{'Harry': 15, 'Draco': 8, 'Nevil': 19, 'Ginie': 18, 'Luna': 14}\n\n*Hint: You can use dictionary functions.*\n","metadata":{"id":"kvV-HnCntDSc","colab_type":"text"}},{"cell_type":"code","source":"marks_boys = {'Harry': 15, 'Draco': 8, 'Nevil': 19}\nmarks_girls = {'Ginie': 18, 'Luna': 14}\nnew_dict = {}\nnew_dict.update(marks_boys)\nnew_dict.update(marks_girls)\nprint(new_dict)","metadata":{"id":"BGSyClBPtDSd","colab_type":"code","colab":{},"trusted":true},"execution_count":7,"outputs":[{"name":"stdout","text":"{'Harry': 15, 'Draco': 8, 'Nevil': 19, 'Ginie': 18, 'Luna': 14}\n","output_type":"stream"}]},{"cell_type":"markdown","source":"### Task 8\nWrite a Python program to find the average of all the values in a dictionary. <font color='red'>[ You are not allowed to use len() and sum()]</font>\n\n**Input:**\\\nsalary = {'Jon': 100, 'Dan':200, 'Rob':300}\n\n**Output:**\\\nAverage is 200\n\n*Hint: You can use dictionary functions to get all the values of the dictionary, then run loop to calculate the sum and the total number of values in the dictionary.*\n","metadata":{"id":"QRzP4HYStDSi","colab_type":"text"}},{"cell_type":"code","source":"salary = {'Jon': 100, 'Dan': 200, 'Rob': 300}\nsum = 0\nnum = 0\nfor x in salary.values():\n sum += x\n num += 1\nprint(\"Average is: \", int(sum/num))","metadata":{"id":"VMRLnA53tDSk","colab_type":"code","colab":{},"trusted":true},"execution_count":8,"outputs":[{"name":"stdout","text":"Average is: 200\n","output_type":"stream"}]},{"cell_type":"markdown","source":"### Task 9\nWrite a Python program that discards **duplicate elements** from the following dictionary. For the common values of the dictionary, only 1 key should be in the output.\n\nbank = {'customer1':101, 'customer2':102, 'customer3':101, 'customer4':103, 'customer5':102}\n\n**Output:**\\\n{'customer1': 101, 'customer2': 102, 'customer4': 103}\n\n*Hint: Think of membership operators (in and not in). You can use dictionary functions to get the values.*\n","metadata":{"id":"5NS_moXQtDSo","colab_type":"text"}},{"cell_type":"code","source":"bank = {'customer1': 101, 'customer2': 102,\n 'customer3': 101, 'customer4': 103, 'customer5': 102}\nnew_bank = {}\nfor i, j in bank.items():\n if j not in new_bank.values():\n new_bank.update({i: j})\nprint(new_bank)","metadata":{"id":"LUKwue_ctDSo","colab_type":"code","colab":{},"trusted":true},"execution_count":9,"outputs":[{"name":"stdout","text":"{'customer1': 101, 'customer2': 102, 'customer4': 103}\n","output_type":"stream"}]},{"cell_type":"markdown","source":"### Task 10\nWrite a Python program that finds the largest value with its key from the following dictionary. <font color='red'>[without using max() function]</font> \n\nbook_shop = {'sci fi': 12, 'mystery': 15, 'horror': 8, 'mythology': 10, 'young_adult': 4, 'adventure':14}\n\n**Output:**\\\nThe highest selling book genre is mystery and the number of books sold are 15\n\n*Hint: Think of membership operators (in and not in). You can use dictionary functions to get the values.*\n","metadata":{"id":"IsqFQpqKtDSs","colab_type":"text"}},{"cell_type":"code","source":"book_shop = {'sci fi': 12, 'mystery': 15, 'horror': 8, 'mythology': 10, 'young_adult': 4, 'adventure': 14}\nhighest = 0\nfor key, value in book_shop.items():\n if value > highest:\n highest = value\n key_highest = key\nprint(\"The highest selling book genre is\", key_highest, \"and the number of books sold are\", highest)","metadata":{"id":"v9Kosgs4tDSt","colab_type":"code","colab":{},"trusted":true},"execution_count":10,"outputs":[{"name":"stdout","text":"The highest selling book genre is mystery and the number of books sold are 15\n","output_type":"stream"}]},{"cell_type":"markdown","source":"### Task 11\nWrite a Python program that combines two dictionary adding values for common keys. \n**Input:**\\\ndict1 = {'a': 10, 'b':20, 'c':30}\\\ndict2 = {'a': 10, 'c':20, 'd':30, 'e':40}\n\n**Output:**\\\n{'a': 20, 'b': 20, 'c': 50, 'd': 30, 'e': 40}\n\n\n*Hint: Think of membership operators (in and not in). You can use dictionary functions to get the values. Need to create a new dictionary to store the combined items.* \n","metadata":{"id":"qozufw7itDSy","colab_type":"text"}},{"cell_type":"code","source":"dict1 = {'a': 10, 'b': 20, 'c': 30}\ndict2 = {'a': 10, 'b': 20, 'd': 30, 'e': 40}\nnew_dict = {}\nnew_dict.update(dict1)\nfor key, value in dict2.items():\n if key not in new_dict.keys():\n new_dict[key] = value\n else:\n new_dict[key] += value\nprint(new_dict)","metadata":{"id":"S0EIYODZtDSz","colab_type":"code","colab":{},"trusted":true},"execution_count":11,"outputs":[{"name":"stdout","text":"{'a': 20, 'b': 40, 'c': 30, 'd': 30, 'e': 40}\n","output_type":"stream"}]},{"cell_type":"markdown","source":"### Task 12\nWrite a Python program that takes a string as an input from the user and counts the frequency of each character using the dictionary. For solving this problem, you need to use each character as a key and its frequency as values. <font color='red'>[without using count() function]</font> \n\n\n**Input:**\\\n\"Python programming is fun\"\n\n**Output:**\\\n{'p': 2, 'y': 1, 't': 1, 'h': 1, 'o': 2, 'n': 3, 'r': 2, 'g': 2, 'a': 1, 'm': 2, 'i': 2, 's': 1, 'f': 1, 'u': 1}\n\n*Hint: Need to create a new dictionary to store the frequency. Case is ignored (P and p are considered same).*\n\n","metadata":{"id":"hAqjgwGNtDS2","colab_type":"text"}},{"cell_type":"code","source":"user_string = input(\"Enter a line: \")\nnew_dict = {}\nvalue = 1\nfor char in range(len(user_string)):\n if user_string[char] == \" \":\n continue\n elif user_string[char] not in new_dict.keys():\n new_dict[user_string.lower()[char]] = value\n continue\n else:\n new_dict[user_string[char]] = value + 1\nprint(new_dict)","metadata":{"id":"-m2yPIT7tDS3","colab_type":"code","colab":{},"trusted":true},"execution_count":12,"outputs":[{"output_type":"stream","name":"stdin","text":"Enter a line: Python programming is fun\n"},{"name":"stdout","text":"{'p': 2, 'y': 1, 't': 1, 'h': 1, 'o': 2, 'n': 2, 'r': 2, 'g': 2, 'a': 1, 'm': 2, 'i': 2, 's': 1, 'f': 1, 'u': 1}\n","output_type":"stream"}]},{"cell_type":"markdown","source":"### Task 13\nSuppose you are given the following dictionary where its values are lists. \n\n\ndict = {'A': [1,2,3], 'b': ['1', '2'], \"c\": [4, 5, 6, 7]}\n\nWrite a Python program that counts the total number of items in the dictionary’s values and prints it. <font color='red'>[without using sum() , len(), count() function]</font> \n\n**Output:**\\\n9\n\n","metadata":{"id":"QWf1zsBctDS6","colab_type":"text"}},{"cell_type":"code","source":"dict = {'A': [1, 2, 3], 'b': ['1', '2'], \"c\": [4, 5, 6, 7]}\ncount = 0\nfor x in dict.values():\n for value_count in dict.values():\n count += 1\nprint(count)","metadata":{"id":"atnD3A1utDS6","colab_type":"code","colab":{},"trusted":true},"execution_count":13,"outputs":[{"name":"stdout","text":"9\n","output_type":"stream"}]},{"cell_type":"markdown","source":"### Task 14\n\nWrite a Python program that takes a number (N) from the user and creates a dictionary with all the numbers starting from 1 to N (including) as keys and its cubes as values. Lastly, prints the dictionary.\n\n**Example1:** \n\nInput: 5\\\nOutput: {1: 1, 2: 8, 3: 27, 4: 64, 5: 125}\n\n**Example2:** \n\nInput: 3\\\nOutput: {1: 1, 2: 8, 3: 27}\n","metadata":{"id":"rCCFcYmPtDS-","colab_type":"text"}},{"cell_type":"code","source":"num = int(input(\"Enter a number: \"))\nnew_dict = {}\nfor i in range(1, num+1):\n new_dict.update({i: i**3})\nprint(new_dict)","metadata":{"id":"1l3nIxHltDS-","colab_type":"code","colab":{},"trusted":true},"execution_count":14,"outputs":[{"output_type":"stream","name":"stdin","text":"Enter a number: 5\n"},{"name":"stdout","text":"{1: 1, 2: 8, 3: 27, 4: 64, 5: 125}\n","output_type":"stream"}]},{"cell_type":"markdown","source":"### Task 15\n\nSuppose you have been given the following list of tuples. \n\nlist = [(\"a\", 1), (\"b\", 2), (\"a\", 3), (\"b\", 1), (\"a\", 2), (\"c\", 1)]\n\nWrite a Python program that converts this list of tuples into a dictionary and prints the dictionary. <font color='red'>[You are not allowed to use set]</font> \n\n**Output:**\n\n{'a': [1, 3, 2], 'b': [2, 1], 'c': [1]}\n\n*Hint: Think of membership operators (in and not in).* \n","metadata":{"id":"-imuPb3EtDTD","colab_type":"text"}},{"cell_type":"code","source":"list = [(\"a\", 1), (\"b\", 2), (\"a\", 3), (\"b\", 1), (\"a\", 2), (\"c\", 1)]\nnew_dict = {}\nnew_list = []\nfor tuple in list:\n if tuple[0] not in new_dict.keys():\n new_dict[tuple[0]] = []\n new_dict[tuple[0]].append(tuple[1])\n else:\n new_dict[tuple[0]].append(tuple[1])\nprint(new_dict)","metadata":{"id":"bBBcHpgYtDTE","colab_type":"code","colab":{},"trusted":true},"execution_count":15,"outputs":[{"name":"stdout","text":"{'a': [1, 3, 2], 'b': [2, 1], 'c': [1]}\n","output_type":"stream"}]},{"cell_type":"code","source":"","metadata":{"id":"8sBhQBmhtDTI","colab_type":"code","colab":{}},"execution_count":null,"outputs":[]}]}

0 commit comments

Comments
 (0)