|
1 | 1 |
|
| 2 | + |
| 3 | +# Question 54 |
| 4 | + |
| 5 | +### **Question** |
| 6 | + |
| 7 | +> ***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.*** |
| 8 | +
|
| 9 | +> ***Example: |
| 10 | +If the following email address is given as input to the program:*** |
| 11 | +``` |
| 12 | +john@google.com |
| 13 | +``` |
| 14 | +> ***Then, the output of the program should be:*** |
| 15 | +``` |
| 16 | +google |
| 17 | +``` |
| 18 | +> ***In case of input data being supplied to the question, it should be assumed to be a console input.*** |
| 19 | +
|
| 20 | +---------------------- |
| 21 | +### Hints |
| 22 | +> ***Use \w to match letters.*** |
| 23 | +
|
| 24 | +---------------------- |
| 25 | + |
| 26 | +**Main author's Solution: Python 2** |
| 27 | +```python |
| 28 | +import re |
| 29 | +emailAddress = raw_input() |
| 30 | +pat2 = "(\w+)@(\w+)\.(com)" |
| 31 | +r2 = re.match(pat2,emailAddress) |
| 32 | +print r2.group(2) |
| 33 | +``` |
| 34 | +---------------- |
| 35 | +**My Solution: Python 3** |
| 36 | +```python |
| 37 | +#to be written |
| 38 | + |
| 39 | +``` |
| 40 | +--------------------- |
| 41 | + |
| 42 | + |
| 43 | +# Question 55 |
| 44 | + |
| 45 | +### **Question** |
| 46 | + |
| 47 | +>***Write a program which accepts a sequence of words separated by whitespace as input to print the words composed of digits only.*** |
| 48 | +
|
| 49 | +>***Example: |
| 50 | +If the following words is given as input to the program:*** |
| 51 | +``` |
| 52 | +2 cats and 3 dogs. |
| 53 | +``` |
| 54 | +>***Then, the output of the program should be:*** |
| 55 | +``` |
| 56 | +['2', '3'] |
| 57 | +``` |
| 58 | +>***In case of input data being supplied to the question, it should be assumed to be a console input.*** |
| 59 | +
|
| 60 | + |
| 61 | +---------------------- |
| 62 | +### Hints |
| 63 | +> ***Use re.findall() to find all substring using regex.*** |
| 64 | +
|
| 65 | +---------------------- |
| 66 | + |
| 67 | +**Main author's Solution: Python 2** |
| 68 | +```python |
| 69 | +import re |
| 70 | +s = raw_input() |
| 71 | +print re.findall("\d+",s) |
| 72 | +``` |
| 73 | +---------------- |
| 74 | +**My Solution: Python 3** |
| 75 | +```python |
| 76 | +#to be written |
| 77 | + |
| 78 | +``` |
| 79 | +--------------------- |
| 80 | + |
| 81 | + |
| 82 | + |
| 83 | +# Question 56 |
| 84 | + |
| 85 | +### **Question** |
| 86 | + |
| 87 | +> ***Print a unicode string "hello world".*** |
| 88 | +
|
| 89 | +---------------------- |
| 90 | +### Hints |
| 91 | +> ***Use u'strings' format to define unicode string.*** |
| 92 | +
|
| 93 | +---------------------- |
| 94 | + |
| 95 | +**Main author's Solution: Python 2** |
| 96 | +```python |
| 97 | +unicodeString = u"hello world!" |
| 98 | +print unicodeString |
| 99 | +``` |
| 100 | +---------------- |
| 101 | +**My Solution: Python 3** |
| 102 | +```python |
| 103 | +#to be written |
| 104 | + |
| 105 | +``` |
| 106 | +--------------------- |
| 107 | + |
| 108 | +# Question 57 |
| 109 | + |
| 110 | +### **Question** |
| 111 | + |
| 112 | +> ***Write a program to read an ASCII string and to convert it to a unicode string encoded by utf-8.*** |
| 113 | +
|
| 114 | +---------------------- |
| 115 | +### Hints |
| 116 | +> ***Use unicode() function to convert.*** |
| 117 | +
|
| 118 | +---------------------- |
| 119 | + |
| 120 | +**Main author's Solution: Python 2** |
| 121 | +```python |
| 122 | +s = raw_input() |
| 123 | +u = unicode( s ,"utf-8") |
| 124 | +print u |
| 125 | +``` |
| 126 | +---------------- |
| 127 | +**My Solution: Python 3** |
| 128 | +```python |
| 129 | +#to be written |
| 130 | + |
| 131 | +``` |
| 132 | +--------------------- |
| 133 | + |
| 134 | +# Question 58 |
| 135 | + |
| 136 | +### **Question** |
| 137 | + |
| 138 | +> ***Write a special comment to indicate a Python source code file is in unicode.*** |
| 139 | +
|
| 140 | +---------------------- |
| 141 | +### Hints |
| 142 | +> ***Use unicode() function to convert.*** |
| 143 | +
|
| 144 | +---------------------- |
| 145 | + |
| 146 | +**Solution:** |
| 147 | +```python |
| 148 | +# -*- coding: utf-8 -*- |
| 149 | +``` |
| 150 | +---------------- |
| 151 | +# Question 59 |
| 152 | + |
| 153 | +### **Question** |
| 154 | + |
| 155 | +>***Write a program to compute 1/2+2/3+3/4+...+n/n+1 with a given n input by console (n>0).*** |
| 156 | +
|
| 157 | +>***Example: |
| 158 | +If the following n is given as input to the program:*** |
| 159 | +``` |
| 160 | +5 |
| 161 | +``` |
| 162 | +>***Then, the output of the program should be:*** |
| 163 | +``` |
| 164 | +3.55 |
| 165 | +``` |
| 166 | +>***In case of input data being supplied to the question, it should be assumed to be a console input.*** |
| 167 | +
|
| 168 | + |
| 169 | +---------------------- |
| 170 | +### Hints |
| 171 | +> ***Use float() to convert an integer to a float*** |
| 172 | +
|
| 173 | +---------------------- |
| 174 | + |
| 175 | +**Main author's Solution: Python 2** |
| 176 | +```python |
| 177 | +n=int(raw_input()) |
| 178 | +sum=0.0 |
| 179 | +for i in range(1,n+1): |
| 180 | + sum += float(float(i)/(i+1)) |
| 181 | +print sum |
| 182 | +``` |
| 183 | +---------------- |
| 184 | +**My Solution: Python 3** |
| 185 | +```python |
| 186 | +#to be written |
| 187 | + |
| 188 | +``` |
| 189 | +--------------------- |
0 commit comments