Pyhton Algorithm Assignment Instructions: Algorithms are a generic way of descri
ID: 3888434 • Letter: P
Question
Pyhton Algorithm Assignment
Instructions:
Algorithms are a generic way of describing a solution to a computational problem without use of any specific programming language syntax. It cannot be executed on a real computer, but it can model and resemble real programming code. Natural language is used whenever details are unimportant or distracting.
Your algorithm:
Must be unambiguous (exact)…are the steps numbered? …do we always know where to go next or are we left hanging?
Must be correct (solves the problem)…are the ‘requirements’ being met?
Must come to an end (terminate)…how do we know when to end this?
Must be general (for all cases)….can we input any data & still solve the problem?
Must be at the right level of detail….will a computer (in general) understand what we are asking it to do?
Requirements --> Write a solution to the problem accepts one number from the user in the range of 1 to 10 and displays the Roman numeral version of that number. If the number is outside of the acceptable range, then display an error message (tell the user she entered something wrong).
Number
Roman Numeral
1
I
2
II
3
III
4
IV
5
V
6
VI
7
VII
8
VIII
9
IX
10
X
Number
Roman Numeral
1
I
2
II
3
III
4
IV
5
V
6
VI
7
VII
8
VIII
9
IX
10
X
Explanation / Answer
A good algorithm takes consideration of all the necessary contditions which can happen
while a user intract with application
Below is the details of the algorithm which converts the user input in integer to roman numerals
Step1:- Take input from user.
Step2:- Validate if the input provided by user is integer and in between 1 to 10 or not
if the input is not valid ask the user to enter a valid input
Step3:- Write a function or logic that maps our integer number to roman numeral .
Step4:- asking user if he wants to exit the loop or not with options yes/no
Comparing our algorithm with requirements which are being asked
Your algorithm:
Must be unambiguous (exact)…are the steps numbered? …do we always know where to go next or are we left hanging?
Answer:-
Yes our algorith is being defined in 4 steps with clearly indicating what actions are taken place
Must be correct (solves the problem)…are the ‘requirements’ being met?
Answer:- Yes our algorithm solve our asked problem
Must come to an end (terminate)…how do we know when to end this?
Answer:- Yes our algorithm ask user if he wants to terminate the solution if his needs are fullfilled
Must be general (for all cases)….can we input any data & still solve the problem?
Answer:- Yes we have applied checks in our algorithm to check if the input is valid or not
Must be at the right level of detail….will a computer (in general) understand what we are asking it to do?
Answer:- Yes we have made our algorithm detailed so that computer can understand what actions it need to take place when a specific event is taking place
I have used basic python functions to explain the code to make algorithm more appealing
Below is the details step of the algorithm which converts integer to roman numerals
Step1:- Take input from user.
suppose x to be input
Step2:- Validate if the input provided by user is integer and in between 1 to 10 or not
if the input is not valid ask the user to enter a valid input
x=0
while x>0 or 11<x :
try:
x=int(input())
break
except:
print("Please enter integer between 1 to 10 as input")
Step3:- Write a function or logic that maps our integer number to roman numeral .
We can do it using if /else contd. i.e
if x ==1 : ## here x is our input
print ('I') ## 1 is represented by 'I' in Roman
or we can write a function which will take care of the mapping
function toRoman(num) {
var result = '';
var integer = [ 10, 9, 5, 4, 1];
var roman = [ "X","IX","V","IV","I"];
for (var i = 0;i<=decimal.length;i++) {
// looping over every element of our arrays
while (num%integer[i] < num) {
// keep trying the same number until it won't fit anymore
result += roman[i];
// add the matching roman number to our result string
num -= integer[i];
// remove the decimal value of the roman number from our number
}
}
return result;
}
Output:-
toRoman(5) // returns 'V'
toRoman(3) // returns 'III'
Step4:- asking user if he wants to exit the loop or not with options yes/no
while True
again = input("Do you want to try again? Press Yes if you want to ").strip().lower()
if again != "yes":
break
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.