Skip to content

Commit 8a36d51

Browse files
authored
Challenge LinkedInLearning LinkedInLearning#2 - Converting Hexidecimals
Jupyter Notebook .ipynb with the step-by-step solution I found developing a factorial function. - Original Code Stub - Add Test Cases - Check for Valid Hexidecimals - Add For-Loops to check HD characters against the library - Add assignments for variables & ensure For-Loop returns result value when exited - Update ForLoop to retrieve HD values from the library & perform addition (validate results) - Update ForLoop to perform multiplication in multiples of 16 on HD values
1 parent bce2c29 commit 8a36d51

File tree

1 file changed

+292
-0
lines changed

1 file changed

+292
-0
lines changed
Lines changed: 292 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,292 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "code",
5+
"execution_count": null,
6+
"id": "0693f2c2-54cf-4b8a-b480-9aa4847622e4",
7+
"metadata": {},
8+
"outputs": [],
9+
"source": [
10+
"</> Code Challenge: Hex to Dec"
11+
]
12+
},
13+
{
14+
"cell_type": "code",
15+
"execution_count": null,
16+
"id": "536e914e-3c94-4678-8080-dd64804d65ff",
17+
"metadata": {},
18+
"outputs": [],
19+
"source": [
20+
"#Original Code Stub\n",
21+
"\n",
22+
"hexNumbers = {\n",
23+
" '0': 0, '1': 1, '2': 2, '3': 3, '4': 4, '5': 5, '6': 6, '7': 7, '8': 8, '9': 9,\n",
24+
" 'A': 10, 'B': 11, 'C': 12, 'D': 13, 'E': 14, 'F': 15\n",
25+
"}\n",
26+
"\n",
27+
"# Converts a string hexadecimal number into an integer decimal\n",
28+
"# If hexNum is not a valid hexadecimal number, returns None\n",
29+
"def hexToDec(hexNum):\n",
30+
" pass"
31+
]
32+
},
33+
{
34+
"cell_type": "code",
35+
"execution_count": 2,
36+
"id": "38e48fed-bdda-428f-808d-e8c94cd6a5a9",
37+
"metadata": {},
38+
"outputs": [
39+
{
40+
"name": "stdout",
41+
"output_type": "stream",
42+
"text": [
43+
"1A\n",
44+
"FFF\n",
45+
"1234\n"
46+
]
47+
}
48+
],
49+
"source": [
50+
"#Add return function to code stub w/ test values\n",
51+
"\n",
52+
"hexNumbers = {\n",
53+
" '0': 0, '1': 1, '2': 2, '3': 3, '4': 4, '5': 5, '6': 6, '7': 7, '8': 8, '9': 9,\n",
54+
" 'A': 10, 'B': 11, 'C': 12, 'D': 13, 'E': 14, 'F': 15\n",
55+
"}\n",
56+
"\n",
57+
"def hexToDec(hexNum):\n",
58+
" return hexNum\n",
59+
"\n",
60+
"# Testing the function\n",
61+
"print(hexToDec('1A')) \n",
62+
"print(hexToDec('FFF')) \n",
63+
"print(hexToDec('1234'))"
64+
]
65+
},
66+
{
67+
"cell_type": "code",
68+
"execution_count": 3,
69+
"id": "f60d62b3-7961-44b6-86a0-da958cb85371",
70+
"metadata": {},
71+
"outputs": [
72+
{
73+
"name": "stdout",
74+
"output_type": "stream",
75+
"text": [
76+
"1A\n",
77+
"FFF\n",
78+
"None\n"
79+
]
80+
}
81+
],
82+
"source": [
83+
"#Add logic for handling invalid hex over 3 values long\n",
84+
"\n",
85+
"hexNumbers = {\n",
86+
" '0': 0, '1': 1, '2': 2, '3': 3, '4': 4, '5': 5, '6': 6, '7': 7, '8': 8, '9': 9,\n",
87+
" 'A': 10, 'B': 11, 'C': 12, 'D': 13, 'E': 14, 'F': 15\n",
88+
"}\n",
89+
"\n",
90+
"def hexToDec(hexNum):\n",
91+
" if len(hexNum) > 3:\n",
92+
" return None\n",
93+
" \n",
94+
" else:\n",
95+
" return hexNum\n",
96+
"\n",
97+
"# Testing the function\n",
98+
"print(hexToDec('1A')) \n",
99+
"print(hexToDec('FFF')) \n",
100+
"print(hexToDec('1234'))"
101+
]
102+
},
103+
{
104+
"cell_type": "code",
105+
"execution_count": 5,
106+
"id": "0c32edd9-6f08-4f4d-9e8d-cadad9a3a702",
107+
"metadata": {},
108+
"outputs": [
109+
{
110+
"name": "stdout",
111+
"output_type": "stream",
112+
"text": [
113+
"True\n",
114+
"True\n",
115+
"None\n"
116+
]
117+
}
118+
],
119+
"source": [
120+
"#Add For-Loop that checks char against library & returns values\n",
121+
"\n",
122+
"hexNumbers = {\n",
123+
" '0': 0, '1': 1, '2': 2, '3': 3, '4': 4, '5': 5, '6': 6, '7': 7, '8': 8, '9': 9,\n",
124+
" 'A': 10, 'B': 11, 'C': 12, 'D': 13, 'E': 14, 'F': 15\n",
125+
"}\n",
126+
"\n",
127+
"def hexToDec(hexNum):\n",
128+
" if len(hexNum) > 3:\n",
129+
" return None\n",
130+
" \n",
131+
" for char in hexNum:\n",
132+
" if char in hexNumbers:\n",
133+
" return True\n",
134+
" else:\n",
135+
" return None\n",
136+
" return hexNum\n",
137+
"\n",
138+
"# Testing the function\n",
139+
"print(hexToDec('1A')) \n",
140+
"print(hexToDec('FFF')) \n",
141+
"print(hexToDec('1234'))"
142+
]
143+
},
144+
{
145+
"cell_type": "code",
146+
"execution_count": 7,
147+
"id": "2c076c74-d333-4a4f-baea-ab7e51a483dc",
148+
"metadata": {},
149+
"outputs": [
150+
{
151+
"name": "stdout",
152+
"output_type": "stream",
153+
"text": [
154+
"True\n",
155+
"True\n",
156+
"None\n"
157+
]
158+
}
159+
],
160+
"source": [
161+
"#Update For-Loop with variable assignments & result var\n",
162+
"\n",
163+
"hexNumbers = {\n",
164+
" '0': 0, '1': 1, '2': 2, '3': 3, '4': 4, '5': 5, '6': 6, '7': 7, '8': 8, '9': 9,\n",
165+
" 'A': 10, 'B': 11, 'C': 12, 'D': 13, 'E': 14, 'F': 15\n",
166+
"}\n",
167+
"\n",
168+
"def hexToDec(hexNum):\n",
169+
" if len(hexNum) > 3:\n",
170+
" return None\n",
171+
" hexNum = hexNum.upper()\n",
172+
" decimal_value = 0\n",
173+
" for char in hexNum:\n",
174+
" if char in hexNumbers:\n",
175+
" return True\n",
176+
" else:\n",
177+
" return None\n",
178+
" return decimal_value\n",
179+
"\n",
180+
"# Testing the function\n",
181+
"print(hexToDec('1A')) \n",
182+
"print(hexToDec('FFF')) \n",
183+
"print(hexToDec('1234'))"
184+
]
185+
},
186+
{
187+
"cell_type": "code",
188+
"execution_count": 10,
189+
"id": "51490f78-be25-49a9-8e39-8abdc4e8975c",
190+
"metadata": {},
191+
"outputs": [
192+
{
193+
"name": "stdout",
194+
"output_type": "stream",
195+
"text": [
196+
"11\n",
197+
"45\n",
198+
"None\n"
199+
]
200+
}
201+
],
202+
"source": [
203+
"#Check ForLoop math with addition hexdec values from library\n",
204+
"\n",
205+
"hexNumbers = {\n",
206+
" '0': 0, '1': 1, '2': 2, '3': 3, '4': 4, '5': 5, '6': 6, '7': 7, '8': 8, '9': 9,\n",
207+
" 'A': 10, 'B': 11, 'C': 12, 'D': 13, 'E': 14, 'F': 15\n",
208+
"}\n",
209+
"\n",
210+
"def hexToDec(hexNum):\n",
211+
" if len(hexNum) > 3:\n",
212+
" return None\n",
213+
" hexNum = hexNum.upper()\n",
214+
" decimal_value = 0\n",
215+
" for char in hexNum:\n",
216+
" if char in hexNumbers:\n",
217+
" decimal_value = decimal_value + hexNumbers[char]\n",
218+
" else:\n",
219+
" return None\n",
220+
" return decimal_value\n",
221+
"\n",
222+
"# Testing the function\n",
223+
"print(hexToDec('1A')) \n",
224+
"print(hexToDec('FFF')) \n",
225+
"print(hexToDec('1234'))"
226+
]
227+
},
228+
{
229+
"cell_type": "code",
230+
"execution_count": 11,
231+
"id": "680efa56-1c24-4e72-9182-646082cbfc75",
232+
"metadata": {},
233+
"outputs": [
234+
{
235+
"name": "stdout",
236+
"output_type": "stream",
237+
"text": [
238+
"26\n",
239+
"4095\n",
240+
"None\n"
241+
]
242+
}
243+
],
244+
"source": [
245+
"#Update For-Loop with the hexadecimal converter/formula\n",
246+
"\n",
247+
"hexNumbers = {\n",
248+
" '0': 0, '1': 1, '2': 2, '3': 3, '4': 4, '5': 5, '6': 6, '7': 7, '8': 8, '9': 9,\n",
249+
" 'A': 10, 'B': 11, 'C': 12, 'D': 13, 'E': 14, 'F': 15\n",
250+
"}\n",
251+
"\n",
252+
"def hexToDec(hexNum):\n",
253+
" if len(hexNum) > 3:\n",
254+
" return None\n",
255+
" hexNum = hexNum.upper()\n",
256+
" decimal_value = 0\n",
257+
" for char in hexNum:\n",
258+
" if char in hexNumbers:\n",
259+
" decimal_value = decimal_value * 16 + hexNumbers[char]\n",
260+
" else:\n",
261+
" return None\n",
262+
" return decimal_value\n",
263+
"\n",
264+
"# Testing the function\n",
265+
"print(hexToDec('1A')) \n",
266+
"print(hexToDec('FFF')) \n",
267+
"print(hexToDec('1234'))"
268+
]
269+
}
270+
],
271+
"metadata": {
272+
"kernelspec": {
273+
"display_name": "Python 3 (ipykernel)",
274+
"language": "python",
275+
"name": "python3"
276+
},
277+
"language_info": {
278+
"codemirror_mode": {
279+
"name": "ipython",
280+
"version": 3
281+
},
282+
"file_extension": ".py",
283+
"mimetype": "text/x-python",
284+
"name": "python",
285+
"nbconvert_exporter": "python",
286+
"pygments_lexer": "ipython3",
287+
"version": "3.12.5"
288+
}
289+
},
290+
"nbformat": 4,
291+
"nbformat_minor": 5
292+
}

0 commit comments

Comments
 (0)