Skip to content

Instantly share code, notes, and snippets.

@pembeci
Last active December 29, 2016 20:36
Show Gist options
  • Save pembeci/59cb3130b357e79dfbad516bcc32840a to your computer and use it in GitHub Desktop.
Save pembeci/59cb3130b357e79dfbad516bcc32840a to your computer and use it in GitHub Desktop.
Ceng 1003 Final Exam Preperation - Fall 2016
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## CENG 1003 - Final Exam Preperation (Fall 2016)\n",
"\n",
"In the exam, for most of the questions you will put yourself into the shoes of\n",
"Phyton interpreter (i.e. evaluate the expressions, keep the values of the\n",
"variables, determine the flow based on conditional and loop statements) and\n",
"write the output of the given code. So for the following questions try to come up with an answer by paper\n",
"and pencil first. Then run the actual code and see if you were correct. If you weren't,\n",
"think once more about the given code, add some print calls to understand what is going on."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Lists, Strings, Tuples"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**Q1:** Write the output of the following code segment."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"list1 = [ 7, 6, 5, 4, 3, 2, 1 ]\n",
"list2 = [ 11, 4, 13, 12, 15, 9, 17 ]\n",
"for i in range(0,7):\n",
" for j in range(i,7):\n",
" if((list1[i]+list2[j]) % 4 == 0):\n",
" print(list1[i], list2[j])"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**Q2:** Write the output of the following code segment."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"list1 = [ 2, 7, 4, 1, 8 ];\n",
"list2 = [ 1, 2, 3, 11, 12, 13, 21, 22, 23, 24 ]\n",
"total = 0;\n",
"for i in list1:\n",
" total += list2[i-1];\n",
"print(total);"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**Q3:** Write the output of the following code segment."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"a = ( [1,2,3,4,5], 456, \"Hello\", 123)\n",
"print(a[0][2], a[2][2:], a[1] + a[-1])\n",
"s1 = \"HappyNewYear\"\n",
"s2 = \"012345678901\" # given to help you count indices\n",
"print(s1[3:7], s1[:4], s1[9:], s1[-4], s1[4:-4], s1[-5:-2])"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**Q4:** Write the output of the following code segment."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"str1 = \"Python Rocks.\"\n",
"str2 = \"Oh my God, a red cat.\"\n",
"str3 = \"012345678901234567890\" # given to help you count indices\n",
"for c in str1:\n",
" i = str2.find(c)\n",
" if (i > 0 and c.isalpha()): \n",
" print(c,i, end=\" \")\n",
" else:\n",
" print(\"???\", end=\" \")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**Q5:** Write the output of the following code segment."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"s = \"How to think like a computer scientist\"\n",
"result = \"--\"\n",
"for w in s.split():\n",
" if w[0] in \"cost\": result = result + w[:3] + \"--\"\n",
"print(result)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**Q6:** Write the output of the following code segment. Explain shortly what function `q6` does. Don't give a line by line explanation, just tell what the returned result is based on the function's arguments (i.e. *function f returns the larger of the given numbers* or *function f takes a list and returns the average of the numbers in that list*)."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"def q6(s1,s2):\n",
" result = \"\"\n",
" for c in s1:\n",
" if c not in s2:\n",
" result = result + c \n",
" return result\n",
"\n",
"print(q6(\"hello leyla\", \"alo\"))\n",
"print(q6(\"alo leyla\", \"hello\"))\n",
"print(q6(\"ant\", \"butterfly\"))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**Q7:** Write the output of the following code segment. Be careful about references to lists."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"def q7(a_list):\n",
" for i in range(len(a_list)):\n",
" a_list[i] = a_list[i] + 10\n",
"\n",
"a = [1,2,3,4,[50,100],6]\n",
"b = a\n",
"c = b[-2]\n",
"b.insert(2,7)\n",
"c.append(9)\n",
"print(a)\n",
"a[0] = a.pop()\n",
"c[0] = 11\n",
"print(b)\n",
"b[2:4] = [\"some\", \"new\", \"values\"]\n",
"c.reverse()\n",
"q7(c)\n",
"print(a)\n",
"\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**Q8:** Here is a function that will find the maximum value in a numbers list."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"def find_max(nums):\n",
" result = nums[0] # assume first number is the maximum one\n",
" for num in nums[1:]: # check the rest of the numbers\n",
" if num > result: result = num # if you find a larger num than the current one found, modify your result\n",
" return result "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Write a similar function called `find_max2` that will return the second largest value inside its nums parameter. This is similar to finding the largest value but this time you have to keep track of the largest (i.e `max1`) and the second largest value (i.e `max2`) while you are iterating over the values in the array. Whenever you find a new array value that is larger then one of them you have to update these variables accordingly (i.e. if new value is larger than `max1` then `max1` becomes `max1` and `max1` becomes this new value but if it is only larger than `max2` then only update `max2` as this new value)."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**Q9:** Write the output of the following code segment."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"s = \"How to think like a computer scientist\"\n",
"(a,b) = ([\"b\"],[\"a\"])\n",
"for w in s.split():\n",
" if len(w)%2 == 0:\n",
" a.append(w)\n",
" else:\n",
" b.append(w)\n",
"print(\",\".join(a) + \"---\" + \";\".join(b))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Files"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**Q10:** Assume you have a file named \"q10_input.txt\" in the same directory as the below code with this content:\n",
"```\n",
"3\n",
"7\n",
"2\n",
"5\n",
"-1\n",
"4\n",
"```\n",
"Give the content of the file \"q10_out.txt\" after the following code is run:\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"inp_file = open(\"q10_input.txt\", \"r\")\n",
"out_file = open(\"q10_out.txt\", \"w\")\n",
"\n",
"line = inp_file.readline()\n",
"while line:\n",
" v1 = int(line.strip()) \n",
" line = inp_file.readline()\n",
" v2 = int(line.strip()) \n",
" for i in range(v1):\n",
" out_file.write(str(v2) + \" \")\n",
" out_file.write(\"\\n\")\n",
" line = inp_file.readline()\n",
" \n",
"inp_file.close()\n",
"out_file.close()\n",
"\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**Q11:** Assume you have a file named \"q11_input.txt\" in the same directory as the below code with this content:\n",
"```\n",
"append;item;mutator\n",
"insert;position, item;mutator\n",
"pop;none;hybrid\n",
"pop;position;hybrid\n",
"sort;none;mutator\n",
"reverse;none;mutator\n",
"index;item;int\n",
"count;item;int\n",
"remove;item;mutator\n",
"```\n",
"Give the output of the following code segment:\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"infile = open(\"q11_input.txt\", \"r\")\n",
"a = []\n",
"b = []\n",
"c = []\n",
"for line in infile:\n",
" line=line.strip()\n",
" values = line.split(\";\")\n",
" if values[2][0] == \"m\":\n",
" a.append(values[0])\n",
" elif values[2][0] == \"h\":\n",
" b.append(values[0])\n",
" else:\n",
" c.append(values[0])\n",
"print(\"ms: \", \",\".join(a))\n",
"print(\"hs: \", \",\".join(b))\n",
"print(\"is: \", \",\".join(c))\n",
" "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Dictionaries"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**Q12:** Write the output of the following code segment."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"d = { \"a\": 5, \"b\": {\"x\": 0, \"y\":0}, \"c\": [1,2,3], \"d\": 3, \"e\": (8,9)}\n",
"d[\"a\"] = d.get(\"y\", 12)\n",
"d[\"b\"][\"f\"] = d.get(\"d\", 13)\n",
"d[\"f\"] = d[\"e\"][1] + 14\n",
"d[\"c\"][1] = 15\n",
"d[16] = 17\n",
"d[\"y\"] = 18\n",
"d[\"b\"][\"x\"] = 19\n",
"d[\"c\"].append(20)\n",
"for key,val in d.items():\n",
" print(key,\"➜\",val)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**Q13:** Write the output of the following code segment."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"code = \"abbabacaab\"\n",
"cnt = {}\n",
"for (i,c1) in enumerate(code[:-1]): # careful, we are ignoring the last item in our loop\n",
" c2 = code[i+1]\n",
" if c1 not in cnt:\n",
" cnt[c1] = {}\n",
" cnt[c1][c2] = cnt[c1].get(c2,0) + 1\n",
"for c1 in cnt:\n",
" for c2 in cnt[c1]:\n",
" print(c1 + c2 + \":\" + str(cnt[c1][c2]))\n",
" \n",
"# if you didn't get it right print the dictionary and change string code to be a smaller string \n",
"# print(cnt) \n",
" "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**Q14:** Assume you have a file named \"q14_input.txt\" in the same directory as the below code with this content:\n",
"```\n",
"t1,1-0,2-2,3-2,0-2\n",
"t2,1-1,2-1,0-3,0-0\n",
"t3,0-2,5-1,2-4,3-3\n",
"```\n",
"Give the output of the following code segment:\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"infile = open(\"q14_input.txt\", \"r\")\n",
"t = {} \n",
"for line in infile:\n",
" vals = line.strip().split(\",\")\n",
" t[vals[0]] = {\"w\":0, \"l\":0, \"t\":0}\n",
" for s in vals[1:]:\n",
" g = s.split(\"-\")\n",
" g1 = int(g[0])\n",
" g2 = int(g[1])\n",
" if g1>g2: k = \"w\"\n",
" elif g2>g1: k=\"l\"\n",
" else: k=\"t\"\n",
" t[vals[0]][k] = t[vals[0]][k] + 1\n",
"for n in t.keys():\n",
" print(n, end=\": \")\n",
" for k in [\"w\", \"l\", \"t\"]:\n",
" print(k.upper() + \"(\" + str(t[n][k]) + \")\", end=\" \")\n",
" print() "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Recursion"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**Q15:** Write the output of the following code segment."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"def q15(word):\n",
" if len(word) == 0:\n",
" return \"\"\n",
" else:\n",
" first = word[0]\n",
" rest = word[1:] \n",
" sub_result = q15(rest) \n",
" if first >= \"a\" and first <=\"z\":\n",
" result = first.upper() + sub_result\n",
" elif first >= \"A\" and first <=\"Z\":\n",
" result = first.lower() + sub_result\n",
" else: \n",
" result = \".\" + sub_result \n",
" return result\n",
" \n",
"print(q15(\"Hello! Is this Dr. Smith's e-mail?\")) "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**Q16:** Write the output of the following code segment."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"def q16(nums):\n",
" if len(nums) == 0:\n",
" return ([],[])\n",
" else:\n",
" first = nums[0]\n",
" rest = nums[1:] \n",
" (r1,r2) = q16(rest) \n",
" if first%2==0:\n",
" r1.append(first)\n",
" else: \n",
" r2.append(first) \n",
" return (r1,r2)\n",
" \n",
"print(q16([3,7,2,8,5,6,2,1,3])) "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**Q17:** Write the output of the following code segment."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"def q17(nums,chunk_size):\n",
" if len(nums) <= chunk_size:\n",
" return [nums]\n",
" else:\n",
" first = nums[0]\n",
" rest = nums[1:] \n",
" chunks = q17(rest, chunk_size) \n",
" if len(chunks[0]) < chunk_size:\n",
" chunks[0].insert(0,first)\n",
" else: \n",
" chunks.insert(0,[first]) \n",
" return chunks\n",
" \n",
"print(q17([1,2,3],3)) \n",
"print(q17([1,2,3,4,5],3))\n",
"print(q17([1,2,3,4,5,6,7],3))\n",
"print(q17([1,2,3,4,5,6,7,8,9,10,11,12],5))\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
" **Q18:** Fill in the blanks in the following recursive function such that it returns the count of even numbers in its argument `nums` which is a list of numbers."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"def q18(nums):\n",
" if len(nums) == 0:\n",
" return 0\n",
" else:\n",
" first = nums[0]\n",
" rest = nums[1:] \n",
" sub_result = ________ \n",
" if ________:\n",
" return __________\n",
" else: \n",
" return __________ \n",
"\n",
"a = [1,2,3,4,5,6,8,0,3,12,5] \n",
"print(\"There are \" + str(q18(a)) + \" evens in \" + str(a)) \n",
"# Expected output: There are 6 evens in [1, 2, 3, 4, 5, 6, 8, 0, 3, 12, 5]\n",
"a = [3,5,7] \n",
"print(\"There are \" + str(q18(a)) + \" evens in \" + str(a)) \n",
"# Expected output: There are 0 evens in [3,5,7]\n",
"a = [2,4,6,3,8,2,1] \n",
"print(\"There are \" + str(q18(a)) + \" evens in \" + str(a)) \n",
"# Expected output: There are 5 evens in [2,4,6,3,8,2,1]\n",
"\n"
]
}
],
"metadata": {
"anaconda-cloud": {},
"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": 1
}
append;item;mutator
insert;position, item;mutator
pop;none;hybrid
pop;position;hybrid
sort;none;mutator
reverse;none;mutator
index;item;int
count;item;int
remove;item;mutator
t1,1-0,2-2,3-2,0-2
t2,1-1,2-1,0-3,0-0
t3,0-2,5-1,2-4,3-3
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment