Skip to content

Commit 845d789

Browse files
committed
Python Code change
1 parent e7ddc4e commit 845d789

File tree

4 files changed

+140
-0
lines changed

4 files changed

+140
-0
lines changed

python/challenges/my_magic.py

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import sys
2+
3+
4+
def magic_number(n, max):
5+
n_str = str(n) # convert number in string (list)
6+
n_list = list(map(int, n_str)) # list of integer
7+
n_l_temp= list(map(int, n_str)) # list of integer temp copy
8+
init = 0 #init value
9+
valid = []
10+
total = len(n_list)
11+
for t in range(total):
12+
e = n_l_temp[init]
13+
check = e + init
14+
while max * e >= len(n_l_temp) or init >= len(n_l_temp):
15+
n_l_temp = n_l_temp.extend(n_l_temp)
16+
if n_l_temp[check] != 0 and n_l_temp[check] != e:
17+
if n_l_temp[check] not in valid:
18+
valid.append(n_l_temp[check])
19+
init += e
20+
else:
21+
break
22+
else:
23+
break
24+
return True if len(n_list) == len(valid) else False
25+
26+
line = "100 1000"
27+
split = line.split(" ")
28+
# '''if the line input is incorrect''''
29+
if len(split) < 2:
30+
print("-1")
31+
elif not split[0].isdigit() or not split[0].isdigit():
32+
print("-1")
33+
34+
magic_numbers = []
35+
a = int(split[0])
36+
b = int(split[1])
37+
max = len(split[1])
38+
for i in range(a, b + 1):
39+
if magic_number(i, max):
40+
magic_numbers.append(i)
41+
42+
if len(magic_numbers) > 0:
43+
print(*magic_numbers, sep="\n")
44+
else:
45+
print("-1")

python/challenges/ngram.py

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import sys
2+
3+
long_text = "Mary had a little lamb its fleece was white as snow; " \
4+
"And everywhere that Mary went, the lamb was sure to go. " \
5+
"It followed her to school one day, which was against the rule; " \
6+
"It made the children laugh and play, to see a lamb at school. " \
7+
"And so the teacher turned it out, but still it lingered near, " \
8+
"And waited patiently about till Mary did appear." \
9+
"\"Why does the lamb love Mary so?\" the eager children cry;\" " \
10+
"\"Why, Mary loves the lamb, you know\" the teacher did reply.\" "
11+
long_text = long_text.replace(",", "").replace(".", "").replace(";", "")
12+
text_list = long_text.split(" ")
13+
14+
15+
def ngrams(word, n):
16+
output = {}
17+
total = 0;
18+
for i in range(len(text_list) - n):
19+
g = ' '.join(text_list[i:i + n])
20+
if g.startswith(word):
21+
total += 1
22+
output.setdefault(g, 0)
23+
output[g] += 1
24+
25+
d = {k.replace(word + " ", ""): v / total for k, v in output.items() if True}
26+
27+
return {v[0]: v[1] for v in sorted(d.items(), key=lambda kv: (-kv[1], kv[0]))}
28+
29+
30+
print(ngrams("the", 2))

python/challenges/parentheses.py

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import sys
2+
3+
4+
def proper_parentheses(line):
5+
6+
count_p = 0 # counting parentheses
7+
count_s = 0 # counting happy faces
8+
count_r = 0 # counting sad faces
9+
10+
for i in range(len(line)):
11+
p = line[i - 1] if i > 0 else " "
12+
if line[i] == "(":
13+
count_p += 1
14+
if line[i] == ")":
15+
count_p -= 1
16+
if p == ":" and line[i] == ")":
17+
count_s += 1
18+
if p == ":" and line[i] == "(":
19+
count_r += 1
20+
if count_p < 0 and count_s < abs(count_p):
21+
return "NO"
22+
23+
return "NO" if count_p > 0 and count_r < count_p else "YES"
24+
25+
26+
for line in sys.stdin:
27+
print(proper_parentheses(line), end="")

python/challenges/phone_comp.py

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import sys
2+
3+
4+
def phone_to_word(number):
5+
keyboard = {
6+
'0': '0',
7+
'1': '1',
8+
'2': 'abc',
9+
'3': 'def',
10+
'4': 'ghi',
11+
'5': 'jkl',
12+
'6': 'mno',
13+
'7': 'pqrs',
14+
'8': 'tuv',
15+
'9': 'wxyz'
16+
}
17+
ret = []
18+
# recursive solution
19+
# O(n^n) with n = phone digits
20+
getCombination(number, ret, keyboard, "", 0)
21+
22+
return ','.join(ret)
23+
24+
25+
def getCombination(number, ret, keyboard, st, index):
26+
if index >= len(number):
27+
ret.append(st)
28+
return
29+
30+
chars = keyboard.get(number[index], '') #
31+
for i in chars:
32+
st += i
33+
getCombination(number, ret, keyboard, st, index + 1)
34+
st = st[0:len(st) - 1] # clean temp string
35+
36+
37+
for line in sys.stdin:
38+
print(phone_to_word(line.strip()), end="")

0 commit comments

Comments
 (0)