worksheet for week 4

warmup

Try string functions

s = "ABCDEFGHI"
i = 3
j = 5
print(s[i])  .............
print(s[5+3])  ..........
print(s[i:j+1])  ..........
print(s.find("D")) .......

Before you start the work, please spend time to familiarize yourself with getting a character from a string.  Here is how to do it.  Use “for c in s” to grab an individual c from the string s.  As it is in for-loop, c will be initialized to a new character each time the body of loop is executed. In the end, the string s runs out, so “c in s” will return a False and the loop terminated.  See this example of printing each character of a string on a line of its own.

for c in “hello world”:
    print(c)



work 1  increasing number

Fill in  the blank

The input is a string of numbers, “12345”.  It is increasing-digit if the successive number is larger.  Write a program to check if an input string is increasing-digit.  ("12321" is not increasing-digit).

n = input("Enter digits : ")
is_increasing = __________
prev = ___________         # begin with “small” number
for d in ____________ :    # get each character in the input
    if _________________ : # compare the current char with the previous one
        is_increasing = __________
        break
    prev = d               # update the previous char

if is_increasing :
    print("Yes, this is an increasing-digit number")
else :
    print("No, this is not an increasing-digit number")


work 2  Arabic to Thai

Fill in the blank

Write a program to convert a string of Arabic number to Thai numeral.

in_txt  = input("Enter digits : ")
out_txt = ""
arabic_number = "0123456789"
thai_number   = "๐๑๒๓๔๕๖๗๘๙"
for _____________________ :
    k = ______________________
    if k < 0 :
        out_txt += _________________
    else :
        out_txt += _________________
print(out_txt)


Hint:  One way to do it is to use s.find(…) to locate an arabic numeral in arabic_number.  That will give you an index.  Use that index to get thai_number.  This is an old trick, invented since the early days of programming (that we use constant string to store information).  The other way is to use the function ord() and chr().  Here is what they are:

print(ord(“1”))
49
print(chr(49))
1

print(ord(“๑”))
3665


The ord(c) gives the “coding” of the character c.  The chr(i) returns the character with the code i. 

work 3 write to a file

Write a program to output a file of the following pattern 1111 … 9999, one number on a line. So, the output file contains nine lines.

1111
2222
3333
....
9999


work 4  read and convert file

read the above file and then convert each line to a number and +1  then write it out as string.  the output will be

1112
2223
3334
...
10000



Show Code

For work 2, arabic to thai, I am going to show you an alternative way to coding, using ord() and chr().  I know by exploring a bit the code of arabic and thai numeral.

print(ord(“1”))
49
print(ord(“๑”))
3665


So, I know the "distance" of the code of Thai numeral and Arabic.  It is 3665-49 = 3616.  I then convert arabic to thai by adding 3616 to its code.  Then make it a string by chr(code).  Here is the complete solution:

# convert input of arabic numeral to thai

inp = input()
out = ""
for c in inp:
    acode = ord(c)
    tcode = acode + 3616
    out += chr(tcode)
print(out)

How I find out ord() and chr() functions?   I google "how to convert character in python" and on my computer the first link that comes up is:
http://stackoverflow.com/questions/704152/how-can-i-convert-a-character-to-a-integer-in-python-and-viceversa

Enjoy!