Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Description You are provided with a stub in which you need to insert your code where indicated without doing any changes to the existing code

Description
You are provided with a stub in which you need to insert your code where indicated
without doing any changes to the existing code to complete the task.
The current code will accept a zero or positive integer (that is, non-strictly negative integer)
with possible leading 0s and converts it to base 8(keeping leading 0s, if any).
Given the following directions:
0: Move North
1: Move North-East
2: Move East
3: Move South-East
4: Move South
5: Move South-West
6: Move West
7: Move North-West
Reading the number written in base 8 from right to left. We start from a position that is the
unique position where the switch is on. Moving to a position switches on to off and off to
on there. By default, all positions are off.
Your program should display the minimal rectangular shape that includes all on positions as
shown in the test cases below.
Test Cases
Enter a non-strictly negative integer: 0
Keeping leading 0's, if any, in base 8,0 reads as 0.
Enter a non-strictly negative integer: 00
Keeping leading 0's, if any, in base 8,00 reads as 00.
Enter a non-strictly negative integer: 0256
Keeping leading 0's, if any, in base 8,0256 reads as 0400.
Enter a non-strictly negative integer: 3654
Keeping leading 0's, if any, in base 8,3654 reads as 7106.
Enter a non-strictly negative integer: 100738324
Keeping leading 0's, if any, in base 8,100738324 reads as 600222424.
Python code start here
# Reading the number written in base 8 from right to left,
# keeping the leading 0's, if any:
# 0: move N 1: move NE 2: move E 3: move SE
# 4: move S 5: move SW 6: move W 7: move NW
#
# We start from a position that is the unique position
# where the switch is on.
#
# Moving to a position switches on to off, off to on there.
import sys
on ='\u26aa'
off ='\u26ab'
code = input('Enter a non-strictly negative integer: ').strip()
try:
if code[0]=='-':
raise ValueError
int(code)
except ValueError:
print('Incorrect input, giving up.')
sys.exit()
nb_of_leading_zeroes =0
for i in range(len(code)-1):
if code[i]=='0':
nb_of_leading_zeroes +=1
else:
break
print("Keeping leading 0's, if any, in base 8,", code, 'reads as',
'0'* nb_of_leading_zeroes + f'{int(code):o}.'
)
print()
#print(nb_of_leading_zeroes)
# INSERT YOUR CODE HERE

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Filing And Computer Database Projects

Authors: Jeffrey Stewart

2nd Edition

007822781X, 9780078227813

More Books

Students also viewed these Databases questions