Skip to content

Instantly share code, notes, and snippets.

@NeganSmith92
Created February 19, 2024 06:02
Show Gist options
  • Save NeganSmith92/1c486bdd8f4537c3acd0dae0ceaf618e to your computer and use it in GitHub Desktop.
Save NeganSmith92/1c486bdd8f4537c3acd0dae0ceaf618e to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 3,
"id": "533fbd52",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"#fff\n"
]
}
],
"source": [
"#EXERCISE_1\n",
"x = [1, 'Hello', True, 1.093E-3, '#fff']\n",
"print(x[-1])"
]
},
{
"cell_type": "code",
"execution_count": 7,
"id": "539e71f9",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"4\n",
"1\n",
"4\n",
"5\n"
]
}
],
"source": [
"#EXERCISE_2\n",
"x = [9, 3, 4, 1, 4, 5, 6, 3, 7, 8]\n",
"for i in x[2:6]:\n",
" print(i)"
]
},
{
"cell_type": "code",
"execution_count": 21,
"id": "6b9e0737",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"1\n",
" 2\n",
" 3\n",
" 4\n",
" 5\n",
" 6\n",
" 7\n",
"8\n"
]
}
],
"source": [
"#EXERCISE_3\n",
"x = [1, [2, [3], 4,[5]], [[[6]]], [7], 8]\n",
"for i in x:\n",
" if isinstance(i, list):\n",
" for j in i:\n",
" if isinstance(j, list):\n",
" for k in j:\n",
" if isinstance(k, list):\n",
" for y in k:\n",
" print(' ', y)\n",
" else:\n",
" print(' ', k)\n",
" else:\n",
" print(' ', j)\n",
" else:\n",
" print(i)"
]
},
{
"cell_type": "code",
"execution_count": 22,
"id": "aa877182",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[1, 3, 4, 5]\n",
"['a', 2, 'b', 'c']\n",
"[1, 3, 4, 5, 'a', 2, 'b', 'c']\n"
]
}
],
"source": [
"#EXERCISE_4\n",
"x = [1, 2, 3, 4, 5]\n",
"y = ['a', 'b', 'c']\n",
"z = x.pop(1)\n",
"y.insert(1, z)\n",
"z = x + y\n",
"\n",
"print(x)\n",
"print(y)\n",
"print(z)"
]
},
{
"cell_type": "code",
"execution_count": 23,
"id": "d801f921",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"['a', 2000, 'i', 'o', 'u']\n",
"['a', 'e', 'i', 'o', 'u']\n"
]
}
],
"source": [
"#EXERCISE_5\n",
"a = 'aeiou'\n",
"b = tuple(a)\n",
"c = list(b)\n",
"d = c\n",
"e = list(d)\n",
"\n",
"c[1] = 2000\n",
"print(d)\n",
"print(e)"
]
},
{
"cell_type": "code",
"execution_count": 28,
"id": "cffcb393",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[0, 1, 2, 3, 4, 5]\n",
"[2, 4, 3, 1, 0, 5]\n",
"[5, 4, 3, 2, 1, 0]\n",
"[5, 4, 3, 2, 1, 0]\n"
]
}
],
"source": [
"#EXERCISE_6\n",
"#To get [0, 1, 2, 3, 4, 5]\n",
"x = sorted ([5, 0, 1, 3, 4, 2])\n",
"print(x)\n",
"\n",
"#To get [2, 4, 3, 1, 0, 5]\n",
"y = [5, 0, 1, 3, 4, 2][::-1]\n",
"print(y)\n",
"\n",
"#To get [5, 4, 3, 2, 1, 0]\n",
"z = sorted([5, 0, 1, 3, 4, 2], reverse=True)\n",
"print(z)\n",
"\n",
"#To get [5, 4, 3, 2, 1, 0] in two lines\n",
"zz = [5, 0, 1, 3, 4, 2]\n",
"yz = list(reversed(sorted(zz)))\n",
"print(yz)"
]
},
{
"cell_type": "code",
"execution_count": 37,
"id": "9dd5488b",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"count of 1: 4\n",
"\n",
"1st index of 1: 1\n",
"\n",
"2nd index of 1: 4\n",
"\n"
]
}
],
"source": [
"#EXERCISE_7\n",
"x = [0, 1, 2, 3, 1, 4, 5, 6, 1, 9, 9, 1]\n",
"\n",
"print('count of 1:', x.count(1))\n",
"print()\n",
"\n",
"c0 = x.index(1)\n",
"print('1st index of 1:', c0)\n",
"print()\n",
"\n",
"#wrongsyntax=c1 = x[c0 + 1:].index(1)\n",
"#correction=\n",
"c1 = x.index(1,c0 + 1)\n",
"print('2nd index of 1:', c1)\n",
"print()"
]
},
{
"cell_type": "code",
"execution_count": 38,
"id": "2655c8eb",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[0, 2, 4]\n",
"[1, 3, 5]\n",
"[1, 3, 5, 3, 5, 7, 5, 7, 9]\n"
]
}
],
"source": [
"#EXERCISE_8\n",
"x = [i for i in range (0,5,2)]\n",
"y = [i for i in range (1,6,2)]\n",
"z = [i+j for i in x for j in y]\n",
"\n",
"print(x)\n",
"print(y)\n",
"print(z)"
]
},
{
"cell_type": "code",
"execution_count": 42,
"id": "73d29450",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"original\n",
"[1, 5, 2, 4, 3] \n",
"\n",
"after modified descending bubble sort\n",
"[5, 4, 3, 2, 1]\n"
]
}
],
"source": [
"#EXERCISE_9\n",
"x = [1, 5, 2, 4, 3]\n",
"\n",
"print('original'); print(x, '\\n')\n",
"\n",
"N = len(x)\n",
"\n",
"for i in range(N):\n",
" for j in range(i+1, N):\n",
" if x[i] < x[j]:\n",
" x[i], x[j] = x[j], x[i]\n",
"\n",
"print('after modified descending bubble sort'); print(x)\n"
]
},
{
"cell_type": "code",
"execution_count": 46,
"id": "6df7b6b4",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"['a', 1, 'b', 2, 'c', 3] \n",
"\n",
"['a', 'b', 'c'] \n",
"\n",
"[1, 2, 3]\n"
]
}
],
"source": [
"#EXERCISE_10\n",
"def sep_odd_even_index(z):\n",
" N = len(z)\n",
" r = range(N)\n",
" x = [z[i] for i in r if i % 2 == 0]\n",
" y = [z[i] for i in r if i % 2 == 1]\n",
" return [x, y]\n",
"\n",
"mix = ['a', 1, 'b', 2, 'c', 3]\n",
"x, y = sep_odd_even_index(mix)\n",
"print(mix, '\\n')\n",
"print(x, '\\n')\n",
"print(y)\n",
"\n",
"#The purpose of the sep_odd_even_index function is to separate the elements of a list into two sublists based on their indices (even and odd). Heres a breakdown of how it works:\n",
"# 1. Input:\n",
"# The function takes a single argument z, which is the list to be separated.\n",
"# 2. Steps:\n",
"# Calculate list length: The function first calculates the length of the input list z and stores it in the variable N.\n",
"# Generate index range: It creates a range of numbers from 0 to N-1 using range(N). This range represents the indices of the elements in the list.\n",
"# Separate elements based on even/odd indices:\n",
"# The function uses list comprehension to create two sublists:\n",
"# x: This sublist contains elements from the original list z at even indices (0, 2, 4, ...). The condition i % 2 == 0 in the list comprehension filters the indices to select only even numbers.\n",
"# y: This sublist contains elements from the original list z at odd indices (1, 3, 5, ...). The condition i % 2 == 1 selects only odd indices.\n",
"# Return: The function returns a list containing the two sublists x and y.\n",
"# the output will be :\n",
"# ['a', 1, 'b', 2, 'c', 3] \n",
"# ['a', 'b', 'c'] \n",
"# [1, 2, 3]"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "7569025a",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"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.11.5"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment