Skip to content

Instantly share code, notes, and snippets.

@afrozhie
Created February 19, 2024 05:28
Show Gist options
  • Save afrozhie/9f4d298c6a417273ba398584aaa8e33c to your computer and use it in GitHub Desktop.
Save afrozhie/9f4d298c6a417273ba398584aaa8e33c to your computer and use it in GitHub Desktop.
exercise 3 19/02/2024
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"id": "7263af42-3a43-4530-8765-4a514535d746",
"metadata": {},
"outputs": [],
"source": [
"Firman Andrian\n",
"21181094"
]
},
{
"cell_type": "code",
"execution_count": 63,
"id": "1ff9f17a-f0c8-4eaa-aa9c-dcea9c42ddad",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"#fff\n"
]
}
],
"source": [
"#Exercise1\n",
"x = [1,'Hello', True, 1.093E-3,'#fff']\n",
"print(x[-1])"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "64d1339a-a2a7-4bee-a458-90184ee62407",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"4\n",
"1\n",
"4\n",
"5\n"
]
}
],
"source": [
"#Exercise2\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": null,
"id": "a5902486-c93d-4d98-9bc2-ae716bb59a81",
"metadata": {},
"outputs": [],
"source": [
"hanya menampilkan elemen ke-2 sampai ke-5 yaitu 4 1 4 5"
]
},
{
"cell_type": "code",
"execution_count": 17,
"id": "e3e35876-8154-480a-af54-63d7749b066e",
"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",
"\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 l in k:\n",
" print(' ', l)\n",
" else:\n",
" print(' ', k)\n",
" else:\n",
" print(' ', j)\n",
" else:\n",
" print(i)"
]
},
{
"cell_type": "code",
"execution_count": 24,
"id": "a914fea9-8c5a-4921-af92-980c26b521bb",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"x: [1, 3, 4, 5]\n",
"y: ['a', 2, 'b', 'c']\n",
"z: [1, 3, 4, 5, 'a', 2, 'b', 'c']\n"
]
}
],
"source": [
"#Exercise4\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:\", x)\n",
"print(\"y:\", y)\n",
"print(\"z:\", z)"
]
},
{
"cell_type": "code",
"execution_count": 25,
"id": "65790a4f-8e2d-4122-9302-7b8fdff4d8a7",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"['a', 2000, 'i', 'o', 'u']\n",
"['a', 'e', 'i', 'o', 'u']\n"
]
}
],
"source": [
"#Exercise5\n",
"a = 'aeiou'\n",
"b = tuple(a)\n",
"c = list(b)\n",
"d = c\n",
"e = list(d)\n",
"c[1] = 2000\n",
"print(d)\n",
"print(e)"
]
},
{
"cell_type": "code",
"execution_count": 33,
"id": "e798ef28-2530-4618-b861-7fe1b79f0e58",
"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": [
"#Exercise6\n",
"#[0, 1, 2, 3, 4, 5]\n",
"result1 = sorted([5, 0, 1, 3, 4, 2])\n",
"print(result1)\n",
"\n",
"#[2, 4, 3, 1, 0, 5]\n",
"result2 = [5, 0, 1, 3, 4, 2][::-1]\n",
"print(result2)\n",
"\n",
"#[5, 4, 3, 2, 1, 0] in one line\n",
"result3 = sorted([5, 0, 1, 3, 4, 2], reverse=True)\n",
"print(result3)\n",
"\n",
"#[5, 4, 3, 2, 1, 0] in two lines\n",
"original_list = [5, 0, 1, 3, 4, 2]\n",
"result4 = list(reversed(sorted(original_list)))\n",
"print(result4)"
]
},
{
"cell_type": "code",
"execution_count": 36,
"id": "4adf05b4-9281-4d98-a82e-9f3f18789664",
"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: 2\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",
"c1 = x[c0 + 1:].index(1)\n",
"print('2nd index of 1:', c1)"
]
},
{
"cell_type": "code",
"execution_count": 37,
"id": "203673db-82e9-46a6-a756-2ddcbcebb4fb",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[0, 2, 4]\n",
"[1, 3, 5]\n",
"[2, 4, 6, 2, 4, 6, 2, 4, 6]\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 = [1+j for i in x for j in y]\n",
"\n",
"print(x)\n",
"print(y)\n",
"print(z)"
]
},
{
"cell_type": "code",
"execution_count": 55,
"id": "fddf9c6e-785b-4cf4-9f44-23e913707e76",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"original\n",
"[1, 5, 2, 4, 3] \n",
"\n",
"after modified bubble sort\n",
"[5, 1, 2, 3, 4]\n"
]
}
],
"source": [
"#Exercise9\n",
"x = [1, 5, 2, 4, 3]\n",
"\n",
"print('original')\n",
"print(x, '\\n')\n",
"\n",
"N = len(x)\n",
"\n",
"for i in range(N):\n",
" for j in range(1, N):\n",
" if x[i] < x[j]:\n",
" x[i], x[j] = x[j], x[i]\n",
"\n",
"print('after modified bubble sort')\n",
"print(x)"
]
},
{
"cell_type": "code",
"execution_count": 62,
"id": "fcb12aef-0ea4-46dd-b567-77516cbf4624",
"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": [
"#Exercise10\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",
"\n",
"print(mix, '\\n')\n",
"print(x, '\\n')\n",
"print(y)\n",
"#Purpose of sep_odd_even_index function:\n",
"#The function takes a list z as input and separates its elements into two lists (x and y)\n",
"#The elements at even indices (starting from 0) are placed in the list x\n",
"#and the elements at odd indices are placed in the list y."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "3ee8bd94-5e26-4c9d-91ab-5004b188e759",
"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