Skip to content

Commit 50faf80

Browse files
committed
Modified
1 parent 23013c8 commit 50faf80

File tree

2 files changed

+184
-0
lines changed

2 files changed

+184
-0
lines changed
Lines changed: 153 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,153 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"id": "0dee32b5-eb08-4b7b-9401-90c1a30b0361",
6+
"metadata": {},
7+
"source": [
8+
"# Task\n",
9+
"#### Read a given string, change the character at a given index and then print the modified string.\n",
10+
"#### Function Description\n",
11+
"#### Complete the mutate_string function in the editor below.\n",
12+
"#### mutate_string has the following parameters:\n",
13+
"#### string string: the string to change\n",
14+
"#### int position: the index to insert the character at\n",
15+
"#### string character: the character to insert\n",
16+
"#### Returns\n",
17+
"#### string: the altered string"
18+
]
19+
},
20+
{
21+
"cell_type": "code",
22+
"execution_count": 2,
23+
"id": "34f1ad9d-85dc-48ad-a1f8-ca9d9fd2f763",
24+
"metadata": {},
25+
"outputs": [
26+
{
27+
"name": "stdin",
28+
"output_type": "stream",
29+
"text": [
30+
" abracadabra\n",
31+
" 5 k\n"
32+
]
33+
},
34+
{
35+
"name": "stdout",
36+
"output_type": "stream",
37+
"text": [
38+
"abrackdabra\n"
39+
]
40+
},
41+
{
42+
"name": "stdin",
43+
"output_type": "stream",
44+
"text": [
45+
"Enter the string: abracadabra\n",
46+
"Enter the position and character (space-separated): 5 k\n"
47+
]
48+
},
49+
{
50+
"name": "stdout",
51+
"output_type": "stream",
52+
"text": [
53+
"Mutated string: abrackdabra\n"
54+
]
55+
}
56+
],
57+
"source": [
58+
"# Define a function to mutate a string at a specified position with a given character\n",
59+
"def mutate_string(string, position, character):\n",
60+
" # Convert the string to a list to allow mutation\n",
61+
" l = list(string)\n",
62+
" # Update the character at the specified position in the list\n",
63+
" l[position] = character\n",
64+
" # Join the list back into a string and return the mutated string\n",
65+
" return ''.join(l)\n",
66+
"\n",
67+
"# Main function\n",
68+
"if __name__ == '__main__':\n",
69+
" # Take input for the original string\n",
70+
" s = input()\n",
71+
" # Take input for the position and character to mutate\n",
72+
" i, c = input().split()\n",
73+
" # Call the mutate_string function with inputs and store the mutated string\n",
74+
" s_new = mutate_string(s, int(i), c)\n",
75+
" # Print the mutated string\n",
76+
" print(s_new)\n",
77+
"\n",
78+
"\n",
79+
"#-------------------------OR------------------------\n",
80+
"\n",
81+
"\n",
82+
"# Function to mutate a string at a given position with a new character\n",
83+
"def mutate_string(string, position, character):\n",
84+
" # Create a new string by replacing the character at the specified position\n",
85+
" new_string = string[:position] + character + string[position + 1:]\n",
86+
" return new_string\n",
87+
"\n",
88+
"# Main program\n",
89+
"if __name__ == '__main__':\n",
90+
" # Input a string\n",
91+
" s = input(\"Enter the string: \")\n",
92+
" # Input the position and character to replace\n",
93+
" i, c = input(\"Enter the position and character (space-separated): \").split()\n",
94+
" # Call the mutate_string function to get the mutated string\n",
95+
" s_new = mutate_string(s, int(i), c)\n",
96+
" # Print the mutated string\n",
97+
" print(\"Mutated string:\", s_new)"
98+
]
99+
},
100+
{
101+
"cell_type": "code",
102+
"execution_count": 1,
103+
"id": "8f9a8bbf-d4f7-4b87-b3cd-d272c4559a1a",
104+
"metadata": {},
105+
"outputs": [
106+
{
107+
"name": "stdout",
108+
"output_type": "stream",
109+
"text": [
110+
"Selected Jupyter core packages...\n",
111+
"IPython : 8.25.0\n",
112+
"ipykernel : 6.28.0\n",
113+
"ipywidgets : 8.1.2\n",
114+
"jupyter_client : 8.6.0\n",
115+
"jupyter_core : 5.5.0\n",
116+
"jupyter_server : 2.10.0\n",
117+
"jupyterlab : 4.2.3\n",
118+
"nbclient : 0.8.0\n",
119+
"nbconvert : 7.10.0\n",
120+
"nbformat : 5.9.2\n",
121+
"notebook : 7.2.1\n",
122+
"qtconsole : 5.5.1\n",
123+
"traitlets : 5.14.3\n"
124+
]
125+
}
126+
],
127+
"source": [
128+
"!jupyter --version"
129+
]
130+
}
131+
],
132+
"metadata": {
133+
"kernelspec": {
134+
"display_name": "example",
135+
"language": "python",
136+
"name": "example"
137+
},
138+
"language_info": {
139+
"codemirror_mode": {
140+
"name": "ipython",
141+
"version": 3
142+
},
143+
"file_extension": ".py",
144+
"mimetype": "text/x-python",
145+
"name": "python",
146+
"nbconvert_exporter": "python",
147+
"pygments_lexer": "ipython3",
148+
"version": "3.12.2"
149+
}
150+
},
151+
"nbformat": 4,
152+
"nbformat_minor": 5
153+
}

string_mutations.ipynb

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,37 @@
9696
" # Print the mutated string\n",
9797
" print(\"Mutated string:\", s_new)"
9898
]
99+
},
100+
{
101+
"cell_type": "code",
102+
"execution_count": 1,
103+
"id": "8f9a8bbf-d4f7-4b87-b3cd-d272c4559a1a",
104+
"metadata": {},
105+
"outputs": [
106+
{
107+
"name": "stdout",
108+
"output_type": "stream",
109+
"text": [
110+
"Selected Jupyter core packages...\n",
111+
"IPython : 8.25.0\n",
112+
"ipykernel : 6.28.0\n",
113+
"ipywidgets : 8.1.2\n",
114+
"jupyter_client : 8.6.0\n",
115+
"jupyter_core : 5.5.0\n",
116+
"jupyter_server : 2.10.0\n",
117+
"jupyterlab : 4.2.3\n",
118+
"nbclient : 0.8.0\n",
119+
"nbconvert : 7.10.0\n",
120+
"nbformat : 5.9.2\n",
121+
"notebook : 7.2.1\n",
122+
"qtconsole : 5.5.1\n",
123+
"traitlets : 5.14.3\n"
124+
]
125+
}
126+
],
127+
"source": [
128+
"!jupyter --version"
129+
]
99130
}
100131
],
101132
"metadata": {

0 commit comments

Comments
 (0)