Question: Understanding how to extract digits from a multi-digit number in groups and by themselves (relevant for zyLab 3.15. NOTE: for zyLab 3.15 you MUST


Understanding how to extract digits from a multi-digit number in groups and by themselves (relevant for zyLab 3.15. NOTE: for zyLab 3.15 you MUST write a solution that uses the // (whole number division, floor division, often just called "div") and the % (modulo, remainder, often just called "mod") operators on an INTEGER NUMBER. NO USING STRINGS for the 3.15 lab assignment. However, do not toss your code out. You are going to get an additional lab assignment for this week that will ask you to solve that problem using strings. But there will be IMPORTANT RESTRICTIONS on what you can do. Why? Because using more advanced concepts and techniques we have not discussed at all is NOT a way to learn computer science. It is a way to use things you don't particularly well understand. It is not the way we teach. That is why a lot of our problems are very explicity about what to use in your solution and what NOT to use. Open a python shell window, a fresh one, do the following work that window, then save that window and SUBMIT IT with your work in it. DO NOT CLOSE THE SHELL WINDOW until you are done with this little problem. DO THIS DURING our lab time tonight. AT THE SHELL PROMPT (where you are typing all these things) Set a variable phNum to a possible phone number, for example: phNum = 1234567890 Notice that the parts of the phone number, area code, exchange, and extension, are as follows: 123 is the area code, 456 is the exchange, 7890 is the extension using this terminology It turns out it is easy to extract the "last x digits" from any integer using the remainder operator. Type the following in the shell and type what is printed below what you typed (type in and press enter key): (I did the first one for you) To Type in shell: phNum % 10 phNum % 100 phNum % 1000 phNum % 10000 phNum % 100000 phNum % 1000000 Result: 0 this retrives the last ??? (how many) digits of the number ---NOW, when you use mod to divide by a power of ten, what is the relationship between the form of the power of ten and the number of digits that are retrieved each tim? Think about it, don't goole or look it up. SO to conclude this part of the exercise, it is very easy to retrieve the last n digits from an integer with the % operator where n is the number of digits you want to get. Conclusion: OK, we can get the last certain number of digits but only the extension is at the end of the number. What to do? CHOP OFF the digits you just extracted!! To explore that, type the following in the shell and write what is printed out when you do: To Type in shell: phNum // 10 phNum // 100 phNum // 1000 phNum // 10000 phNum // 100000 phNum // 1000000 Result: this retrives the last ??? (how many) digits of the number 0 What is the relationship between the power of 10 you are dividing by and the part of the number that is "removed"? Can you explain what the pattern is and why it happens? NOW how can you use these two ideas to get the three different pieces of the phone number store in separate variables? ALSO, note that once you have the first two pieces and have removed them from the number, the remaining value is the area code, no need for any more divisions.
Step by Step Solution
3.46 Rating (153 Votes )
There are 3 Steps involved in it
When you use the modulo operator to divide by a power of ten the relationship between the form of th... View full answer
Get step-by-step solutions from verified subject matter experts
