Python打卡Day15

Question 54

Question

Assuming that we have some email addresses in the "username@companyname.com" format, please write program to print the company name of a given email address. Both user names and company names are composed of letters only.

Example: If the following email address is given as input to the program:

1john@google.com

Then, the output of the program should be:

1google

In case of input data being supplied to the question, it should be assumed to be a console input.


Hints

Use \w to match letters.


Main author's Solution: Python 2

1import re
2emailAddress = raw_input()
3pat2 = "(\w+)@(\w+)\.(com)"
4r2 = re.match(pat2,emailAddress)
5print r2.group(2)

My Solution: Python 3

1import re
2
3email = "john@google.com elise@python.com"
4pattern = "\w+@(\w+).com"
5ans = re.findall(pattern,email)
6print(ans)

Question 55

Question

Write a program which accepts a sequence of words separated by whitespace as input to print the words composed of digits only.

Example: If the following words is given as input to the program:

12 cats and 3 dogs.

Then, the output of the program should be:

1['2', '3']

In case of input data being supplied to the question, it should be assumed to be a console input.


Hints

Use re.findall() to find all substring using regex.


Main author's Solution: Python 2

1import re
2s = raw_input()
3print re.findall("\d+",s)

My Solution: Python 3

1import re
2
3email = input()
4pattern = "\d+"
5ans = re.findall(pattern,email)
6print(ans)

OR

1email = input().split()
2ans = []
3for word in email:
4    if word.isdigit():     # can also use isnumeric() / isdecimal() function instead
5       ans.append(word)
6print(ans)

OR

1email = input().split()
2ans = [word for word in email if word.isdigit()]  # using list comprehension method
3print(ans)

Question 56

Question

Print a unicode string "hello world".


Hints

Use u'strings' format to define unicode string.


Main author's Solution: Python 2

1unicodeString = u"hello world!"
2print unicodeString

Question 57

Question

Write a program to read an ASCII string and to convert it to a unicode string encoded by utf-8.


Hints

Use unicode()/encode() function to convert.


Main author's Solution: Python 2

1s = raw_input()
2u = unicode( s ,"utf-8")
3print u

My Solution: Python 3

1s = input()
2u = s.encode('utf-8')
3print(u)

Question 58

Question

Write a special comment to indicate a Python source code file is in unicode.


Hints

Use unicode() function to convert.


Solution:

1# -*- coding: utf-8 -*-

Question 59

Question

Write a program to compute 1/2+2/3+3/4+...+n/n+1 with a given n input by console (n>0).

Example: If the following n is given as input to the program:

15

Then, the output of the program should be:

13.55

In case of input data being supplied to the question, it should be assumed to be a console input.


Hints

Use float() to convert an integer to a float.Even if not converted it wont cause a problem because python by default understands the data type of a value


Main author's Solution: Python 2

1n=int(raw_input())
2sum=0.0
3for i in range(1,n+1):
4    sum += float(float(i)/(i+1))
5print sum

My Solution: Python 3

1n = int(input())
2sum = 0
3for i in range(1, n+1):
4    sum+= i/(i+1)
5print(round(sum, 2))  # rounded to 2 decimal point

go to previous day

go to next day

Discussion