-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFacebook221018
More file actions
83 lines (66 loc) · 2.02 KB
/
Facebook221018
File metadata and controls
83 lines (66 loc) · 2.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Tue Oct 15 15:13:20 2019
@author: oscarbarbier
Good morning! Here's your coding interview problem for today.
This problem was asked by Facebook.
Given the mapping a = 1, b = 2, ... z = 26, and an encoded message,
count the number of ways it can be decoded.
For example, the message '111' would give 3, since it could be decoded as 'aaa', 'ka', and 'ak'.
You can assume that the messages are decodable. For example, '001' is not allowed.
"""
Solution Oscar
def count_decode (chaine) :
if len(chaine) == 1 or not chaine :
return 1
elif int(chaine[0]) > 2 :
return(count_decode(chaine[1:]))
elif chaine[0] == "2" :
if int(chaine[1]) > 6 :
return(count_decode(chaine[1:]))
elif int(chaine[1]) == 0 :
return(count_decode(chaine[2:]))
else :
return(count_decode(chaine[1:]) + count_decode(chaine[2:]))
elif chaine[0] == "1" :
if int(chaine[1]) == 0 :
return(count_decode(chaine[2:]))
else :
return(count_decode(chaine[1:]) + count_decode(chaine[2:]))
Solution Charles
from functools import reduce
def product(a,b):
return a * b
def fibo(n):
def go(a,b,n):
if n == 0:
return a
else:
return go(b,a+b,n-1)
return go(1,1,n)
def fb(txt):
l, s, ind = [], 0, 0
for c in txt:
print(c)
if c in {'1', '2'}:
s += 1
else:
if c == '0':
s -= 1
else:
if s > 0 and txt[ind - 1] == '1':
s += 1
if s > 0 and txt[ind - 1] == '2' and c in {'3', '4', '5', '6'}:
s +=1
if s == 0:
s += 1
l.append(s)
s = 0
ind += 1
if ind == len(txt) and c in {'1', '2'}:
l.append(s)
print(l)
fibs = [fibo(x) for x in l]
print(fibs)
return reduce(product, fibs)