Skip to content

Commit a3b3f86

Browse files
authored
function examples practice
1 parent 7961cfb commit a3b3f86

File tree

2 files changed

+132
-0
lines changed

2 files changed

+132
-0
lines changed

Function_Examples.ipynb

+127
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"id": "8c2eb91b-a782-4871-9122-0b653889fbca",
6+
"metadata": {},
7+
"source": [
8+
"## 1]Check if a string is a plaindrome"
9+
]
10+
},
11+
{
12+
"cell_type": "code",
13+
"execution_count": 5,
14+
"id": "21030465-b98a-4583-b755-7fce292323fb",
15+
"metadata": {},
16+
"outputs": [
17+
{
18+
"name": "stdout",
19+
"output_type": "stream",
20+
"text": [
21+
"False\n",
22+
"False\n",
23+
"True\n"
24+
]
25+
}
26+
],
27+
"source": [
28+
"def is_palindrome(s):\n",
29+
" s=s.lower().replace(\"\",\"\")\n",
30+
" return s==s[::-1]\n",
31+
"print(is_palindrome(\"a man a plan a canal panama\"))\n",
32+
"print(is_palindrome(\"Hello\"))\n",
33+
"print(is_palindrome(\"aba\"))\n"
34+
]
35+
},
36+
{
37+
"cell_type": "markdown",
38+
"id": "cad35baa-c958-4cfe-9684-1a8c623d5169",
39+
"metadata": {},
40+
"source": [
41+
"## 2] Calculate the factorials of a number using recursion"
42+
]
43+
},
44+
{
45+
"cell_type": "code",
46+
"execution_count": 2,
47+
"id": "b6ec531f-8f6f-415d-abcf-cc63aec6ebfe",
48+
"metadata": {},
49+
"outputs": [
50+
{
51+
"name": "stdout",
52+
"output_type": "stream",
53+
"text": [
54+
"720\n"
55+
]
56+
}
57+
],
58+
"source": [
59+
"def factorial(n):\n",
60+
" if n==0:\n",
61+
" return 1\n",
62+
" else:\n",
63+
" return n * factorial(n-1)\n",
64+
"print(factorial(6))"
65+
]
66+
},
67+
{
68+
"cell_type": "markdown",
69+
"id": "24895723-19f2-4a65-a976-5166cf4d6531",
70+
"metadata": {},
71+
"source": [
72+
"## 3]A function to read a file and count the frequency of each word"
73+
]
74+
},
75+
{
76+
"cell_type": "code",
77+
"execution_count": 4,
78+
"id": "edc530ca-4ad1-4d62-b8f8-8ce5ca643a9f",
79+
"metadata": {},
80+
"outputs": [
81+
{
82+
"name": "stdout",
83+
"output_type": "stream",
84+
"text": [
85+
"{'hello': 1, 'sunil': 2, 'how': 1, 'are': 3, 'you': 3, 'where': 1, 'what': 1, 'doing': 1, 'in': 1, 'pune': 1}\n"
86+
]
87+
}
88+
],
89+
"source": [
90+
"def count_word_frequency(file_path):\n",
91+
" word_count={}\n",
92+
" with open (file_path,'r') as file:\n",
93+
" for line in file:\n",
94+
" words=line.split()\n",
95+
" for word in words:\n",
96+
" word=word.lower().strip('.,!?;:\"\\'')\n",
97+
" word_count[word]=word_count.get(word,0)+1\n",
98+
" return word_count\n",
99+
"\n",
100+
"filepath='sample.txt'\n",
101+
"word_frquency=count_word_frequency(filepath)\n",
102+
"print(word_frquency)"
103+
]
104+
}
105+
],
106+
"metadata": {
107+
"kernelspec": {
108+
"display_name": "Python 3 (ipykernel)",
109+
"language": "python",
110+
"name": "python3"
111+
},
112+
"language_info": {
113+
"codemirror_mode": {
114+
"name": "ipython",
115+
"version": 3
116+
},
117+
"file_extension": ".py",
118+
"mimetype": "text/x-python",
119+
"name": "python",
120+
"nbconvert_exporter": "python",
121+
"pygments_lexer": "ipython3",
122+
"version": "3.12.7"
123+
}
124+
},
125+
"nbformat": 4,
126+
"nbformat_minor": 5
127+
}

sample.txt

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
Hello Sunil
2+
How are you
3+
Where are you
4+
What are you doing?
5+
Sunil in pune

0 commit comments

Comments
 (0)