Skip to content

Instantly share code, notes, and snippets.

@htlcnn
Created March 7, 2016 10:40
Show Gist options
  • Save htlcnn/d2dab29681c2d6294c01 to your computer and use it in GitHub Desktop.
Save htlcnn/d2dab29681c2d6294c01 to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# 3.1"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"0b110001000100101000\n",
"1000\n"
]
}
],
"source": [
"input_number = 201000\n",
"bin_input = bin(input_number)\n",
"for index, char in enumerate(bin_input):\n",
" if char == '1':\n",
" last_one_index = index\n",
"output = bin_input[last_one_index:]\n",
"print bin_input\n",
"print output"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# 3.2"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"this is a negative number\n"
]
}
],
"source": [
"input_number = -324\n",
"if input_number < 0:\n",
" print \"this is a negative number\"\n",
"elif input_number == 0:\n",
" print \"this is zero\"\n",
"else:\n",
" print \"this is a positive number\""
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# 3.3"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"maria.ozawa.mp9\n"
]
}
],
"source": [
"full_file_name = 'maria.ozawa.mp9'\n",
"for index, char in enumerate(full_file_name):\n",
" if char == '.':\n",
" last_dot_index = index\n",
" else:\n",
" last_dot_index = len(full_file_name)\n",
"file_name = full_file_name[:last_dot_index]\n",
"print file_name"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# 3.4"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"1 5\n",
"2 6\n",
"3 7\n",
"4 8\n",
"5 9\n",
"6 10\n",
"7 11\n",
"8 12\n",
"9 13\n",
"10 14\n",
"11 15\n"
]
}
],
"source": [
"lst = range(5,16)\n",
"for idx, i in enumerate(lst):\n",
" print idx + 1, i"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# 3.5"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"December 31\n"
]
}
],
"source": [
"input_month = 12\n",
"months = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December']\n",
"days_in_month = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]\n",
"if input_month in range(13):\n",
" print months[input_month-1], days_in_month[input_month-1]\n",
"else:\n",
" print 'Not a month'"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# 3.6"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"['m', 'e', 'u', 'v', 'i']\n"
]
}
],
"source": [
"names = \"cho meo ga chuot vit ngan\"\n",
"print [c for c in names if names.count(c) == 1]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# 3.7"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"0 == 0 * 5\n",
"5 == 1 * 5\n",
"10 == 2 * 5\n",
"15 == 3 * 5\n",
"20 == 4 * 5\n",
"25 == 5 * 5\n",
"30 == 6 * 5\n",
"35 == 7 * 5\n",
"40 == 8 * 5\n",
"45 == 9 * 5\n",
"50 == 10 * 5\n",
"55 == 11 * 5\n",
"60 == 12 * 5\n",
"65 == 13 * 5\n",
"70 == 14 * 5\n",
"75 == 15 * 5\n",
"80 == 16 * 5\n",
"85 == 17 * 5\n",
"90 == 18 * 5\n",
"95 == 19 * 5\n"
]
}
],
"source": [
"for i in range(100):\n",
" if i % 5 == 0:\n",
" print str(i) + ' == ' + str(i/5) + ' * 5' "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# 3.8"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[0, 15, 30, 45, 60, 75, 90, 105, 120, 135, 150, 165, 180, 195, 210, 225, 240, 255, 270, 285, 300, 315, 330, 345, 360, 375, 390, 405, 420, 435, 450, 465, 480, 495, 510, 525, 540, 555, 570, 585, 600, 615, 630, 645, 660, 675, 690, 705, 720, 735, 750, 765, 780, 795, 810, 825, 840, 855, 870, 885, 900, 915, 930, 945, 960, 975, 990]\n",
"33165\n"
]
}
],
"source": [
"lst = [i for i in range(1000) if i % 3 == 0 and i % 5 == 0]\n",
"print lst\n",
"print sum(lst)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# 3.9"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[[1, 9, 1], [2, 8, 1], [3, 7, 1], [4, 6, 1], [5, 5, 1], [6, 4, 1], [6, 8, 2], [6, 9, 2], [7, 3, 1], [7, 6, 2], [7, 7, 2], [7, 9, 3], [8, 2, 1], [8, 4, 2], [8, 5, 2], [8, 6, 3], [8, 7, 3], [8, 8, 3], [8, 8, 4], [8, 9, 4], [9, 1, 1], [9, 2, 2], [9, 3, 2], [9, 3, 3], [9, 4, 3], [9, 4, 4], [9, 5, 3], [9, 5, 4], [9, 5, 5], [9, 6, 4], [9, 6, 5], [9, 6, 6], [9, 7, 4], [9, 7, 5], [9, 7, 6], [9, 7, 7], [9, 8, 5], [9, 8, 6], [9, 8, 7], [9, 8, 8], [9, 9, 5], [9, 9, 6], [9, 9, 7], [9, 9, 8], [9, 9, 9]]\n"
]
}
],
"source": [
"print [[a, b, c] for a in range(1, 10) for b in range(1, 10) for c in range(1, 10) if a + b / c == 10]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# 3.10"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"2 3 5 7 11 13 17 19 23 29\n"
]
}
],
"source": [
"primes = [2]\n",
"is_prime = False\n",
"for num in range(2, 50):\n",
" for i in range(2, num):\n",
" if num % i == 0:\n",
" is_prime = False\n",
" break\n",
" else:\n",
" is_prime = True\n",
" if is_prime:\n",
" primes.append(num)\n",
" if len(primes) == 10:\n",
" break\n",
"for i in primes:\n",
" print i,"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 2",
"language": "python",
"name": "python2"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 2
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython2",
"version": "2.7.10"
}
},
"nbformat": 4,
"nbformat_minor": 0
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment