23 Jul 2018

Loop and Break in Python




j=0

while (j < 11):
    print("inside while J=" , j)
    if (j == 3):
        break

    j += 1 # increment j by 1 (same as j=j+1) 
    
print("I am free J=", j)

'''
Look at first line of code, we have set j=0 initially.
the program now can enter the while loop as value of j is less than 11
while (j < 11):
Since j = 0, program enters the loop and prints "inside while j= 0"
next it checks if ( j==3), in the first loop run j is 0, so it skips if (j==3) code block
Then it finds j+=1 , which means increment the value of j. It is same as j=j+1
If we dont do that program will end up a iternal loop.
now j=1
Then it returns to while (j < 11) : condition,
the j is 1 so it is again allowed to enter the loop and run while code block again
So this goes on. we know at every run j get incremented by 1
I becomes 1, 2 and then 3
since j < 11 it can enter the loop again with j=3
in the loop it finds if (j==3) : condition. Now it is true,
Now it goes into if block and finds the break statement
break statement breaks out from inner most loop block, that is while j < 11 code block
so it jumps out of while loop and prints "I am free J=3"

Shilpa64 Algohack
'''

Decimal to Roman Numeral Algorithm in Python


Problem :
Write a method that when passed an integer between 1 and 3000
returns a string in Roman numeral.
4 should return 'IIII'.

Hint: Use the integer division and modulus methods.
Roman Numerals : I = 1, V = 5, X = 10, L = 50, C = 100, D = 500, M = 1000

Objective : Convert a decimal number to a roman number.
Source( #1millionwomentotech)

This is a python solution for the problem

import math

d=0
while ((d > 3000) or (d==0)):
      d=int(input("Enter number 1-3000 :"))

rn=""

#our rules = [M => 1000 , D = 500, C => 100,  L => 50, X = 10,  V => 5, I => 1]   

decimal=[1000,500,100,50,10,5,1]
roman=["M","D","C","L","X","V","I"] 
i=0
for n in decimal:
   if (d==9) : 
        rn=rn+"IX" 
        break
   if (d==4):
        rn=rn+"IV"
        break
   
   q = math.floor(d/n)
   r= d % n
   rn=rn+ (q *  roman[i])  
   d=r
   i=i+1

print(rn)

The important question is how did we get it ?
I think it's worth learning.

Method:
We need to develop an algorithm to solve this problem.
An Algorithm is a sequence of steps to solve a problem. In each step we get closer to the solution.

Now We have to break the problem to small tasks to solve it.
Each task handles a part of the problem. It take inputs, process and output something.
The next task takes that output as an input, process and output something for the next task.
The input > process > output > input > process > output > input ...... goes on until all tasks are done.
We build the solution completing each task.
Each task may need few steps .
These steps are our lines of code.
Finally we may have the found the solution.
Remember in programming we find solution not at once. It is step wise.
 
What is important is identify problem and tasks to solve the problem.
Think it as a workflow.

Imagine flying from Fiji Island to small city in Cape Town. It is not one stop.
Multiple stop, flight cancellations, delays and all problems can come.
There always can be better ways.
This is my way!

1. Design Manual Algorithm
Think about how we would like to do it on paper. Describe It first!
I picked 2669 because it satisfies all Roman numerals.

2, Look at 2669 like an ancient Roman.
For a fellow Roman this number is made of  1000+1000+500+100+50+ 5+1+1+1+1
So what we have to do is break 2669 to 1000s, 500s, 100s, 50s, 10s, 5s and 1s.

3.0 Our Tasks

Step 1: Find how many 1000s in the number given.
Initial number = 2669
Since 2669 >= 1000 ; The initail largest base value is 1000
Divide 2669/1000. Quotient = 2, Remainder =669.
So the symbol M will be repeated twice.  M * Quotient
MM

Step 2: Find how many 500s in the remaining number
Now we convert remainder 669
1000 > 669 >= 500 ; The base value now 500.
Divide 669/500. Quotient = 1, Remainder =169. The symbol D repeated once. D * M * Quotient
MMD

Step 3: Find how many 100s in remaining number
Now we convert remainder 139
500 > 169 >= 100 ; largest base value is 100.
Divide 169/100. Quotient = 1, Remainder = 69. The symbol C repeated once.
MMDC

Step 4: Find how many 50s in remaining number
Now we convert remainder 69
100 > 69 >= 50 ; largest base value is 50.
Divide 69/50. Quotient = 1, Remainder = 19. The symbol L repeated once.
MMDCL

Step 5: Find how many 10s in remaining number
Now we convert remainder 19
50 > 19 >= 10 ; largest base value is 10.
Divide 19/10. Quotient = 1, Remainder = 9. The symbol X repeated once.
MMDCLX

Step 5: Find how many 5s in remaining number
Now we convert remainder 9
10 > 9 >= 5 ; largest base value is 5.
Divide 9/5. Quotient = 1, Remainder = 4 The symbol V repeated once.
MMDCLXV

Step 6: Find how many 1s in remaining number
Now we convert remainder 4
Divide 4/1. Quotient = 4, Remainder = 0 The symbol I repeated four times. IIII
MMDCLXVIIII

4. Fix Unexpected Problems:
Oh! our flight is not direct to the small city.  Tts an island, we have to take the boat and a car too.

Decimal 4 and 9 are represented in Roman numerals as IV and IX
Our algorithm gives IIII or VIIII . Roman's will not buy our program.
So we need a fix step 5 and 6 with some conditions

if remainder is 9 use IX
if remainder == 4 use IV

Use the algorithm and convert 0, 1, 5, 7, 10, 11, 25, 51, 105, 551, 1056, 2005 to Roman on paper.
You can convert any number to Roman in paper now.

Now lets write the algorithm in pseudo code as we did it manually.

Pseudo code is the steps we take to solve the problem, its not a programming language.
Pseudo code is not a standard, its English and math based description of how we intend to program the solution. This is my style. You can write it in your own way.
What we actually do is document the steps we did manually that way computer would do it.

rn=""

if not d <=3000  or d = 0 ask again 
let d= 2669
#floor gives quotient of a division  floor(7/2) is 3
q = floor(d/1000) => 2 * 1000 => 2 * M
#% is modular division  5 % 2 is 1
r= d % 1000 => 669
rn="MM" 

if r >=500 :  

d=r= 669
q = floor (d/500) => 1 * 500 => 1 * D
r= d % 500 => 169
rn="MM"+"D"

if r >= 100

d=r= 169
d = floor(d/100) => 1 * 100 => 1 * C
r= d % 100 => 69
rn="MMD"+"C"

if r >= 50

d=r= 69
d = floor(d/50) => 1 * 50 => 1 * L
r= d % 50 => 19
rn="MMDC"+"L"

if r >= 10

d=r=19
d = floor(d/10) => 1 * 10 => 1 * X
r= d % 10 => 9
rn="MMDCL"+"X"

if r >= 5

if r ==9 rn = "MMDCLX"+"IX"
else
d=r
d = floor(d/5) => n (1 or 0) 
    r= d % 5 => r1
    rn="MMDCL"+"V" 

if r >=1 
if r ==4 rn = "MMDCLX"+"IV"
    rn="MMDCL"+"I" * r 

Now test out few numbers again.
Do 0, 1, 111, 245, 1500, 2999, 3000, 3001 manually on your pseudo code to make sure.

If they works out, this seems to be our algorthm to solve the problem in steps.
This does not have to be perfect.
Because we never run pseudo code on a computer.

Now lets develop an algorithm for the computer.

First study the repeated patterns in manual algorithm.

To write python code we need to know about
They are our ingredients to code the solution.
Lists, List Elements, Iterating lists with for loops and breaks, Strings, integer division and modules operator, input and output function.

How do we represent base data and rules ?
our rules are [M => 1000 , D = 500, C => 100,  L => 50, X = 10,  V => 5, I => 1] 

our rules can be represented in two lists
decimal=[1000,500,100,50,10,5,1]
roman[M,D,C,L,X,V,I]

if number is bigger than 0 or  bigger than 3000 ask again
d=input a number
rn=""  initial roman number is empty 

We can see a loop in calculation part
i=0

for n in decimal[...]  here we process the list
   if d==9 rn=rn+"IX" break 
   if d==4 rn=rn+"IV" break
   #floor gives quotient of a division we know how many 100s when we do 350/100
   q = floor(divide d/n) 
  #  %  is modular operator gives a remainder of a division
   r= d % decimal[i] => 669 
   rn=rn+ q *  roman[q]  # build the roman number string
   d=r
   i=i+1

print(rn)

Now code your idea in python or in any other language.
This is how I teach kids to code algorithms. May suit aspiring beginners.
If you like this, I would write more lesson plans for your home work.