-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtask.py
More file actions
519 lines (404 loc) · 15.2 KB
/
Copy pathtask.py
File metadata and controls
519 lines (404 loc) · 15.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
# ------------------------ FUNCTION 1 ---------------------------
ALL_DIGITS = ['1', '2', '3', '4', '5', '6', '7', '8', '9', '0']
UPPER_CASE_HEX_ALPHAS = ['A', 'B', 'C', 'D', 'E', 'F']
LOWER_CASE_HEX_ALPHAS = ['a', 'b', 'c', 'd', 'e', 'f']
def is_hex(num_str):
"""Takes a string of digits as a parameter. Returns True if the string represents a hex number.
Returns False otherwise.
"""
if num_str[0:2].lower() == '0x':
return True
else:
return False
def is_float(num_str):
"""Takes a string of digits as a parameter. Returns True if the string represents a float number.
Returns False otherwise.
"""
for char in num_str:
if char == '.':
return True
return False
def process_int(num_str):
"""Takes a string representation of an integer as a parameter. Converts the the string to an int and
returns the int. If the string contains non-numeric characters the function returns None.
"""
number = 0
place = 1
for char in reversed(num_str):
# Check for non-numeric character
if char not in ALL_DIGITS:
return None
digit = ord(char) - 48
number += digit * place
place *= 10
return number
def process_float(num_str):
"""Takes a string representation of a float as a parameter. Converts the string to
a float and returns the float. If the string contains multiple decimal points the function
returns None. If the string contains any non-numeric characters the function returns None.
"""
num_str = num_str.split('.')
# Check for multiple decimal points
if len(num_str) > 2:
return None
before_decimal = num_str[0]
after_decimal = num_str[1]
number = 0.
place = 1
for char in reversed(before_decimal):
# Check for non-numeric character
if char not in ALL_DIGITS:
return None
digit = ord(char) - 48
number += digit * place
place *= 10
place = .1
for char in after_decimal:
# Check for non-numeric character
if char not in ALL_DIGITS:
return None
digit = ord(char) - 48
number += digit * place
place *= .1
places = len(after_decimal)
number = round(number, places)
return number
def process_hex(num_str):
"""Takes a string representation of a hexadecimal number as a parameter. Converts the string to an integer
of equal value and returns the integer. If the string contains any non-numeric or non-hex-alpha digits
the function returns None.
"""
number = 0
place = 1
for char in reversed(num_str):
if char in ALL_DIGITS:
digit = ord(char) - 48
elif char in UPPER_CASE_HEX_ALPHAS:
digit = ord(char) - 55
elif char in LOWER_CASE_HEX_ALPHAS:
digit = ord(char) - 87
else:
return None
number += digit * place
place *= 16
return number
def conv_num(num_str):
"""Takes a string representation of a number as a parameter. The number can be either an
integer, float, or hexadecimal. The function converts the string to an int or float and returns it.
If the number is an int or hex, the function returns an int. If the number is a float the function returns
a float.
"""
positive = True
number = None
# Confirm input is string
if not isinstance(num_str, str):
return None
# Check for an empty string
if num_str == '':
return None
# Determine Positive or Negative, if negative strip off minus sign
if num_str[0] == '-':
positive = False
num_str = num_str[1:]
# Determine num_type, if hex strip off 0x
if is_hex(num_str):
num_type = 'HEX'
num_str = num_str[2:]
elif is_float(num_str):
num_type = 'FLOAT'
else:
num_type = 'INT'
# process number based on type
if num_type == 'INT':
number = process_int(num_str)
elif num_type == 'FLOAT':
number = process_float(num_str)
elif num_type == 'HEX':
number = process_hex(num_str)
if number is not None and not positive:
number = -number
return number
# ------------------------ FUNCTION 2 ---------------------------
def my_datetime(num_sec):
"""takes a timestamp value (num_sec) representing # of seconds passed
since 1/1/1970 and converts it to a string in MM-DD-YYYY format"""
# convert seconds to days, 86400 seconds in a day
days = num_sec // 86400
# create array that represents the amount of days in a month in a yearly
# calendar
days_months = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
# initialize leap_year_counter to 2 since we start at 1970
leap_year_counter = 2
# initialize normal_year days to 365 and leap_year_days to 366
normal_year_days = 365
leap_year_days = 366
# initialize current_year to 1970, current_month to 1
current_year = 1970
current_month = 1
# convert days to years, update current_year
while ((leap_year_counter == 4 and days > 366) or
(leap_year_counter != 4 and days > 365)):
# if we're in a leap year
if ((current_year % 4 == 0 and current_year % 100 != 0) or
(current_year % 400 == 0)):
days = days - leap_year_days
leap_year_counter = 1
# not a leap year
else:
days = days - normal_year_days
leap_year_counter += 1
current_year += 1
# if we landed on a leap year, set 2nd element in days_months to 29
if ((current_year % 4 == 0 and current_year % 100 != 0) or
(current_year % 400 == 0)):
days_months[1] = 29
for month in days_months:
# break loop if we hit last month
if days < month:
break
else:
days = days - month
current_month += 1
# account for landing on january 1st edge case
if current_month == 13:
current_month = 1
current_year += 1
# finally, get our current_day
current_day = days + 1
# convert our integers into MM-DD-YYYY string format
string_day = ""
string_month = ""
string_year = str(current_year)
if current_day >= 1 and current_day < 10:
string_day = "0" + str(current_day)
else:
string_day = str(current_day)
if current_month >= 1 and current_month < 10:
string_month = "0" + str(current_month)
else:
string_month = str(current_month)
return (string_month + "-" +
string_day + "-" + string_year)
# ------------------- FUNCTION 3 -----------------------
# CITATION: See PyCharm Contributors, Works Cited at end
def convert_dec_to_bin(number):
# CITATION: See Brennan, Works Cited at end
binary_string = ""
result = number
if number == 0:
# If it's zero, we're done.
binary_string = "0"
elif number > 0:
while result != 0:
# CITATION: See Agrawal (Division Operators...),
# Works Cited at end
new_result = result // 2
remainder = result % 2
result = new_result
string_remainder = ""
if remainder == 0:
string_remainder = "0"
else:
string_remainder = "1"
# CITATION: See Byers, Works Cited at end
binary_string = string_remainder + binary_string
# CITATION: See Wikipedia Contributors, Works Cited at end
# Pad the nybble if needed
while len(binary_string) % 4 != 0:
binary_string = "0" + binary_string
elif number < 0:
# Get the positive version
positive_number = 0 - number
result = positive_number
# Do the same calcs as above for the positive version
while result != 0:
# CITATION: See Agrawal (Division Operators...),
# Works Cited at end
new_result = result // 2
remainder = result % 2
result = new_result
string_remainder = ""
if remainder == 0:
string_remainder = "0"
else:
string_remainder = "1"
# CITATION: See Byers, Works Cited at end
binary_string = string_remainder + binary_string
# CITATION: See Wikipedia Contributors, Works Cited at end
# Pad the nybble if needed
while len(binary_string) % 4 != 0:
binary_string = "0" + binary_string
# CITATION: See RapidTables.com Contributors, Works Cited at end
# Now we just need to prepend the negative sign
binary_string = "-" + binary_string
return binary_string
def convert_bin_to_hex(number):
# CITATION: See Brennan, RapidTables Contributors, Thakur,
# Works Cited at end
# CITATION: See Kumar, Mishra (Gullu), Works Cited at end
bit_dictionary = {
"0000": "0",
"0001": "1",
"0010": "2",
"0011": "3",
"0100": "4",
"0101": "5",
"0110": "6",
"0111": "7",
"1000": "8",
"1001": "9",
"1010": "A",
"1011": "B",
"1100": "C",
"1101": "D",
"1110": "E",
"1111": "F"
}
bit_string = ""
result = number
if number == "0":
return "00"
if number[0] != "-":
first_nybble = True
while len(result) > 0:
cur_nybble = result[len(result) - 4:(len(result) + 1)]
# CITATION: See Wikipedia Contributors, Byers,
# Mishra (Gullu) Works Cited
hexadecimal_digit = bit_dictionary.get(cur_nybble)
# Slice this nybble off the result
result = result[0:len(result) - 4]
# CITATION: See PyCharm Contributors, Works Cited at end
if first_nybble:
first_nybble = False
bit_string = hexadecimal_digit + bit_string
elif not first_nybble and len(result) > 0:
first_nybble = True
bit_string = " " + hexadecimal_digit + bit_string
else:
bit_string = hexadecimal_digit + bit_string
if len(result) == 0 and (len(bit_string) % 3) - 2 != 0:
# We're at the last byte and need to pad
bit_string = "0" + bit_string
else:
bit_string = convert_bin_to_hex_negative(result)
return bit_string
def convert_bin_to_hex_negative(result):
# CITATION: See Kumar, Mishra (Gullu), Works Cited at end
bit_dictionary = {
"0000": "0",
"0001": "1",
"0010": "2",
"0011": "3",
"0100": "4",
"0101": "5",
"0110": "6",
"0111": "7",
"1000": "8",
"1001": "9",
"1010": "A",
"1011": "B",
"1100": "C",
"1101": "D",
"1110": "E",
"1111": "F"
}
bit_string = ""
first_nybble = True
# Slice off the negative sign
result = result[1:len(result) + 1]
while len(result) > 0:
cur_nybble = result[len(result) - 4:(len(result) + 1)]
# CITATION: See Wikipedia Contributors, Byers,
# Mishra (Gullu) Works Cited
hexadecimal_digit = bit_dictionary.get(cur_nybble)
# Slice this nybble off the result
result = result[0:len(result) - 4]
# CITATION: See PyCharm Contributors, Works Cited at end
if first_nybble:
first_nybble = False
bit_string = hexadecimal_digit + bit_string
elif not first_nybble and len(result) > 0:
first_nybble = True
bit_string = " " + hexadecimal_digit + bit_string
else:
bit_string = hexadecimal_digit + bit_string
if len(result) == 0 and (len(bit_string) % 3) - 2 != 0:
# theNumber = len(bit_string) % 3 - 2
# We're at the last byte and need to pad
bit_string = "0" + bit_string
# Just need to prepend the negative sign now
bit_string = "-" + bit_string
return bit_string
def conv_endian(num, endian="big"):
num = convert_bin_to_hex(convert_dec_to_bin(num))
# If it's big, we're done
if endian == "big":
return num
elif endian == "little":
result = num
bit_string = ""
if result[0] != "-":
while len(result) > 0:
cur_nybble = result[0:2]
bit_string = cur_nybble + bit_string
result = result[3:len(result) + 1]
if len(result) != 0:
bit_string = " " + bit_string
else:
# Slice off the negative sign
result = result[1:len(result) + 1]
while len(result) > 0:
cur_nybble = result[0:2]
bit_string = cur_nybble + bit_string
result = result[3:len(result) + 1]
if len(result) != 0:
bit_string = " " + bit_string
# Just need to prepend the negative sign now
bit_string = "-" + bit_string
return bit_string
else:
# CITATION: See OSU Course Contributors, Works Cited at end
# Some invalid endianness was passed
return None
"""
WORKS CITED
Agrawal, Arpit. "Division Operators in Python." GeeksForGeeks,
https://www.geeksforgeeks.org/division-operator-in-python/.
Brennan, Eugene. "How to Convert Hex to Binary and Binary to Hexadecimal."
Owlcation.com, https://owlcation.com/stem/
How-to-Convert-Hex-to-Binary-and-Binary-to-Hexadecimal.
Brennan, Eugene. "How to Convert Decimal to Binary and Binary to Decimal."
Owlcation.com, https://owlcation.com/stem/
How-to-Convert-Decimal-to-Binary-and-Binary-to-Decimal.
Byers, Mark <StackOverflow username>. "inserting characters at the start
and end of a string." StackOverflow,
https://stackoverflow.com/questions/10059554/
inserting-characters-at-the-start-and-end-of-a-string.
Mishra (Gullu), Shashank. "Switch Case in Python (Replacement)."
GeeksForGeeks.com,
https://www.geeksforgeeks.org/switch-case-in-python-replacement/.
Kumar, Prashant <StackOverflow username>. "What is the Python equivalent
for a case/switch statement? [duplicate]." StackOverflow.com,
https://stackoverflow.com/questions/11479816/
what-is-the-python-equivalent-for-a-case-switch-statement.
OSU Course Contributors. "Group Project: Part 2." Canvas,
https://oregonstate.instructure.com/courses/1810943/
assignments/8334612?module_item_id=20728375.
PyCharm Contributors. "Show quick fixes." PyCharm Software Feature,
https://www.jetbrains.com/pycharm/.
RapidTables.com Contributors. "Decimal to Binary converter."
RapidTables.com,
https://www.rapidtables.com/convert/number/decimal-to-binary.html.
RapidTables.com Contributors. "How to convert binary to hex."
Rapidtables.com,
https://www.rapidtables.com/convert/number/how-binary-to-hex.html.
Shah, Nirmi. "What does the if __name__ == “__main__”: do?"
GeeksForGeeks.com,
https://www.geeksforgeeks.org/what-does-the-if-__name__-__main__-do/.
Thakur, Arjun. "How to Convert Binary to Hexadecimal?" TutorialsPoint.com,
https://www.tutorialspoint.com/how-to-convert-binary-to-hexadecimal.
Wikipedia Contributors. "Nibble." En.Wikipedia.org,
https://en.wikipedia.org/wiki/Nibble.
Wikipedia Contributors. "Hexadecimal." Simple.Wikipedia.org,
https://simple.wikipedia.org/wiki/Hexadecimal
"""