0% found this document useful (0 votes)
24 views4 pages

1000 Python Practice Problems - Ipynb

The document contains a series of Python practice problems designed to enhance programming skills. Each problem includes a description, the corresponding code implementation, and the output results. Topics covered include finding the second highest number, computing factorials, reversing lists, and identifying duplicates in lists.

Uploaded by

sbaidya230
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views4 pages

1000 Python Practice Problems - Ipynb

The document contains a series of Python practice problems designed to enhance programming skills. Each problem includes a description, the corresponding code implementation, and the output results. Topics covered include finding the second highest number, computing factorials, reversing lists, and identifying duplicates in lists.

Uploaded by

sbaidya230
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 4

{"cells":[{"source":"<a href=\"https://www.kaggle.

com/code/aayushsin7a/1000-python-
practice-problems?scriptVersionId=170969359\" target=\"_blank\"><img align=\"left\"
alt=\"Kaggle\" title=\"Open in Kaggle\"
src=\"https://kaggle.com/static/images/open-in-kaggle.svg\"></a>","metadata":
{},"cell_type":"markdown"},{"cell_type":"markdown","id":"1f9f2703","metadata":
{"papermill":{"duration":0.007153,"end_time":"2024-04-
08T11:46:45.539681","exception":false,"start_time":"2024-04-
08T11:46:45.532528","status":"completed"},"tags":[]},"source":["# List Series "]},
{"cell_type":"markdown","id":"fc633fb7","metadata":{"papermill":
{"duration":0.005874,"end_time":"2024-04-
08T11:46:45.552066","exception":false,"start_time":"2024-04-
08T11:46:45.546192","status":"completed"},"tags":[]},"source":["# 1. Find the
second highest number in a list\n","- Steps define a function(assign a variable to
be passed in the function)\n","- convert the list into a set to remove duplicates
if any\n","- sort it \n","- retrieve second last element "]},
{"cell_type":"code","execution_count":1,"id":"0a4cd8e3","metadata":
{"_cell_guid":"b1076dfc-b9ad-4769-8c92-
a6c4dae69d19","_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","execution":
{"iopub.execute_input":"2024-04-08T11:46:45.566298Z","iopub.status.busy":"2024-04-
08T11:46:45.56565Z","iopub.status.idle":"2024-04-
08T11:46:45.580256Z","shell.execute_reply":"2024-04-
08T11:46:45.578968Z"},"papermill":{"duration":0.024552,"end_time":"2024-04-
08T11:46:45.582686","exception":false,"start_time":"2024-04-
08T11:46:45.558134","status":"completed"},"tags":[]},"outputs":
[{"name":"stdout","output_type":"stream","text":["Second Highest Number : - 411\
n"]}],"source":["def second_highest(numbers):\n"," numbers = list(set(numbers))\
n"," numbers.sort()\n"," return numbers[-2]\n","\n","numbers =
[100,200,312,1223,411,90,123,234,241,53,87]\n","print(\"Second Highest Number :
-\",second_highest(numbers))"]},{"cell_type":"markdown","id":"83c3dfdb","metadata":
{"papermill":{"duration":0.005945,"end_time":"2024-04-
08T11:46:45.595077","exception":false,"start_time":"2024-04-
08T11:46:45.589132","status":"completed"},"tags":[]},"source":["# 2. Write a
function to compute a factorial of a number using recursion "]},
{"cell_type":"code","execution_count":2,"id":"cd3542e9","metadata":{"execution":
{"iopub.execute_input":"2024-04-08T11:46:45.609346Z","iopub.status.busy":"2024-04-
08T11:46:45.608971Z","iopub.status.idle":"2024-04-
08T11:46:45.615341Z","shell.execute_reply":"2024-04-
08T11:46:45.61414Z"},"papermill":{"duration":0.016636,"end_time":"2024-04-
08T11:46:45.617986","exception":false,"start_time":"2024-04-
08T11:46:45.60135","status":"completed"},"tags":[]},"outputs":
[{"name":"stdout","output_type":"stream","text":["Factorial of 5 is :- 120\
n"]}],"source":["def factorial(num):\n"," if num == 0:\n"," return 1 \
n"," return num * factorial(num - 1)\n","num = 5\n","print(f'Factorial of {num}
is :- ',factorial(num))\n"]},{"cell_type":"markdown","id":"0a3df932","metadata":
{"execution":{"iopub.execute_input":"2024-04-
08T08:51:10.13604Z","iopub.status.busy":"2024-04-
08T08:51:10.135689Z","iopub.status.idle":"2024-04-
08T08:51:10.155129Z","shell.execute_reply":"2024-04-
08T08:51:10.154223Z","shell.execute_reply.started":"2024-04-
08T08:51:10.136014Z"},"papermill":{"duration":0.006097,"end_time":"2024-04-
08T11:46:45.630543","exception":false,"start_time":"2024-04-
08T11:46:45.624446","status":"completed"},"tags":[]},"source":["# 3. Reverse a
List: Write a function to reverse a given list."]},
{"cell_type":"code","execution_count":3,"id":"914312e7","metadata":{"execution":
{"iopub.execute_input":"2024-04-08T11:46:45.645144Z","iopub.status.busy":"2024-04-
08T11:46:45.644745Z","iopub.status.idle":"2024-04-
08T11:46:45.651435Z","shell.execute_reply":"2024-04-
08T11:46:45.650308Z"},"papermill":{"duration":0.017296,"end_time":"2024-04-
08T11:46:45.654223","exception":false,"start_time":"2024-04-
08T11:46:45.636927","status":"completed"},"tags":[]},"outputs":
[{"name":"stdout","output_type":"stream","text":["Reverse of python is : nohtyp\
n"]}],"source":["def reverseString(str):\n"," if str == '':\n","
print('Nothing 2 return')\n"," return str\n"," return str[::-1]\n","var2
= 'python'\n","print(f'Reverse of {var2} is :',reverseString(var2))\n"]},
{"cell_type":"markdown","id":"f96dac95","metadata":{"papermill":
{"duration":0.006146,"end_time":"2024-04-
08T11:46:45.666893","exception":false,"start_time":"2024-04-
08T11:46:45.660747","status":"completed"},"tags":[]},"source":["# 4. Find the
Maximum and Minimum Element in a List: Write a function to find the maximum and
minimum elements in a list."]},
{"cell_type":"code","execution_count":4,"id":"ad02b2c5","metadata":{"execution":
{"iopub.execute_input":"2024-04-08T11:46:45.681388Z","iopub.status.busy":"2024-04-
08T11:46:45.681025Z","iopub.status.idle":"2024-04-
08T11:46:45.689809Z","shell.execute_reply":"2024-04-
08T11:46:45.688696Z"},"papermill":{"duration":0.018905,"end_time":"2024-04-
08T11:46:45.692177","exception":false,"start_time":"2024-04-
08T11:46:45.673272","status":"completed"},"tags":[]},"outputs":
[{"name":"stdout","output_type":"stream","text":["Minimum element of the given list
is 7\n","Maximum element of the given list is 40\n"]}],"source":["def
MinMax(lst):\n"," if not lst:\n"," return 'Empty list'\n"," #
Initialize min and max with the first element of the list\n"," min_element =
lst[0]\n"," max_element = lst[0]\n"," for item in lst:\n"," if
isinstance(item, (int, float)):\n"," if item < min_element:\n","
min_element = item\n"," if item > max_element:\n","
max_element = item\n"," else:\n"," return 'Invalid list elements,
Non-numeric list'\n"," return min_element,max_element\n","\n","list_a =
[10,20,30,40,12,31,8,7,11]\n","min_element, max_element = MinMax(list_a)\
n","print(\"Minimum element of the given list is \",min_element)\
n","print(\"Maximum element of the given list is \",max_element)"]},
{"cell_type":"markdown","id":"a2a14a3e","metadata":{"papermill":
{"duration":0.006338,"end_time":"2024-04-
08T11:46:45.705206","exception":false,"start_time":"2024-04-
08T11:46:45.698868","status":"completed"},"tags":[]},"source":["# 5. Sum of
Elements in a List: Write a function to calculate the sum of all elements in a
list."]},{"cell_type":"code","execution_count":5,"id":"bc3bb6b2","metadata":
{"execution":{"iopub.execute_input":"2024-04-
08T11:46:45.720338Z","iopub.status.busy":"2024-04-
08T11:46:45.719524Z","iopub.status.idle":"2024-04-
08T11:46:45.727988Z","shell.execute_reply":"2024-04-
08T11:46:45.726869Z"},"papermill":{"duration":0.018892,"end_time":"2024-04-
08T11:46:45.730617","exception":false,"start_time":"2024-04-
08T11:46:45.711725","status":"completed"},"tags":[]},"outputs":
[{"name":"stdout","output_type":"stream","text":["Sum of elements: 45\n","Sum of
elements: Empty List\n","Sum of elements: Invalid list\n"]}],"source":["def
sum_of_elements(lst):\n"," if not lst:\n"," return 'Empty List'\n","
total_sum = 0\n"," for item in lst:\n"," if isinstance(item,
(int,float)):\n"," total_sum += item\n"," else:\n","
return 'Invalid list'\n"," return total_sum\n","\n","x = [1,2,3,4,5,6,7,8,9]
\n","print(\"Sum of elements:\", sum_of_elements(x))\n","# Test with an empty list\
n","print(\"Sum of elements:\", sum_of_elements([]))\n","\n","# Test with a list
containing non-numeric values\n","print(\"Sum of elements:\", sum_of_elements([1,
'a', 3, 4]))"]},{"cell_type":"markdown","id":"1d510d2e","metadata":{"papermill":
{"duration":0.006487,"end_time":"2024-04-
08T11:46:45.743979","exception":false,"start_time":"2024-04-
08T11:46:45.737492","status":"completed"},"tags":[]},"source":["# 6. Find Common
Elements in Two Lists: Write a function to find the common elements between two
lists."]},{"cell_type":"code","execution_count":6,"id":"44d676e9","metadata":
{"execution":{"iopub.execute_input":"2024-04-
08T11:46:45.759407Z","iopub.status.busy":"2024-04-
08T11:46:45.758982Z","iopub.status.idle":"2024-04-
08T11:46:45.766578Z","shell.execute_reply":"2024-04-
08T11:46:45.765751Z"},"papermill":{"duration":0.018442,"end_time":"2024-04-
08T11:46:45.769204","exception":false,"start_time":"2024-04-
08T11:46:45.750762","status":"completed"},"tags":[]},"outputs":
[{"name":"stdout","output_type":"stream","text":["Common Elements in the given list
: [1, 2, 'A']\n"]}],"source":["def commonElementsInList(lst):\n"," if not lst:\
n"," return 'Empty list'\n"," common_elements = []\n"," for i in
range(len(lst)):\n"," for j in range(i+1,len(lst)):\n"," if
lst[i] == lst[j] and lst[i] not in common_elements:\n","
common_elements.append(lst[i])\n"," return common_elements\n","my_list =
[1,2,3,41,1,2,\"A\",\"B\",\"C\",\"A\",\"b\"]\n","print(\"Common Elements in the
given list :\", commonElementsInList(my_list))"]},
{"cell_type":"markdown","id":"3bb87a17","metadata":{"papermill":
{"duration":0.006526,"end_time":"2024-04-
08T11:46:45.78267","exception":false,"start_time":"2024-04-
08T11:46:45.776144","status":"completed"},"tags":[]},"source":["# 7. Find
Duplicates from a List: Write a function to Find duplicates from a list."]},
{"cell_type":"code","execution_count":7,"id":"9d9ac66a","metadata":{"execution":
{"iopub.execute_input":"2024-04-08T11:46:45.798625Z","iopub.status.busy":"2024-04-
08T11:46:45.797652Z","iopub.status.idle":"2024-04-
08T11:46:45.80715Z","shell.execute_reply":"2024-04-
08T11:46:45.805461Z"},"papermill":{"duration":0.020103,"end_time":"2024-04-
08T11:46:45.809579","exception":false,"start_time":"2024-04-
08T11:46:45.789476","status":"completed"},"tags":[]},"outputs":
[{"name":"stdout","output_type":"stream","text":["Duplicate Elements in the given
list : ['Apple', 'CR7']\n"]}],"source":["def find_dupes(lst):\n","
if not lst:\n"," return 'Empty List'\n"," non_duplicate_list = []\
n"," for i in range(len(lst)):\n"," for j in range(i+1,len(lst)):\n","
if lst[i] == lst[j] and lst[i] not in non_duplicate_list:\n","
non_duplicate_list.append(lst[i])\n"," return non_duplicate_list\
n","my_duplicate_list = ['Apple','mango','Apple','CR7','CR7']\n","print(\"Duplicate
Elements in the given list :\", find_dupes(my_duplicate_list))"]},
{"cell_type":"code","execution_count":8,"id":"aa165dd6","metadata":{"execution":
{"iopub.execute_input":"2024-04-08T11:46:45.825108Z","iopub.status.busy":"2024-04-
08T11:46:45.824678Z","iopub.status.idle":"2024-04-
08T11:46:45.832036Z","shell.execute_reply":"2024-04-
08T11:46:45.830821Z"},"papermill":{"duration":0.018276,"end_time":"2024-04-
08T11:46:45.834696","exception":false,"start_time":"2024-04-
08T11:46:45.81642","status":"completed"},"tags":[]},"outputs":
[{"name":"stdout","output_type":"stream","text":["Duplicate Elements in the given
list : ['Apple', 'mango', 'CR7']\n"]}],"source":["def remove_dupes(lst):\n"," if
not lst:\n"," return 'Empty List'\n"," non_duplicate_list = []\n","
for i in range(len(lst)):\n"," if lst[i] not in non_duplicate_list:\n","
non_duplicate_list.append(lst[i])\n"," return non_duplicate_list\n","\
n","my_duplicate_list = ['Apple', 'mango', 'Apple', 'CR7', 'CR7']\
n","print(\"Duplicate Elements in the given list :\",
remove_dupes(my_duplicate_list))\n"]},
{"cell_type":"markdown","id":"ae2ba046","metadata":{"papermill":
{"duration":0.006618,"end_time":"2024-04-
08T11:46:45.849202","exception":false,"start_time":"2024-04-
08T11:46:45.842584","status":"completed"},"tags":[]},"source":["# 8. Merge Two
Sorted Lists: Write a function to merge two sorted lists into a single sorted
list."]},{"cell_type":"code","execution_count":9,"id":"1cd4640d","metadata":
{"execution":{"iopub.execute_input":"2024-04-
08T11:46:45.86503Z","iopub.status.busy":"2024-04-
08T11:46:45.864615Z","iopub.status.idle":"2024-04-
08T11:46:45.872167Z","shell.execute_reply":"2024-04-
08T11:46:45.870869Z"},"papermill":{"duration":0.018394,"end_time":"2024-04-
08T11:46:45.874594","exception":false,"start_time":"2024-04-
08T11:46:45.8562","status":"completed"},"tags":[]},"outputs":
[{"name":"stdout","output_type":"stream","text":["Sorted array is: [11, 12, 22, 25,
34, 64, 90]\n"]}],"source":["def bubble_sort(arr):\n"," n = len(arr)\n"," #
Traverse through all the elements in the list\n"," for i in range(n):\n","
# Last i elements are already in place, so we don't need to check them again\n","
for j in range(0,n-i-1):\n"," if arr[j]>arr[j+1]:\n","
arr[j],arr[j+1] = arr[j+1],arr[j]\n","\n","my_list = [64, 34, 25, 12, 22, 11, 90]\
n","bubble_sort(my_list)\n","\n","print(\"Sorted array is:\", my_list)"]},
{"cell_type":"code","execution_count":null,"id":"70be3a97","metadata":{"papermill":
{"duration":0.006651,"end_time":"2024-04-
08T11:46:45.888555","exception":false,"start_time":"2024-04-
08T11:46:45.881904","status":"completed"},"tags":[]},"outputs":[],"source":
[]}],"metadata":{"kaggle":{"accelerator":"none","dataSources":
[],"dockerImageVersionId":30646,"isGpuEnabled":false,"isInternetEnabled":true,"lang
uage":"python","sourceType":"notebook"},"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.10.13"},"papermill":{"default_parameters":
{},"duration":3.997848,"end_time":"2024-04-
08T11:46:46.315533","environment_variables":
{},"exception":null,"input_path":"__notebook__.ipynb","output_path":"__notebook__.i
pynb","parameters":{},"start_time":"2024-04-
08T11:46:42.317685","version":"2.5.0"}},"nbformat":4,"nbformat_minor":5}

You might also like