We know about hexidecimal numbers, and we know about floating point numbers... Let's write a function to interpret hexidecimal floating point numbers! Write two functions, hexifloat2float() and...

1 answer below »

We know about hexidecimal numbers, and we know about floating point numbers... Let's write a function to interpret hexidecimal floating point numbers!


Write two functions,hexifloat2float()andfloat2hexifloat(), which convert a hexidecimal encoded floating point number (expressed as a string) to a regular floating point number, and a function to reverse the process.


A hexifloat number is a string, such as "F832.991", containing hexidecimal digits and a decimal point. This is a real number in base 16, so each place value uses 16 to the appropriate power, rather than 10. Your function needs to convert it to a real number in base 10, and store that in a floating point value, rather than a string.



float2hexifloat()is highly likely to result in a non-terminating sequence of hexidecimal digits, so please have your algorithm return 8 digits of precision, but no more. The remainder should be truncated.


You may assume all alphabetic hexadecimal characters are capitalized.

Answered Same DayApr 28, 2021

Answer To: We know about hexidecimal numbers, and we know about floating point numbers... Let's write a...

Vaishnavi R answered on Apr 29 2021
143 Votes
{
"nbformat": 4,
"nbformat_minor": 0,
"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.8.5"
},
"colab": {
"name": "finalexam-p1u0wmhc.ipynb",
"provenance": []
}
},
"cells": [
{
"cell_type": "markdown",
"metadata": {
"id": "2FMnwLguWKGs"
},
"source": [
"# COMPUTER SCIENCE 1MD3 Final Examination\n",
"### Instructor: Mr Nicholas Moore\n",
"\n",
"## Late submissions will not be accepted.\n",
"\n",
"The only proof that your test was completed on time is on time submission.\n",
"\n",
"### Highest possible grade: (47 / 42)\n",
"\n",
"### Special Instructions - Please read\n",
"\n",
"* DO NOT RENAME THE ASSIGNMENT FILE (otherwise it might not be submitted or graded).\n",
"* DO NOT USE FUNCTION NAMES OTHER THAN THOSE PROVIDED IN THE QUESTION (or you will get zero for the question).\n",
"* DO NOT CHANGE THE THE NUMBER OF FUNCTION ARGUMENTS, USE THEM AS PROVIDED OR INDICATED IN THE QUESTION.\n",
"* Validate your notebook before submitting\n",
"* You must Submit the assignment from the assignments tab before the deadline. Late submissions will not be accepted. The only proof that your work was completed before the deadline is an on-time submission.\n",
"* If you are having trouble with the server not responding, you are probably trying to execu
te and infinite loop. To remedy this:\n",
" * click the stop button on the tool bar (proportional response), or\n",
" * go to \\\"Control Panel\\\" >> \\\"Stop My Server\\\" >> \\\"Start My Server\\\" (nuclear option). Note that you will lose any unsaved work with this one.\n",
" \n",
"This exam became available at 9:00am EST, April 28th, and must be submitted by 9:00am EST, April 29th.\n",
"\n",
"THERE IS NO GRACE PERIOD FOR EXAM SUBMISSIONS. YOU MUST SUBMIT BY THIS TIME OR YOU WILL GET ZERO.\n",
"\n",
"This is an open-book test, which means you are permitted to use reference material, such as wikipedia and the python documentation at python.org\n",
"\n",
"This is NOT a collaborative assessment. This exam will be evaluated for plagiarism, and offenders will certainly be prosecuted. Remember: never share your password, and never share your code! People who share their code are just as guilty of plagiarism as those who they share it with. \n",
"\n",
"If you have questions for myself or the TAs, please submit them to the Piazza. Please do not share your solutions on Piazza (or any other website). \n",
"\n",
"### Ensure all your function names and class field names are EXACTLY AS SPECIFIED.\n",
"\n",
"#### We will have no sympathy whatsoever for the submission of code which does not compile due to syntax errors.\n",
"\n",
"Good luck! "
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "ERkQ03bCWKHB"
},
"source": [
"# Question 1 - What's on the Menu? (7 points)\n",
"\n",
"Nick is retiring from teaching to follow his dream... fast food franchise ownership! Specifically **NickDonald's**, famous for their savoury and succulent menu items, such as the **BigMoore**, **Premium Chicken Sand-Nick**, and the highly popular **Deluxe Hot Choco-Nick**. But he needs to make a basic organization class scheme for his menu!\n",
"\n",
"## The Product Class\n",
"\n",
"Everything on the menu at **NickDonald's** has the following private attributes:\n",
"\n",
"- name $\\rightarrow$ String\n",
"- description $\\rightarrow$ String\n",
"- price $\\rightarrow$ Float\n",
"\n",
"Write a class, `Product`, which contains these attributes, as well as the following methods.\n",
"\n",
"- `init()` $\\rightarrow$ All string values are initialized to empty strings, all float values are initialized to 0.0 \n",
"- `getName()` $\\rightarrow$ returns the value of the name attribute.\n",
"- `getDescription()` $\\rightarrow$ returns the value of the description attribute.\n",
"- `getPrice()` $\\rightarrow$ returns the value of the price attribute.\n",
"- `setName()` $\\rightarrow$ Takes one argument other than self, and assigns it to the name attribute.\n",
"- `getDescription()` $\\rightarrow$ Takes one argument other than self, and assigns it to the description attribute.\n",
"- `setPrice()` $\\rightarrow$ Takes one argument other than self, and assigns it to the price attribute.\n",
"\n",
"Don't worry, this seems like a lot of methods, but they're all one-liners (except for `init` of course...)\n",
"\n",
"## Food and Clothing\n",
"\n",
"Create two derived class, `Food` and `Clothing` which inherit from `Product`. \n",
"\n",
"`Food` should add one attribute in addition to those contained in `Product` that should also be private: `calories`. Please create setting and getting methods for this new attribute in the same style as the methods of the `Product` class (i.e., `setCalories` and `getCalories`). Calories should be initialized with an integer value provided to the `init` function. \n",
"\n",
"Now, do the same with the `Clothing` class, adding a private `size` attribute. `size` is a string, and should have a default value of 'XL'. This class should also include `setSize` and `getSize` methods. \n"
]
},
{
"cell_type": "code",
"metadata": {
"deletable": false,
"nbgrader": {
"cell_type": "code",
"checksum": "4df7512045954416e4a18a51d9ea58bc",
"grade": false,
"grade_id": "cell-776c33f667326149",
"locked": false,
"schema_version": 3,
"solution": true,
"task": false
},
"id": "9l9HHuVAWKHC"
},
"source": [
""
],
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"id": "CTHcFjF-WKHD"
},
"source": [
"food1 = Food(50000)\n",
"food1.setName(\"Bacon BigMoore\")\n",
"food1.setDescription(\"An unholy shrine to gluttony and intemperance\")\n",
"food1.setPrice(6.66)\n",
"\n",
"print(\"Test Case 1 : \", (food1.getName() == \"Bacon BigMoore\"))\n",
"print(\"Test Case 2 : \", (food1.getDescription() == \"An unholy shrine to gluttony and intemperance\"))\n",
"print(\"Test Case 3 : \", (food1.getPrice() == 6.66))\n",
"print(\"Test Case 4 : \", (food1.getCalories() == 50000))\n",
"\n",
"# Privacy test\n",
"try : \n",
" print (food1.name)\n",
" print (\"Test Case 5 : Failed\")\n",
"except :\n",
" print (\"Test Case 5 : Passed\")"
],
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"deletable": false,
"editable": false,
"nbgrader": {
"cell_type": "code",
"checksum": "97688f2b11ca48a5d6f9567598ec8191",
"grade": true,
"grade_id": "cell-ddca92d5ab9d4a96",
"locked": true,
"points": 0,
"schema_version": 3,
"solution": false,
"task": false
},
"id": "65qgzLZ3WKHE"
},
"source": [
""
],
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"deletable": false,
"editable": false,
"nbgrader": {
"cell_type": "code",
"checksum": "b377bc1b0ab6cc1bea555700d856f67c",
"grade": true,
"grade_id": "cell-601e4a89455cdddf",
"locked": true,
"points": 1,
"schema_version": 3,
"solution": false,
"task": false
},
"id": "Z9fok7ItWKHE"
},
"source": [
""
],
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"deletable": false,
"editable": false,
"nbgrader": {
"cell_type": "code",
"checksum": "4c51be2b63baf8f1baa7ce629268f3c7",
"grade": true,
"grade_id": "cell-88ad2c6fa6984813",
"locked": true,
"points": 1,
"schema_version": 3,
"solution": false,
"task": false
},
"id": "XySd3cdFWKHF"
},
"source": [
""
],
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"deletable": false,
"editable": false,
"nbgrader": {
"cell_type": "code",
"checksum": "6beab0cb95cdd9a208dff682f9a13dcd",
"grade": true,
"grade_id": "cell-3d1a3fd3f867426d",
"locked": true,
"points": 1,
"schema_version": 3,
"solution": false,
"task": false
},
"id": "9yjlCYfzWKHF"
},
"source": [
""
],
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"deletable": false,
"editable": false,
"nbgrader": {
"cell_type": "code",
"checksum": "5e9737d00ee52f80958dae3001a0b7bc",
"grade": true,
"grade_id": "cell-a23edc943fd3e48c",
"locked": true,
"points": 1,
"schema_version": 3,
"solution": false,
"task": false
},
"id": "I1fuKT9XWKHF"
},
"source": [
""
],
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"deletable": false,
"editable": false,
"nbgrader": {
"cell_type": "code",
"checksum": "9ea8194f32a1d3ad8c3e23ca03d8f2fa",
"grade": true,
"grade_id": "cell-ab6384a378b50940",
"locked": true,
"points": 1,
"schema_version": 3,
"solution": false,
"task": false
},
"id": "EJ-WVVvuWKHG"
},
"source": [
""
],
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"deletable": false,
"editable": false,
"nbgrader": {
"cell_type": "code",
"checksum": "9cf58b1424c0e3787bddbb12465e9ee7",
"grade": true,
"grade_id": "cell-b5b1aec44d2707d0",
"locked": true,
"points": 2,
"schema_version": 3,
"solution": false,
"task": false
},
"id": "jIFG-KDyWKHG"
},
"source": [
""
],
"execution_count": null,
"outputs": []
},
{
"cell_type": "markdown",
"metadata": {
"id": "NW4rrG2LWKHH"
},
"source": [
"## Question 2: HexiFloat! (8 points)\n",
"\n",
"We know about hexidecimal numbers, and we know about floating point numbers... Let's write a function to interpret hexidecimal floating point numbers! \n",
"\n",
"Write two functions, `hexifloat2float()` and `float2hexifloat()`, which convert a hexidecimal encoded floating point number (expressed as a string) to a regular floating point number, and a function to reverse the process. \n",
"\n",
"A hexifloat number is a string, such as \"F832.991\", containing hexidecimal digits and a decimal point. This is a real number in base 16, so each place value uses 16 to the appropriate power, rather than 10. Your function needs to convert it to a real number in base 10, and store that in a floating point value, rather than a string. \n",
"\n",
"`float2hexifloat()` is highly likely to result in a non-terminating sequence of hexidecimal digits, so please have your algorithm return 8 digits of precision, but no more. The remainder should be truncated. \n",
"\n",
"You may assume all alphabetic hexadecimal characters are capitalized. \n",
"\n",
"HINT: The prof thinks starting with `float2hexifloat()` might be slightly easier. \n",
"\n",
"NOTE: In case it isn't obvious, the thing you're being tested on here is whether you can extend the ideas presented in Topic 5 to hexidecimal numbers. Do not post questions asking how the math works on Piazza (OR ANSWER SUCH QUESTIONS!) because that's what you're being TESTED ON HERE. Thank you for your cooperation. "
]
},
{
"cell_type": "code",
"metadata": {
"deletable": false,
"nbgrader": {
"cell_type": "code",
"checksum": "6e6fd02a7cb4be1388d9af8b65787d25",
"grade": false,
"grade_id": "cell-677a4df6d5d7e2cd",
"locked": false,
"schema_version": 3,
"solution": true,
"task": false
},
"id": "2hLK1CJmWKHI"
},
"source": [
"def hexifloat2float(hfstring) :\n",
" hexvalues = hfstring.split(\".\")\n",
" hexval = hexvalues[0]\n",
" length = len(hexval)\n",
" base = 1\n",
" dec_val = 0\n",
" for i in range(length - 1, -1, -1):\n",
" if hexval[i] >= '0' and hexval[i] <= '9':\n",
" dec_val += (ord(hexval[i]) - 48) * base\n",
" base = base * 16\n",
" elif hexval[i] >= 'A' and hexval[i] <= 'F':\n",
" dec_val += (ord(hexval[i]) - 55) * base\n",
" base = base * 16\n",
" \n",
" hexval = hexvalues[1]\n",
" hexval = \"\".join(reversed(hexval))\n",
" length = len(hexval)\n",
" base = 1/16\n",
" dec_val1 =0 \n",
" for i in range(length - 1, -1, -1):\n",
" if hexval[i] >= '0' and hexval[i] <= '9':\n",
" dec_val1 += (ord(hexval[i]) - 48) * base\n",
" base = base/16\n",
" elif hexval[i] >= 'A' and hexval[i] <= 'F':\n",
" dec_val1 += (ord(hexval[i]) - 55) * base\n",
" base = base/16\n",
"\n",
" ansInDecimal = dec_val+dec_val1\n",
" return ansInDecimal\n",
"\n",
"def float2hexifloat (floatval) :\n",
" floatvalStr = str(floatval)\n",
" val = floatvalStr.split(\".\")\n",
" num = int(val[0])\n",
" ans = \"\"\n",
" while num!=0:\n",
" t = int(num%16)\n",
" if t >= 10:\n",
" if t==10:\n",
" ans = \"A\"+ans\n",
" elif t == 11:\n",
" ans = \"B\"+ans\n",
" elif t==12:\n",
" ans = \"C\"+ans\n",
" elif t == 13:\n",
" ans = \"D\"+ans\n",
" elif t == 14:\n",
" ans = \"E\"+ans\n",
" elif t == 15:\n",
" ans = \"F\"+ans\n",
" else:\n",
" ans = str(t)+ans\n",
" num = int(num/16)\n",
" \n",
" num = int(val[1])\n",
" ans1 = \"\"\n",
" for i in range(0,8):\n",
" s = \"0.\"+str(num)\n",
" n = float(s)*16\n",
" stringVal = str(n)\n",
" values = stringVal.split(\".\")\n",
" t = int(values[0])\n",
" if t >= 10:\n",
" if t==10:\n",
" ans1 = \"A\"+ans1\n",
" elif t == 11:\n",
" ans1 = \"B\"+ans1\n",
" elif t==12:\n",
" ans1 = \"C\"+ans1\n",
" elif t == 13:\n",
" ans1 = \"D\"+ans1\n",
" elif t == 14:\n",
" ans1 = \"E\"+ans1\n",
" elif t == 15:\n",
" ans1 = \"F\"+ans1\n",
" else:\n",
" ans1 = str(t)+ans1\n",
" num = int(values[1])\n",
" ans1 = \"\".join(reversed(ans1))\n",
" if ans == \"\":\n",
" return \"0.\"+ans1\n",
" else:\n",
" return ans+\".\"+ans1\n",
" \n",
"\n",
"# YOUR CODE HERE\n",
"#raise NotImplementedError()\n",
"\n"
],
"execution_count": 5,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "JWqoBasMWKHI",
"outputId": "58f521d7-e833-493c-9680-2eeda2f36764"
},
"source": [
"print(\"Test Case 1 : \", hexifloat2float(\"FF.0\") == 255.0)\n",
"print(\"Test Case 2 : \", hexifloat2float(\"BAD.CAB\") == 2989.791748046875)\n",
"print(\"Test Case 3 : \", float2hexifloat(0.3429) == \"0.57C84B5D\")\n",
"print(\"Test Case 3 : \", float2hexifloat(3413421.39752989) == \"3415AD.65C484D4\")"
],
"execution_count": 6,
"outputs": [
{
"output_type": "stream",
"text": [
"Test Case 1 : True\n",
"Test Case 2 : True\n",
"Test Case 3 : True\n",
"Test Case 3 : True\n"
],
"name": "stdout"
}
]
},
{
"cell_type": "code",
"metadata": {
"deletable": false,
"editable": false,
"nbgrader": {
"cell_type": "code",
"checksum": "fe82aff68c378621594a356e695a9369",
"grade": true,
"grade_id": "cell-673635640f1159d5",
"locked": true,
"points": 1,
"schema_version": 3,
...
SOLUTION.PDF

Answer To This Question Is Available To Download

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here