Hour 18: Using Special Data Types and Functions

Example 18.1, Page No 297

In [6]:
# There is no proper concept of Enum(Enumaration) in python so i have implemented in this manner
def enum(*sequential, **named):
    enums = dict(zip(sequential, range(len(sequential))), **named)
    return type('Enum', (), enums)


language = enum(human=100,animal=50,computer=51)

days = enum('SUN','MON','TUE','WED','THU','FRI','SAT')

print "human=%d, animal=%d, computer=%d" % (language.human,language.animal,language.computer)
print "SUN: %d" % days.SUN
print "MON: %d" % days.MON
print "TUE: %d" % days.TUE
print "WED: %d" % days.WED
print "THU: %d" % days.THU
print "FRI: %d" % days.FRI
print "SAT: %d" % days.SAT
human=100, animal=50, computer=51
SUN: 0
MON: 1
TUE: 2
WED: 3
THU: 4
FRI: 5
SAT: 6

Example 18.2, Page No 298

In [1]:
# There is no proper concept of Enum(Enumaration) in python so i have implemented in this manner
def enum(*sequential, **named):
    enums = dict(zip(sequential, range(len(sequential))), **named)
    return type('Enum', (), enums)

units= enum(penny=1,nickel=5,dime=10,quarter=25,dollar=100)
money_unit=[units.dollar,units.quarter,units.dime,units.nickel,units.penny]
unit_name=["dollar(s)","quarter(s)","dime(s)","nickel(s)","penny(s)"]

cent=int(raw_input("Enter a monetary value in cents:"))

print "Which is equivalent to:"
tmp=0
for i in range(5):
    tmp=cent / money_unit[i]
    cent = cent-(tmp*money_unit[i])
    if(tmp):
        print "%d %s" % (tmp,unit_name[i]),
Enter a monetary value in cents:141
Which is equivalent to:
1 dollar(s) 1 quarter(s) 1 dime(s) 1 nickel(s) 1 penny(s)

Example 18.3, Page No 301

In [20]:
# There is no proper concept of Enum(Enumaration) in python so i have implemented in this manner
def enum(*sequential, **named):
    enums = dict(zip(sequential, range(len(sequential))), **named)
    return type('Enum', (), enums)

constants=enum(ITEM_NUM=3,DELT=(ord('a')-ord('A')))

def Convert2Upper(str1,str2):
    str1=list(str1)
    str2=list(i for i in range(len(str1)+1))
    for i in range(len(str1)):
        if ((ord(str1[i])>=97) and (ord(str1[i])<=122)):
            str2[i]=chr(ord(str1[i])-constants.DELT)
        else:
            str2[i]=str1[i]
    str2[i+1]='\0'
    return str2
    
moon=["Whatever we wear","we become beautiful","moon viewing!"]
str1=""
term=0
str3=["","",""]

for i in range(constants.ITEM_NUM):
    if(str1==None):
        print "malloc() failed."
        term=1
        i=constants.ITEM_NUM
    str1=Convert2Upper(moon[i],str1)
    print moon[i]
    str3[i]=str1
print ""
for i in range(len(str3)):
    print "".join(str3[i])
Whatever we wear
we become beautiful
moon viewing!

WHATEVER WE WEAR
WE BECOME BEAUTIFUL
MOON VIEWING!

Example 18.4, Page No 303

In [34]:
# There is no proper concept of Enum(Enumaration) in python so i have implemented in this manner
def enum(*sequential, **named):
    enums = dict(zip(sequential, range(len(sequential))), **named)
    return type('Enum', (), enums)

con=enum(MIN_NUM=0,MAX_NUM=100)

def fRecur(n):
    if(n==con.MIN_NUM):
        return 0
    return (fRecur(n-1)+n)

sum1=sum2=0
for i in range(1,(con.MAX_NUM)+1):
    sum1+=i
    sum2=fRecur(con.MAX_NUM)

print "The value of sum1 is %d." % sum1
print "The value returned by fRecur() is %d." % sum2
The value of sum1 is 5050.
The value returned by fRecur() is 5050.

Example 18.5, Page No 306

In [1]:
#The output is correct as it takes the CMD LINES from respective terminal.
import sys
print "The value received by argc is " ,len(sys.argv),".\n"
print "There are ",len(sys.argv), "command-line arguments passed to main().\n"
print "The first command-line argument is: ",sys.argv[0],"\n"
print "The rest of the command-line arguments are:\n"
for i in range(1,len(sys.argv)):
    print sys.argv[i]
The value received by argc is  8 .

There are  8 command-line arguments passed to main().

The first command-line argument is:  -c 

The rest of the command-line arguments are:

-f
C:\Users\Vaibhav\.ipython\profile_default\security\kernel-683917c3-368a-425a-93a3-9782f6d369b5.json
--IPKernelApp.parent_appname='ipython-notebook'
--profile-dir
C:\Users\Vaibhav\.ipython\profile_default
--interrupt=852
--parent=1008