COMP1001 LAB5.ipynb
COMP1001 LAB5.ipynb
COMP1001 LAB5.ipynb
"cells": [
{
"cell_type": "markdown",
"id": "3ee28378-060b-49da-aaea-bd25d30d0f98",
"metadata": {},
"source": [
"# COMP 1001 - Introduction to Programming\n",
"# LAB5 - 16.11.2023\n",
"https://colab.research.google.com/"
]
},
{
"cell_type": "markdown",
"id": "d954fb85-7828-47b2-8e28-4af37c1a1e6d",
"metadata": {},
"source": [
"# Exercise 1\n",
"Assume that the variable **`date = \"16 November 2023\"`**, and write code
that does the following using the string functions for this variable:\n",
"\n",
"- find the count of **2** \n",
"- write as **16 November 2023** using the replace function \n",
"- find the index of the letter '**r**'\n",
"- convert into uppercase\n",
"- convert into lowercase"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "083c2824-b847-4ec1-a927-57d0e83a6793",
"metadata": {},
"outputs": [],
"source": [
"date = \"16 November 2023\"\n",
"#student code\n"
]
},
{
"cell_type": "markdown",
"id": "74edc8eb-8dd1-40eb-a335-24046aa6ceb5",
"metadata": {},
"source": [
"# Exercise 2: \n",
"\n",
"The following Python code that stores a string:\n",
"\n",
"**`value = 'accuracy-score:53.7496'`**\n",
"\n",
"Use find function and string slicing to extract the portion of the string
after the\n",
"colon character and then use the float function to convert the extracted\n",
"string into a floating point number."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "7e484480-e54e-4cb1-a599-1a2c8e9b34c2",
"metadata": {},
"outputs": [],
"source": [
"#student code "
]
},
{
"cell_type": "markdown",
"id": "18f05231-a265-4b70-8bd2-a97e7b176daf",
"metadata": {},
"source": [
"# Exercise 3\n",
"\n",
"In this exercise, you will write the code for the functions described below.\
n",
"\n",
"The functions:\n",
"- **`count_character(text, ch)`**: Given a string and a single-character
string, returns the number of times the second string appears in the first string.\
n",
"\n",
"- **`remove_digits(text)`**: Returns a new string that is the same as the
given string, but with digits removed.\n",
"\n",
"- **`is_long(text)`**: Given a string, if the string has more than 15
characters, returns the string 'long text'. Otherwise, returns the string 'short
text'.\n",
"\n",
"\n",
"**Sample Output:**\n",
"```\n",
"the givent text: COMP1001 - Introduction to Programming\n",
"count of the character n: 2\n",
"removing digits: COMP - Introduction to Programming\n",
"is long? : the length is 36 so it is long text\n",
"```"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "a178b4fb-2c0f-4286-9755-05c9a16ffdc5",
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"def count_character(text, ch):\n",
" # student code\n",
"\n",
"\n",
"def remove_digits(text):\n",
" # student code\n",
"\n",
"\n",
"def is_long(text):\n",
" # student code\n",
" \n",
"\n",
"# test your functions"
]
},
{
"cell_type": "markdown",
"id": "d020858e-fa32-4783-bca5-be69efd3f2ae",
"metadata": {},
"source": [
"# Exercise 4\n",
"Write a program that reads 5 integer numbers from the user and stores them a
list named `numbers`. Check the following statements for the list:\n",
"\n",
"- `numbers[1:3]`\n",
"- `numbers[2:]`\n",
"- `numbers[:]`\n",
"- `max(numbers)`\n",
"- `min(numbers)`\n",
"- `sum(numbers)`\n",
"- `len(numbers)`"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "ec953935-28bc-49d9-93d4-5a546b3f7001",
"metadata": {},
"outputs": [],
"source": [
"#Student Code"
]
},
{
"cell_type": "markdown",
"id": "f3953a43-1153-4b79-969c-81025b852ca6",
"metadata": {},
"source": [
"# Exercise 5\n",
"Write a function that takes a list of positive integer numbers and replaces
all occurences of the maximum value with `-1`.\n",
"\n",
"For example, the sample list is `[6, 4, 7, 9, 5, 3, 8, 2, 6, 1, 7, 2, 8, 6, 9,
4, 9]`\n",
"\n",
"\n",
"**Sample Output**\n",
"```\n",
"[6, 4, 7, -1, 5, 3, 8, 2, 6, 1, 7, 2, 8, 6, -1, 4, -1]\n",
"```"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "1262d0b2-69cb-4e9f-8aa1-5a9b5246aedc",
"metadata": {},
"outputs": [],
"source": [
"#Student Code"
]
},
{
"cell_type": "markdown",
"id": "7d6b86a6-f962-435c-b84f-211758ca3226",
"metadata": {},
"source": [
"# Exercise 6\n",
"Bring the given list to the desired format.\n",
"\n",
"**liste=[[1,2,3],[4,5,6,7,8],[9,10,11,12,13,14,15]]**\n",
"```\n",
"new_list= [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15]\n",
"```"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "7b227c13-842c-472d-996b-d7b66dbe7590",
"metadata": {},
"outputs": [],
"source": [
"#Student code"
]
}
],
"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.4"
}
},
"nbformat": 4,
"nbformat_minor": 5
}