Skip to content

Instantly share code, notes, and snippets.

@aahnik
Last active March 21, 2023 10:09
Show Gist options
  • Save aahnik/e6bf31b99e5930d5886f0d5f83cde494 to your computer and use it in GitHub Desktop.
Save aahnik/e6bf31b99e5930d5886f0d5f83cde494 to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"from typing import List"
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"Create a list with 1000 values -> 1,2,3,1,2,3,1,2,3,1,2...\n"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"\n",
"my_list = [1,2,3]*(1000//3) + [1]\n"
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"Create a function taking in 2 arguments. Function has to return a list containing the corresponding next ten Fibonacci numbers (Recursive approach)\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[22, 34, 56, 90, 146, 236, 382, 618, 1000, 1618]"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# recursive implementation\n",
"\n",
"def gen_fibos(a:int,b:int,n:int)->List[int]:\n",
" fibos = [0]*n\n",
" def gen_fibos_impl(a,b,i):\n",
" if i == n:\n",
" return\n",
" fibos[i] = a + b \n",
" gen_fibos_impl(b,fibos[i],i+1)\n",
" gen_fibos_impl(a,b,0)\n",
" return fibos \n",
"\n",
"gen_fibos(10,12, 10)\n"
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"Given two integer numbers, return their product only if the product is equal to or lower than 1000, else return their sum.\n"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"(30, 2000)"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"op = lambda a,b: a*b if a*b < 1000 else a+b\n",
"op(5,6),op(1000,1000)"
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"Write a program to accept a string from the user and display characters that are present at an even index number."
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"('hello', 'hlo')"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"a = input(\"enter string: \")\n",
"even_chars = lambda a: \"\".join([ a[i] for i in range(len(a)) if i%2==0])\n",
"a,even_chars(a)"
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"Write a program to remove the first n characters from a string.\n"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'estring'"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"a = \"somestring\"\n",
"n = 3\n",
"a[n:]"
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"Write a program to check if the given number is a palindrome number."
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"(True, False)"
]
},
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"num = 12321\n",
"num2 = 123\n",
"check_pal = lambda num: str(num) == (str(num))[::-1]\n",
"check_pal(num),check_pal(num2)\n"
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"Given a two list of numbers, write a program to create a new list such that the new list should contain odd numbers from the first list and even numbers from the second list.\n"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[1, 3, 5, 7, 9, 12, 14, 16, 18, 20]"
]
},
"execution_count": 8,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"even = lambda a: a%2==0\n",
"odd = lambda a: a%2!=0\n",
"l1 = [1,2,3,4,5,6,7,8,9,10]\n",
"l2 = [11,12,13,14,15,16,17,18,19,20]\n",
"list(filter(odd,l1)) + list(filter(even,l2))\n"
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"Merge 2 lists into a single dictionary - one list is the keys other is the values\n"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{1: 'a', 2: 'b', 3: 'c'}"
]
},
"execution_count": 9,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"keys = [1,2,3]\n",
"values = ['a','b','c']\n",
"dict(zip(keys,values))"
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"Given a list of keys remove their corresponding key-value pairs from the dictionary\n"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"({65: 'A',\n",
" 67: 'C',\n",
" 68: 'D',\n",
" 69: 'E',\n",
" 70: 'F',\n",
" 71: 'G',\n",
" 72: 'H',\n",
" 74: 'J',\n",
" 75: 'K',\n",
" 77: 'M',\n",
" 80: 'P',\n",
" 81: 'Q',\n",
" 82: 'R',\n",
" 84: 'T',\n",
" 86: 'V',\n",
" 87: 'W',\n",
" 88: 'X',\n",
" 90: 'Z'},\n",
" ['B', 'I', 'L', 'N', 'O', 'S', 'U', 'Y'])"
]
},
"execution_count": 10,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"arr = [i for i in range(65,65+26)]\n",
"chars = list(map(chr,arr))\n",
"data = dict(zip(arr,chars))\n",
"to_remove = [66,73,76,78,79,83,85,89]\n",
"removed = [data.pop(i) for i in to_remove]\n",
"data,removed\n"
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"Write a Python program to check if a value exists in a dictionary.\n"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"('exists', 'does not exist')"
]
},
"execution_count": 11,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# if a key exists\n",
"exists = lambda key: \"exists\" if data.get(key)!=None else \"does not exist\"\n",
"exists(84),exists(83)"
]
},
{
"cell_type": "code",
"execution_count": 24,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 24,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# if a value exists\n",
"'M' in data.values()"
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"Explore Nested dictionaries."
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {},
"outputs": [],
"source": [
"my_data = {\n",
" 1: \"hello\",\n",
" 2: {\n",
" \"cat\": \"dog\",\n",
" 2: {\n",
" 1:1,2:2\n",
" }\n",
" }\n",
"}"
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"Swap two tuples in Python"
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"(1, 2, 3) ('p', 'q', 'r')\n",
"('p', 'q', 'r') (1, 2, 3)\n"
]
}
],
"source": [
"a = (1,2,3)\n",
"b = ('p','q','r')\n",
"print(a,b)\n",
"a,b = b,a\n",
"print(a,b)\n"
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"Count the number of occurrences of an item in a tuple.\n"
]
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"5"
]
},
"execution_count": 14,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"a = (1,1,1,1,1,3,4,5,6,7,77,7,77,7,0)\n",
"a.count(1)"
]
},
{
"cell_type": "code",
"execution_count": 15,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"(True, False)"
]
},
"execution_count": 15,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"a = (1,1,1)\n",
"b = (1,1,1)\n",
"c = (1,2,3)\n",
"a == b , a == c"
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"Create a string made of the first, middle and last character of a given string.\n"
]
},
{
"cell_type": "code",
"execution_count": 16,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"('hlo', 'acd')"
]
},
"execution_count": 16,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"stringer = lambda string: string[0]+string[len(string)//2]+string[-1]\n",
"stringer(\"hello\"),stringer(\"abcd\")"
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"Given 2 strings, insert string 1 in the middle of string 2.\n"
]
},
{
"cell_type": "code",
"execution_count": 17,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'abhellocd'"
]
},
"execution_count": 17,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"midsert = lambda s1,s2: s2[:len(s2)//2] + s1 + s2[len(s2)//2:]\n",
"midsert(\"hello\", \"abcd\")"
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"Rearrange a string in its alphabetical order."
]
},
{
"cell_type": "code",
"execution_count": 18,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[' ', 'e', 'g', 'h', 'i', 'l', 'l', 'n', 'o', 'r', 's', 't']"
]
},
"execution_count": 18,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"s = \"hello string\"\n",
"sorted(s)"
]
},
{
"cell_type": "code",
"execution_count": 19,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"(13, 13, 13, 7, 1, 7, 7, 13, 13, 5, 2, 2, 13, 7, 13, 7, 7, 7, 13, 13, 13, 2, 2, 5, 1, 2, 1, 13, 13, 13, 5, 5, 5, 2)\n"
]
}
],
"source": [
"s = \"aaabnbbaafidababbbaaasdfjieaaafffs\"\n",
"freq = {}\n",
"for i in s:\n",
" if freq.get(i)!=None:\n",
" freq[i]+=1\n",
" else:\n",
" freq[i] = 1\n",
"histogram = []\n",
"for i in s:\n",
" histogram.append(freq[i])\n",
"\n",
"print(tuple(histogram)) # tuple just for printing in single line, no other significance\n",
"\n"
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"Find all occurrences of a substring in a given string by ignoring the case\n"
]
},
{
"cell_type": "code",
"execution_count": 20,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"5"
]
},
"execution_count": 20,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"given_string = \"hello my name is str str and STR and str and sTr and what not\"\n",
"given_string.lower().count(\"str\")"
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"given a string find the sum of all digits in that string -> 'he110' output -> 2\n"
]
},
{
"cell_type": "code",
"execution_count": 21,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"4"
]
},
"execution_count": 21,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"string = \"he11011\"\n",
"digi_sum = lambda string: sum([int(i) for i in string if i.isdigit()])\n",
"digi_sum(string)"
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"Create an Addition function with any default arguments.\n"
]
},
{
"cell_type": "code",
"execution_count": 22,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"(11, 0)"
]
},
"execution_count": 22,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"def add(a = 0, b = 0):\n",
" return a+b\n",
"\n",
"add(1,10),add()"
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"Explore what all can be returned in a python function.\n"
]
},
{
"cell_type": "code",
"execution_count": 23,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"hello 1\n"
]
}
],
"source": [
"# you can even return functions in python functions\n",
"def hello():\n",
" def what(a):\n",
" print(\"hello\",a)\n",
" return what\n",
"\n",
"b = hello()\n",
"b(1)\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": ".venv",
"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.2"
},
"orig_nbformat": 4
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment