EXERCISE - 6 DATA STRUCTURES

6 A) Write a program combine_lists that combines these lists into a dictionary. B) Write a program to count frequency of characters in a given file. Can you
use character frequency to tell whether the given file is a Python program
file, C program file or a text file?
A) AIM: Write a program combine_lists that combines these lists into a
dictionary.

SOURCE CODE:

m=int(input("Enter list1 size:")) n=int(input("Enter list2 size:")) list1=[] list2=[] list2dict={} if(m==n): for i in range(m): ele=int(input("Enter list1 element:")) list1.append(ele) for i in range(n): ele=int(input("Enter list2 element:")) list2.append(ele) for i in range(m): list2dict[list1[i]]=list2[i] print("After combine two lists into dictionary:\n",list2dict) else: print("Does not combine two lists into dictionary")

OUTPUT:
Enter list1 size:3 Enter list2 size:3 Enter list1 element:1 Enter list1 element:2 Enter list1 element:3 Enter list2 element:10 Enter list2 element:20 Enter list2 element:30 After combine two lists into dictionary: {1: 10, 2: 20, 3: 30}

B) AIM: Write a program to count frequency of characters in a given file.
Can you use character frequency to tell whether the given file is a Python
program file, C program file or a text file?

SOURCE CODE:

filetypes=['.py','.c','.txt'] filename=input("Enter file name:") fp1=open(filename,"r") for ft in filetypes: if(ft in filename): if(ft=='.py'): print("Given file is Python file.") elif(ft=='.c'): print("Given file is C file.") elif(ft=='.txt'): print("Given file is text file.") filedata=fp1.read() fp1.close() print("File Data is:\n",filedata) dict1={} for key in filedata: if(key in dict1): dict1[key]=dict1[key]+1 else: dict1[key]=1 print("\nFrequency of characters in the given file is:\n",dict1)

OUTPUT:

Enter file name:msg.txt Given file is text file. File Data is: Hello RISE Python programming This is File program Frequency of characters in the given file is: {'H': 1, 'e': 2, 'l': 3, 'o': 4, ' ': 5, 'R': 1, 'I': 1, 'S': 1, 'E': 1,
'\n': 3, 'P': 1, 'y': 1, 't': 1, 'h': 2, 'n': 2, 'p': 2, 'r': 4, 'g': 3,
'a': 2, 'm': 3, 'i': 4, 'T': 1, 's': 2, 'F': 1}

No comments:

Post a Comment

Total Pageviews