{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Q no.1) Write a complete Python program that opens a file called 'myfile', reads the entire contents of the file into a string s, determines the number of words in myfile and prints that number on a line.\n",
"Prints out all words in myfile in alphabetical order, each word on a separate line"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"1\n",
"\n"
]
},
{
"data": {
"text/plain": [
"\"\\n# 2nd solution : converting all text to lowercase and performing sort operation\\n\\nls=[]\\ntry:\\n fhand=open('myfile.txt','r') # By default it is in read mode\\n #print('file opened successfully')\\nexcept:\\n print('File does not exist')\\ns=fhand.read().lower()\\nprint(len(s.replace('\\n',' ').split(' ')))\\nls=sorted(s.replace('\\n',' ').split(' '))\\nfor i in ls:\\n print(i)\\n\\n\""
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# 1st solution : sorting the raw file without converting to any case\n",
"\n",
"ls=[]\n",
"\n",
"try:\n",
" fhand=open('myfile.txt','r') # By default it is in read mode\n",
" #print('file opened successfully')\n",
"except:\n",
" print('File does not exist')\n",
"\n",
"# read() return full content of file in string\n",
"s=fhand.read()\n",
"\n",
"# '\\n' indicates next line, so i replaced \\n with ' '(space) the i used split function to know no of words in file\n",
"print(len(s.replace('\\n',' ').split(' ')))\n",
"\n",
"# sorted() is used to sort the list of words in ascending, if u want in descending order replace True instead of False (reverse = True)\n",
"ls=sorted(s.replace('\\n',' ').split(' '),reverse = False) # Bydefault reverse =False\n",
"\n",
"# To print all the words\n",
"for i in ls:\n",
" print(i)\n",
"\n",
"\n",
"'''\n",
"# 2nd solution : converting all text to lowercase and performing sort operation\n",
"\n",
"ls=[]\n",
"try:\n",
" fhand=open('myfile.txt','r') # By default it is in read mode\n",
" #print('file opened successfully')\n",
"except:\n",
" print('File does not exist')\n",
"s=fhand.read().lower()\n",
"print(len(s.replace('\\n',' ').split(' ')))\n",
"ls=sorted(s.replace('\\n',' ').split(' '))\n",
"for i in ls:\n",
" print(i)\n",
"\n",
"'''"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Q no.2) Write a Python function mult(????, ????) that takes two lists ???? and ???? (with len(????) == len(????))\n",
"as input, and returns a new list (of the same length) whose elements are the products of the\n",
"corresponding elements in ???? and ????. Thus if ???? = [1, 2, 3] and ???? = [4, 5, 6] then mult(????, ????) will return\n",
"the list [4, 10, 18]. If ???? and ???? have different lengths, your function will return None."
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"123\n",
"123\n",
"[15129]\n"
]
},
{
"data": {
"text/plain": [
"\"\\n# 2nd Solution \\n\\nimport pandas as pd\\ndef mult(u,v):\\n if len(u)!=len(v):\\n return None\\n else:\\n return list(pd.Series(u)*pd.Series(v))\\n\\nu = input().split(' ')\\nv = input().split(' ')\\ntry:\\n u = list(map(int,u))\\n v = list(map(int,v))\\nexcept:\\n try:\\n u = list(map(int,u))\\n except:\\n v = list(map(int,v))\\nres = mult(u,v)\\nprint(res)\\n\\n\\n\""
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Q no.2)solution :\n",
"\n",
"def mult(u,v):\n",
" # Checking length of two lists, if two list lengths are different then return \"None\"\n",
" if len(u)!=len(v):\n",
" return None\n",
" else:\n",
" ## Here try and except are used because when the input is string, So we can't apply int() to string it throws an error.\n",
" try:\n",
" u1 = list(map(int,u))\n",
" v1 = list(map(int,v))\n",
" return [u1[i]*v1[i] for i in range(len(u))]\n",
" except:\n",
" ## Finally we don't know which list contain string, So I used again try and except in except block also... \n",
" try:\n",
" u1 = list(map(int,u))\n",
" return [u1[i]*v[i] for i in range(len(u))]\n",
" except:\n",
" v1 = list(map(int,v))\n",
" return [u[i]*v1[i] for i in range(len(u))]\n",
"\n",
"## Reading input od two lists\n",
"u = input().split(' ')\n",
"v = input().split(' ')\n",
"\n",
"# Calling the function mult()\n",
"res = mult(u,v)\n",
"\n",
"# Print the Final list \n",
"print(res)\n",
"\n",
"\n",
"'''\n",
"# 2nd Solution \n",
"\n",
"import pandas as pd\n",
"def mult(u,v):\n",
" if len(u)!=len(v):\n",
" return None\n",
" else:\n",
" return list(pd.Series(u)*pd.Series(v))\n",
"\n",
"u = input().split(' ')\n",
"v = input().split(' ')\n",
"try:\n",
" u = list(map(int,u))\n",
" v = list(map(int,v))\n",
"except:\n",
" try:\n",
" u = list(map(int,u))\n",
" except:\n",
" v = list(map(int,v))\n",
"res = mult(u,v)\n",
"print(res)\n",
"\n",
"\n",
"'''"
]
}
],
"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.3"
}
},
"nbformat": 4,
"nbformat_minor": 2
}