-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcodewars.py
More file actions
189 lines (140 loc) · 4.34 KB
/
codewars.py
File metadata and controls
189 lines (140 loc) · 4.34 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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
# def get_a_down_arrow_of(n):
# temp = n
# result = []
# while n > 0:
# line = ''
# spaces = ''
# for i in range(1,n+1):
# line = line + str(i%10)
# reverse = line[0:-1]
# n = n - 1
# for j in range(temp - n - 1):
# spaces = spaces + ' '
# result.append(spaces + line+reverse[::-1])
# return '\n'.join(result)
# def half(i,n):
# return "".join(str(d%10)for d in range(1,n-i+1))
# def line(i,n):
# h = half(i,n)
# return " " * i + h + h[-2::-1]
# def get_a_down_arrow_of(n):
# return "\n".join(line(i,n) for i in range(n))
# insert = input('number:')
# print(get_a_down_arrow_of(insert))
#############
# def is_palindromic(val):
# val = str(val)
# return val == val[::-1]
# def next_pal(val):
# while True:
# val = val + 1
# if is_palindromic(val):
# return val
# insert = input('number:')
# print(next_pal(insert))
#################
# def match_arrays(v,r):
# vset = set(v)
# rset = set(r)
# return len(vset & rset)
# import pickle
# def match_arrays(v,r):
# dic = {}
# total = 0
# for i in v:
# dic[pickle.dumps(i)] = True
# for j in r:
# if pickle.dumps(j) in dic:
# total += 1
# return total
# print(match_arrays(['Perl','Closure','JavaScript',[1,2]],['Go', 'C++','JavaScript']))
# def match_arrays(v, r):
# return sum(1 for x in v if x in r)
# import math
# def is_square(n):
# if n < 0:
# return False
# nsqrt = math.sqrt(n)
# result = math.modf(nsqrt)
# return result[0] == 0
# def is_square(n):
# return n ** .5 % 1 == 0 if n > 0 else False
# insert = input('number')
# print(is_square(insert))
def min_max(lst):
lst = sorted(lst)
return [lst[0],lst[-1]]
def make_lazy(*args):
return lambda : args[0](*args[1:])
# from functools import partial as make_lazy
def modding(a,b):
return a%b
class HTMLGen(object):
def __getattr__(self, name):
def wrapper(text):
if name == "comment":
return "<!--{0}-->".format(text)
else:
return "<{0}>{1}</{0}>".format(name, text)
return wrapper
def my_parse_int(string):
try:
return int(string)
except ValueError:
return "NaN"
#(123) 456-7890
import re
def validPhoneNumber(phoneNumber):
if re.match(r'^\(\d{3}\) \d{3}\-\d{4}$',phoneNumber):
return True
else:
return False
def multiplication_table(row,col):
result = []
for i in range(1,row+1):
temp = []
for j in range(0,col):
temp.append((j+1)*i)
result.append(temp)
return result
def multiplication_table(row,col):
return [[x*y for y in range(1,col+1)] for x in range(1,row+1)]
def find_it(seq):
for i in seq:
if seq.count(i)%2!=0:
return i
def duplicate_encode(word):
return "".join(["(" if word.lower().count(c) == 1 else ")" for c in word.lower()])
# print duplicate_encode('din')
def tribonacci(signature,n):
result = signature
while len(result) < n:
result.append(signature[-1]+signature[-2]+signature[-3])
return result
# print tribonacci([1,1,1],10)
from random import randint,seed
seed(1)
guess = randint(1,100)
seed(1)
# greddy alorithem
def knapsack(capacity,items):
value_per_weight = []
result = []
k = 0
for item in items :
value_per_weight.append((k,float(item[1])/item[0]))
k += 1
value_per_weight.sort(lambda x,y:cmp(x[1],y[1]),reverse=True)
print(value_per_weight)
for item in value_per_weight:
result.insert(item[0],capacity/item[1])
capacity = capacity % item[1]
if capacity == 0:
break
return result
print(knapsack(100,((60,80),(50,50))))
def group_check(s):
while "{}" in s or "()" in s or "[]" in s:
s = s.replace("{}","").replace("()","").replace("[]","")
return not s
print(group_check("({"))