DAY 4
0

## 6kyu. Persistent Bugger.

Write a function, persistence, that takes in a positive parameter num and returns its multiplicative persistence, which is the number of times you must multiply the digits in num until you reach a single digit.

### For example:

`````` persistence(39) => 3  # Because 3*9 = 27, 2*7 = 14, 1*4=4
# and 4 has only one digit.
persistence(999) => 4 # Because 9*9*9 = 729, 7*2*9 = 126,
# 1*2*6 = 12, and finally 1*2 = 2.
persistence(4) => 0   # Because 4 is already a one-digit number.
``````

``````def persistence(n):
count = 0
while n > 9:
t = 1
while n != 0:
t *= n % 10
n //= 10
n = t
count += 1
return count
``````

``````n = reduce(lambda x, y: x * y, [int(x) for x in str(n)], 1)
``````

## 6kyu. Count the smiley faces!

Given an array (arr) as an argument complete the function countSmileys that should return the total number of smiling faces.
Rules for a smiling face:
-Each smiley face must contain a valid pair of eyes. Eyes can be marked as : or ;
-A smiley face can have a nose but it does not have to. Valid characters for a nose are - or ~
-Every smiling face must have a smiling mouth that should be marked with either ) or D.
No additional characters are allowed except for those mentioned.
Valid smiley face examples:
`:) :D ;-D :~)`
Invalid smiley faces:
`;( :> :} :]`

### Example cases:

``````countSmileys([':)', ';(', ';}', ':-D']);       // should return 2;
countSmileys([';D', ':-(', ':-)', ';~)']);     // should return 3;
countSmileys([';]', ':[', ';*', ':\$', ';-D']); // should return 1;
``````

2[: or ;] * 3[No, -, ~] * 2[) or D] = 12 種。

``````def count_smileys(arr):
count = 0
for i in arr:
if i == ':)' or i == ':D' or i == ';)' or i == ';D' or i == ':-)' or i == ':-D' or i == ';-)' or i == ';-D' or i == ':~)' or i == ':~D' or i == ';~)' or i == ';~D':
count += 1
return count
``````

``````[:;][-~]?[)D]
[:;][-~]*[)D]
``````

[-~]? → [-~] 出現 0 次 或 1 次
[-~]* → [-~] 出現 0 次以上