#6-2

import random
lists=[]
for i in range(0,random.randrange(0,101)):
	n = random.randrange(0,2**31)
	lists.append(n) 
while True:
	try:
		newlist =[]
		for i in range(0,random.randrange(0,101)):
			newlist.append(lists[i])
		print newlist
		break
	except IndexError , e:
		print 'Error occurs:',e

#6-3
inplist = raw_input('enter a list of nums:\n').strip().split(',')
inplist.sort(lambda x,y:cmp(x.lower(),y.lower()),reverse = True)
print inplist


#6-5

# exercise b in 6-5

#-*- coding:UTF-8 -*-
import string
def issame(str1=None,str2=None):
	'''Judge the given strings same or different'''
	# str1,str2 = raw_input('Enter two strings to judge: ').strip().split(',')
	strs = string.printable
	maxlen = max(len(str1),len(str2))
	strdict = {}
	for x in xrange(0,100):
		strdict[strs[x]] = x
	try:
		for x in range(0,maxlen):
			if strdict[str1[x]] ^ strdict[str2[x]]:
				# print 'different strings'
				return 0
				break
		else:
			# print 'same strings'
			return 1
	except IndexError:
		# print 'different string' 
		return 0

#exercise c in 6-5
def isrepeat(substr=None,orgstr=None):
	while True:
		orgstr = raw_input('Enter the orignal string : ')
		substr = raw_input('Enter the string to find it if repeat in the orignal string: ')
		if substr not in orgstr:
			print '%s not in %s' % (substr,orgstr)
		else:
			sublen   = len(substr)
			subindex = orgstr.find(substr)
			try:
				while True:
					afterstr = orgstr[subindex+sublen:subindex+sublen*2]
					if issame(substr,afterstr):
						print '%s repeats in %s'%(substr,orgstr)
						return 1
						break
					else:
						subindex = orgstr.index(substr,subindex+1,len(orgstr))
			except (IndexError,ValueError),e:
				print '%s does not repeat in %s'%(substr,orgstr)
				return 0

#exercise d in 6-5
def addreverse(mstr=None):
	mlen = len(mstr)
	for item in [mstr[-1-i] for i in range(0,mlen)]:
		mstr+=item
	print mstr

if __name__ == '__main__':
	isrepeat()
	addreverse('WARMACHINEROCKS')

#6-6
def stripspaces(strs=None):
	while True:
		if strs[0] == ' ':
			strs = strs[1:]
		elif strs[-1] == ' ':
			strs = strs[:-1]
		else:
			return strs
			break
if __name__ == '__main__':
	strs = raw_input("Enter a string :")
	print stripspaces(strs)

#6-7
# -*-coding:utf-8 -*-
#功能:刪除表中能被輸入的數整除的元素
num_str = raw_input('Enter a number: ')
num_num = int(num_str)
fac_list = range(1,num_num+1)
print "Befor: ", fac_list
i=0
while i<len(fac_list):
	if num_num%fac_list[i]==0:
		del fac_list[i]
	else:
		i+=1
print "After: ",fac_list

#6-8

tylist = ['','','twenty','thirty','forty','fifty','sixty','seventy','eighty','ninety']
thlist = ['ten','eleven','twelve','thirteen','forteen','fifteen','sixteen','seventeen','eighteen','nineteen']
nums   = ['','one','two','three','four','five','six','seven','eight','nine']
hund = '-hundred '
while True:
	inputstr = raw_input('Enter a number: ').strip()
	inlen = len(inputstr)
	inputs = int(inputstr)
	if inputs in range(0,1000):
		if inlen == 1:
			print nums[inputs]
		elif inlen ==2:
			if inputstr[0] == '1':
				print thlist[int(inputstr[1])]
			else:
				print tylist[int(inputstr[0])]+'-'+nums[int(inputstr[1])]
		elif inlen == 3:
			if inputstr[1] == '1':
				tail = thlist[int(inputstr[2])]
			else:
				tail = tylist[int(inputstr[1])]+'-'+nums[int(inputstr[2])]
			print nums[int(inputstr[0])]+hund+tail
	else:
		print 'over range,try again'

#6-12

def findchr(strs,chrs):
	lenth = len(strs)
	if chrs in strs:
		for i in range(0,lenth):
			if strs[i]==chrs:
				print i,
	else:
		print '%s not in %s'%(chrs,strs)
		return -1

def rfindchr(strs,chrs):
	lenth = len(strs)
	if chrs in strs:
		for i in range(0,lenth):
			if strs[-1-i]==chrs:
				print -1-i+lenth,
	else:
		print '%s not in %s'%(chrs,strs)
		return -1
def subchr(strs,origchr,newchr):
	if origchr in strs:
		for i in range(0,len(strs)):
			if strs[i]==origchr:
				strs=strs[:i]+newchr+strs[i+1:]
		print strs
	else:
		print '%s not in %s '%(origchr,strs)
		return -1

if __name__ == '__main__':
	subchr('hekkkosd','f','a')	

#6-13

def atoc(strs = None):
	strs = raw_input('Enter a complex number:').strip()
	operators = ['+','-']
	stlen = len(strs)
	for i in xrange(0,stlen):
		op1 = 0
		op2 = strs[:-1] 
		if strs[-1-i] in operators:
			if strs[-2-i] == 'e':
				for x in xrange(0,len(strs[:-2-i])):
					if strs[:-2-i][-1-x] in operators:
						op1 = strs[:-1-i]
						op2 = strs[-1-i:-1]
						break
					else:
						continue
			else:
				op1 = strs[:-1-i]
				op2 = strs[-1-i:-1]
				break
		else:
			continue
	try:
		result = complex(float(op1),float(op2))
		print 'result: ',result,type(result)
	except ValueError:
		print 'Error occurs: ivalid input.\nplease makesure you have input a complex num'
if __name__ == '__main__':
	atoc()

#6-14
# -*- coding:UTF-8 -*-
import random
def game():
	menu = '\
	 選擇以下命令出拳:\n(s)石頭\n(j)剪刀\n(b)布\n'
	cmds = ['石頭','剪刀','布']
	(s,j,b) = cmds
	usrinput = eval(raw_input(menu).strip().lower())
	cptinput = random.choice(cmds)
	if usrinput == cmds[cmds.index(cptinput)-1]:
		print '您贏了~\n\t電腦出的是:',cptinput,'\n\t您出的是:', usrinput
	elif usrinput == cmds[cmds.index(cptinput)-2]:
		print '您輸了~\n\t電腦出的是:',cptinput,'\n\t您出的是:', usrinput
	else:
		print '您和電腦打成平手,出的都是:',cptinput 
if __name__ == "__main__":
	game()

#6-16(有點糙。。。)

M = [[1,1,2],[2,0,1]]
N = [[0,2,3],[1,1,2],[2,1,1]]
res = [[],[]]
result = [[],[]]
print 'The matrix M:',
for x in xrange(0,len(M)):
	print '\n'
	for i in xrange(0,len(M[0])):
		print '\t',M[x][i],
print
print 'The matrix N:',
for x in xrange(0,len(N)):
	print '\n'
	for i in xrange(0,len(N[0])):
		print '\t',N[x][i],
print
print 'M + N:',
for x in xrange(0,len(M)):
	print '\n'
	for i in range(0,len(M[0])):
		res[x].append(M[x][i]+N[x][i])
		print '\t',res[x][i],
print
for x in xrange(0,len(M)):
	for p in xrange(0,len(N[0])):
		sums = 0
		for i in xrange(0,len(N)):
			sums+=M[x][i]*N[i][p]
		result[x].append(sums)
print 
print 'M * N:',
for x in xrange(0,len(result)):
	print '\n'
	for i in xrange(0,len(result[0])):
		print '\t',result[x][i],


.