Skip to content

Instantly share code, notes, and snippets.

@sant-sh
Created June 28, 2017 07:32
Show Gist options
  • Save sant-sh/2ba1e0a72ee9e069e17f191c0cb19ffd to your computer and use it in GitHub Desktop.
Save sant-sh/2ba1e0a72ee9e069e17f191c0cb19ffd to your computer and use it in GitHub Desktop.
Assignment
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# 1. To Check Harshad No"
]
},
{
"cell_type": "code",
"execution_count": 55,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 55,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"def isHarshadNumber(n):\n",
" sum,to_check = 0,n\n",
" while n:\n",
" sum += n%10\n",
" n = n//10\n",
" if to_check % sum == 0:\n",
" return True\n",
" else:\n",
" print (\"That's not a Harshad No, kindly try again !\")\n",
"\n",
"isHarshadNumber(81) "
]
},
{
"cell_type": "markdown",
"metadata": {
"collapsed": true
},
"source": [
"<p> -------------------- End --------------------- </p>"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# 2. To Print Mixed Case"
]
},
{
"cell_type": "code",
"execution_count": 52,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"treehOUSE"
]
}
],
"source": [
"def mixedcase(input_string):\n",
" count = (len(input_string)+1)\n",
" pointer = int(count/2)\n",
" list = []\n",
" for letter in input_string:\n",
" list.append(letter)\n",
" lower = (list[:pointer])\n",
" for l in lower:\n",
" print (l.lower(), end='')\n",
" \n",
" upper = (list[pointer:])\n",
" for u in upper:\n",
" print (u.upper(), end='')\n",
" \n",
"mixedcase('Treehouse')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<p> -------------------- End --------------------- </p>"
]
},
{
"cell_type": "markdown",
"metadata": {
"collapsed": true
},
"source": [
"# 3. to Count the number of items"
]
},
{
"cell_type": "code",
"execution_count": 75,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Counter({'a': 3, 'b': 2, 'c': 2})\n"
]
}
],
"source": [
"from collections import Counter\n",
"\n",
"my_list = ['a', 'b', 'a', 'c', 'b', 'a', 'c']\n",
"c = Counter(my_list) \n",
"print (c)"
]
},
{
"cell_type": "markdown",
"metadata": {
"collapsed": true
},
"source": [
"<p> -------------------- End --------------------- </p>"
]
},
{
"cell_type": "markdown",
"metadata": {
"collapsed": true
},
"source": [
"# 4. List comprehension for solving the equation (Y=X2+1)"
]
},
{
"cell_type": "code",
"execution_count": 137,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[15,\n",
" 16,\n",
" 17,\n",
" 18,\n",
" 19,\n",
" 13,\n",
" 14,\n",
" 15,\n",
" 16,\n",
" 17,\n",
" 11,\n",
" 12,\n",
" 13,\n",
" 14,\n",
" 15,\n",
" 9,\n",
" 10,\n",
" 11,\n",
" 12,\n",
" 13,\n",
" 7,\n",
" 8,\n",
" 9,\n",
" 10,\n",
" 11,\n",
" 5,\n",
" 6,\n",
" 7,\n",
" 8,\n",
" 9,\n",
" 3,\n",
" 4,\n",
" 5,\n",
" 6,\n",
" 7,\n",
" 1,\n",
" 2,\n",
" 3,\n",
" 4,\n",
" 5,\n",
" -1,\n",
" 0,\n",
" 1,\n",
" 2,\n",
" 3,\n",
" -3,\n",
" -2,\n",
" -1,\n",
" 0,\n",
" 1]"
]
},
"execution_count": 137,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"[y-x*2 for x in range(-5,5) for y in range(5,10)]"
]
},
{
"cell_type": "markdown",
"metadata": {
"collapsed": true
},
"source": [
"\n",
"<p> -------------------- End --------------------- </p>"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# 5. List that contains only the elements that are common"
]
},
{
"cell_type": "code",
"execution_count": 146,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[1, 1, 2, 3, 5, 8, 13]\n"
]
}
],
"source": [
"a = [1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89]\n",
"b = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]\n",
"\n",
"output = []\n",
"for i in a:\n",
" for j in b:\n",
" if i==j:\n",
" output.append(i)\n",
"print (output)"
]
},
{
"cell_type": "markdown",
"metadata": {
"collapsed": true
},
"source": [
"<p> -------------------- End --------------------- </p>"
]
}
],
"metadata": {
"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.5.2"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment