Problem 2
Pathfinder
Decode a message from The Martian's rover-camera hex signal, one byte at a time.
Click a letter to guess the next one.
| A41 | B42 | C43 | D44 | E45 | F46 |
| G47 | H48 | I49 | J4A | K4B | L4C |
| M4D | N4E | O4F | P50 | Q51 | R52 |
| S53 | T54 | U55 | V56 | W57 | X58 |
| Y59 | Z5A | SP20 | !21 | ?3F |
Background
In The Martian, Mark Watney is stranded on Mars with no way to talk to
NASA directly. The only working camera nearby is on the old Pathfinder
rover, and all NASA can do with it is aim it: pan left or right, tilt up or
down. That’s not nothing, though. Mark lays a grid of hexadecimal digits,
0 through F, out where the camera can see it: 16 signs spaced 22.5
degrees apart in a full circle around the rover, one for each digit. NASA
rotates the camera to one grid position, and Mark reads off one hex digit.
Two digits make one byte, and one byte, run through ASCII, is one
character. Rotate, pause, rotate, pause, and a sentence spells itself out
one letter at a time.
Watch the scene (starts around 3:40, runs to about 4:00).
Try 484921 above to see how it works, then see the specification below
for what your own program needs to do.
| Dec | Bin | Hex | Chr |
|---|---|---|---|
| 72 | 1001000 | 48 | H |
| 73 | 1001001 | 49 | I |
| 33 | 0100001 | 21 | ! |
484921
into HI! See the
complete ASCII / hex table
for everything else.
Getting Started
Log into cs50.dev, click on your terminal window, and run:
cd
mkdir pathfinder
cd pathfinder
code pathfinder.py
That creates a new folder called pathfinder, moves into it, and opens a
new, empty file called pathfinder.py for you to edit.
Specification
Implement a program, pathfinder.py, that decodes a hexadecimal
transmission from Mars back into the message it spells out.
- Print
Transmission:(with a trailing space, no newline) and prompt the user for a string of hex digits withinput(). - You can assume the transmission is well-formed: an even-length string
of hexadecimal digits (
0-9,A-F), with no spaces or other characters mixed in. - Every two characters is one byte. Convert each byte to the character it represents in ASCII, and build up the decoded message one byte at a time.
- Print the decoded message, followed by a newline.
Usage
Your program should behave like the demo below.
$ python pathfinder.py Transmission: 535441545553 STATUS $ python pathfinder.py Transmission: 4849204D4F4D HI MOM $ python pathfinder.py Transmission: 4E4F542044454144 NOT DEAD
Hints
Need a hint?
- Converting two hex digits into the byte they represent is a change of
base:
int("4D", 16)gives you77, the same wayint("42")gives you42, just reading the string in base 16 instead of base 10. Try it with a pair of digits from your own transmission in place of"4D". chr()turns that integer into the character it corresponds to in ASCII, the reverse of whatord()does.- You’ll build the decoded message one character at a time. Starting
with an empty string and adding to it inside a loop works:
message = message + chr(...), or the shorthandmessage += chr(...). - You still need a way to walk through the transmission two characters at a time instead of one, and there’s more than one way to set that loop up. Think about what you want your loop variable to count, and how you’d turn each count into a two-character piece of the string.
To Get Full Credit
It’s more important that you submit a working solution than that you do everything below. Submit early, then keep improving and resubmit as many times as you like.
- Structure your program with at least one function besides
main(). You might write one to decode the transmission, or one to convert a single byte to a character; that part’s up to you. - At the end of the program, add a comment describing any challenges you ran into or what you’d improve if you did this again. A sentence or two is fine.
Bonus
For extra credit: NASA isn’t the only one who needs to send a message.
If your program is run with a single command-line argument, -e, have it
encode instead of decode: print Message: (with a trailing space, no
newline), prompt for a line of plain text with input(), and print its
hex encoding instead, uppercase, two digits per character, with no spaces
between them.
$ python pathfinder.py -e
Message: Hello
48656C6C6F
Your program should still decode as before when run with no arguments; check50 needs that to still pass.
Style and Submission
Run these one at a time, from inside your pathfinder folder.
Check your style:
style50 pathfinder.py
Check your correctness:
check50 porttack/cs50/problems/py/pathfinder
Submit your work:
submit50 porttack/cs50/problems/py/pathfinder
Glossary
- hexadecimal — Base-16, using 0 through 9 and A through F. Four bits per digit, so one byte is exactly two hex digits.
- byte — Eight bits. Enough to hold one of 256 values.
- ASCII — A table assigning a number from 0 to 127 to each of a small
set of characters.
int()andchr()move between a hex byte and the character it represents. - string — A type that represents sequences of characters.
Version history
- Loading commit history…